Skip to content

feat: add model download cache manager#129

Merged
leehack merged 34 commits into
mainfrom
feat/model-source-resolver
May 11, 2026
Merged

feat: add model download cache manager#129
leehack merged 34 commits into
mainfrom
feat/model-source-resolver

Conversation

@leehack

@leehack leehack commented May 11, 2026

Copy link
Copy Markdown
Owner

Summary

  • Implements the native model download/cache manager for remote model sources, including cache policies, metadata, checksum validation, retries, cancellation, resumable downloads, and cache cleanup APIs.
  • Wires LlamaEngine.loadModelSource so file-backed/native backends download to a local cached path before loading, while URL-capable backends reject native-cache-only options.
  • Splits chat-app GGUF model and mmproj handling into independent asset sources so model/projector cache, download, delete, and activation behavior no longer assumes both files come from the same origin.
  • Hardens web cache identities by persisting digest-based cache markers instead of raw/redacted signed URLs, and rejects unsupported web local-filesystem asset combinations loudly instead of silently succeeding.
  • Migrates example server and model verification tools away from ad-hoc download logic and updates README/changelog/docs for the full download/cache behavior.

Closes #125

Production-readiness scope

This PR is intended to be merge-ready for the declared feature scope:

  • Users can load existing local models exactly as before via loadModel(...).
  • Users can load remote/local structured sources via the additive loadModelSource(...) API.
  • Native/file-backed users get managed download/cache behavior with progress, retry, resume, cancellation, metadata, checksum validation, and cache maintenance APIs.
  • URL-capable web users keep direct URL loading for simple supported requests.
  • Unsupported option/platform combinations fail with explicit errors instead of being treated as successful.
  • Chat app model and mmproj assets are resolved independently for mixed local/remote source combinations where the platform can actually load them.
  • No public API breaking changes are intended; existing loadModel(...) and loadModelFromUrl(...) callers are unchanged.

Intentionally deferred follow-ups

These are not required for the current feature to work, and are tracked separately to avoid merging incomplete scope into main:

Test Plan

  • dart analyze lib test tool/testing/verify_recommended_models.dart tool/testing/verify_models.dart
  • dart test -p vm -j 1 --exclude-tags local-only --reporter compact
  • dart test -p chrome test/unit/core/models/download/model_download_manager_stub_test.dart --reporter compact
  • cd example/llamadart_server && dart test --reporter compact
  • cd example/chat_app && flutter analyze
  • cd example/chat_app && flutter test
  • cd example/chat_app && flutter test integration_test/model_cache_mmproj_e2e_test.dart -d macos confirms local-only E2E is skipped by default
  • cd example/chat_app && flutter test --run-skipped -t local-only integration_test/model_cache_mmproj_e2e_test.dart -d macos
  • Root dart format --output=none --set-exit-if-changed .
  • Root dart analyze
  • git diff --check
  • Manual platform smoke/E2E coverage: macOS, Web Playwright real-app smoke, iOS simulator, Android emulator
  • GitHub CI: Analyze & Lint, Docs Build Check, Test Linux & Web, Test Native macOS, Test Native Windows, Native Prompt Reuse Parity

Review Notes

  • Final independent pre-commit/read-only reviews found no remaining merge blockers.
  • A second-pass production-readiness review after the latest PR body update returned PASS for the declared scope. Non-blocking follow-ups were either already tracked or have been filed as docs(models): clarify local ModelSource option semantics #137/feat(models): serialize concurrent downloads for the same cache key #138 and added to feat(chat-app): improve model/mmproj asset-level cache UX #132.
  • Review-found blockers were fixed before the latest green CI run:
    • Web cache markers no longer use raw/redacted URL strings that could collapse query-only differences; persisted keys use a digest of the canonical identity.
    • Web activation/download now rejects local filesystem projector sources instead of treating unsupported combinations as successful.
    • Percent-encoded traversal, async transient directory creation, URL-load error redaction, and resume documentation comments were addressed in later commits.
  • Added regression coverage for cache hits/misses, noCache cleanup, refresh preservation, retry policy, checksum mismatch cleanup, partial resume validators/If-Range, cancellation cleanup, cache list/get/remove/clear/prune, browser stub behavior, engine remote-source wiring, independent model/mmproj asset handling, and local-only real model+projector loading.

Current review-comment status

All Copilot review threads have been resolved after verifying that the comments were addressed by follow-up commits or updated documentation. No unaddressed merge-blocking review comment is known.

leehack added 2 commits May 10, 2026 20:03
Add structured ModelSource, ModelLoadOptions, resolver targets, and download/cache value models for future package-managed GGUF download flows.

Wire LlamaEngine.loadModelSource to preserve existing local loading and route remote sources through URL-capable backends while explicitly rejecting unsupported foundation options.

Document the additive API surface and add regression coverage for resolver validation, URL redaction, and placeholder download managers.
Copilot AI review requested due to automatic review settings May 11, 2026 01:28

Copilot AI 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.

Pull request overview

This PR introduces a first-party, package-managed model download + cache system for remote GGUF model sources (HTTP(S) and hf://), and wires it into LlamaEngine.loadModelSource(...) so native/file-backed backends download to a local cached file before loading while URL-capable web backends continue to load directly (with option restrictions). It also migrates example/testing tooling and updates docs/changelog to reflect the new structured model source workflow.

Changes:

  • Added new structured model source/value-model APIs (ModelSource, ModelLoadOptions, ModelResolver targets) plus a cross-platform ModelDownloadManager with native IO implementation and browser stub.
  • Updated LlamaEngine to support loadModelSource(...), including native download/cache integration, URL-backend option rejection, and URL redaction for logs/metadata.
  • Migrated server and verification tools to use the new download manager and added comprehensive unit coverage for source parsing, cache behavior, resume/retry, and cleanup.

Reviewed changes

Copilot reviewed 27 out of 27 changed files in this pull request and generated 5 comments.

Show a summary per file
File Description
website/docs/guides/model-lifecycle.md Documents loadModelSource(...), cache policies, manager APIs, and mobile download guidance.
website/docs/changelog/recent-releases.md Adds 0.6.12 release notes for the new model download/cache manager and engine wiring.
tool/testing/verify_recommended_models.dart Replaces ad-hoc HTTP download logic with DefaultModelDownloadManager.ensureModel(...).
tool/testing/verify_models.dart Switches model download path to ensureModel(...) while keeping legacy local-file behavior.
test/unit/core/models/model_source_test.dart Adds unit tests for ModelSource parsing, redaction, and deterministic keying.
test/unit/core/models/model_resolver_test.dart Adds tests for resolver targets, defaults, cancellation handling, and remote passthrough.
test/unit/core/models/model_load_options_test.dart Adds tests for option storage, header immutability, and validation.
test/unit/core/models/download/model_download_manager_test.dart Validates public export surface of the manager + IO implementation.
test/unit/core/models/download/model_download_manager_stub_test.dart Ensures browser stub throws the expected unsupported exception.
test/unit/core/models/download/model_download_manager_io_test.dart Adds end-to-end IO manager tests (cache policies, retry, resume, checksum, cleanup, prune APIs).
test/unit/core/models/download/model_download_manager_base_test.dart Tests the throwing base manager behavior for unsupported operations.
test/unit/core/models/download/model_cache_entry_test.dart Adds tests for progress math, cache entry JSON/redaction, and validations.
test/unit/core/engine/engine_test.dart Adds engine tests for loadModelSource(...) routing, progress forwarding, and URL redaction behavior.
test/integration/engine_integration_test.dart Updates native URL-loading expectation to LlamaUnsupportedException.
README.md Adds usage docs for downloading/caching remote GGUFs via structured sources.
pubspec.yaml Adds crypto dependency for SHA-256 keying/checksum support.
lib/src/core/models/model_source.dart Introduces ModelSource (path/http/hf) parsing, validation, cache keying, and redaction.
lib/src/core/models/model_resolver.dart Adds resolver interfaces + default resolver and load target value types.
lib/src/core/models/model_load_options.dart Adds cache policy + load options (headers, bearer token, sha256, resume, retries, cancel).
lib/src/core/models/download/model_download_manager.dart Adds conditional export for stub vs IO manager implementation.
lib/src/core/models/download/model_download_manager_stub.dart Adds non-IO stub that throws LlamaUnsupportedException.
lib/src/core/models/download/model_download_manager_io.dart Implements the native download/cache manager (streaming, .part, resume/retry, metadata, prune).
lib/src/core/models/download/model_download_manager_base.dart Adds base API types: progress, cache entry metadata, and throwing manager base.
lib/src/core/engine/engine.dart Wires structured model loading into the engine and adds URL redaction + option rejection.
lib/llamadart.dart Exports new public APIs (sources/options/resolver/manager).
example/llamadart_server/lib/src/features/model_management/infrastructure/model_service.dart Migrates server model acquisition to ModelSource + download manager.
CHANGELOG.md Adds 0.6.12 changelog notes for model source/download/cache manager feature set.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread lib/src/core/models/download/model_download_manager_base.dart Outdated
Comment thread lib/src/core/models/download/model_download_manager_io.dart Outdated
Comment thread lib/src/core/engine/engine.dart Outdated
Comment thread website/docs/guides/model-lifecycle.md
Comment thread README.md Outdated
@codecov-commenter

codecov-commenter commented May 11, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 89.15816% with 85 lines in your changes missing coverage. Please review.
✅ Project coverage is 77.74%. Comparing base (fc98c90) to head (2bfc7ad).

Files with missing lines Patch % Lines
lib/src/platform/io/model_download_manager_io.dart 88.86% 45 Missing ⚠️
lib/src/core/models/model_source.dart 87.42% 22 Missing ⚠️
...e/models/download/model_download_manager_base.dart 91.07% 10 Missing ⚠️
lib/src/core/engine/engine.dart 87.69% 8 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main     #129      +/-   ##
==========================================
+ Coverage   76.73%   77.74%   +1.00%     
==========================================
  Files          70       75       +5     
  Lines        8734     9512     +778     
==========================================
+ Hits         6702     7395     +693     
- Misses       2032     2117      +85     
Flag Coverage Δ
unittests 77.74% <89.15%> (+1.00%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 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.

Copilot AI 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.

Pull request overview

Copilot reviewed 39 out of 40 changed files in this pull request and generated 2 comments.

Comment thread lib/src/core/models/download/model_download_manager_base.dart Outdated
Comment thread lib/src/core/models/model_load_options.dart Outdated

Copilot AI 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.

Pull request overview

Copilot reviewed 39 out of 40 changed files in this pull request and generated 4 comments.

Comment thread lib/src/platform/io/model_download_manager_io.dart Outdated
Comment thread lib/src/platform/io/model_download_manager_io.dart
Comment thread lib/src/platform/io/model_download_manager_io.dart Outdated
Comment thread website/docs/guides/model-lifecycle.md

Copilot AI 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.

Pull request overview

Copilot reviewed 39 out of 40 changed files in this pull request and generated 3 comments.

Comment thread lib/src/platform/io/model_download_manager_io.dart Outdated
Comment thread lib/src/platform/io/model_download_manager_io.dart Outdated
Comment thread lib/src/core/models/model_load_options.dart Outdated

Copilot AI 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.

Pull request overview

Copilot reviewed 39 out of 40 changed files in this pull request and generated 1 comment.

Comment thread lib/src/platform/io/model_download_manager_io.dart

Copilot AI 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.

Pull request overview

Copilot reviewed 39 out of 40 changed files in this pull request and generated 2 comments.

Comment thread lib/src/platform/io/model_download_manager_io.dart Outdated
Comment thread lib/src/core/models/model_source.dart Outdated

Copilot AI 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.

Pull request overview

Copilot reviewed 39 out of 40 changed files in this pull request and generated 2 comments.

Comment thread lib/src/platform/io/model_download_manager_io.dart Outdated
Comment thread example/chat_app/lib/screens/manage_models_screen.dart

Copilot AI 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.

Pull request overview

Copilot reviewed 39 out of 40 changed files in this pull request and generated 2 comments.

Comment thread lib/src/platform/io/model_download_manager_io.dart Outdated
Comment thread lib/src/core/models/model_resolver.dart Outdated

Copilot AI 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.

Pull request overview

Copilot reviewed 39 out of 40 changed files in this pull request and generated 1 comment.

Comment thread lib/src/core/engine/engine.dart

Copilot AI 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.

Pull request overview

Copilot reviewed 39 out of 40 changed files in this pull request and generated 2 comments.

Comment thread lib/src/core/models/download/model_download_manager_base.dart
Comment thread lib/src/core/engine/engine.dart Outdated

Copilot AI 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.

Pull request overview

Copilot reviewed 39 out of 40 changed files in this pull request and generated 1 comment.

Comment thread lib/src/core/engine/engine.dart
@leehack leehack merged commit dec0d63 into main May 11, 2026
10 checks passed
@leehack leehack deleted the feat/model-source-resolver branch May 11, 2026 23:32
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.

feat(models): add model download and cache manager

3 participants