Skip to content

Restore model download cancellation parity - #1119

Open
bmehta001 wants to merge 3 commits into
mainfrom
bmehta001/model-download-cancellation-parity
Open

bmehta001 wants to merge 3 commits into
mainfrom
bmehta001/model-download-cancellation-parity

Conversation

@bmehta001

@bmehta001 bmehta001 commented Sep 18, 2026

Copy link
Copy Markdown
Contributor

Summary

  • restore JavaScript model-download cancellation through AbortSignal, including progress-callback bridging and late-abort-safe AbortError mapping
  • restore Python model-download cancellation through threading.Event while preserving user progress-callback exceptions
  • preserve the existing JavaScript callback-first API while adding AbortSignal as an optional second argument

Validation

  • Windows and WSL/Linux: Node addon build and TypeScript build/type checks
  • Windows and WSL/Linux: focused JavaScript model and lifecycle tests (11 passed; 6 real-model tests skipped without FOUNDRY_TEST_DATA_DIR)
  • Windows and WSL/Linux: Python download-cancellation tests (3 passed)
  • Biome lint for changed JavaScript/TypeScript/native-addon files
  • Ruff for changed Python source and tests
  • git diff --check

The full Biome formatter check on the Windows checkout reports pre-existing CRLF normalization differences; focused lint passes without suppressions.

Real-download cancellation stress

Each API used its own empty cache and the real qwen2.5-0.5b registry download. The matrix included two pre-start cancellations and eight active cancellations at thresholds from 0% through 3%, verifying progress occurred and the model remained uncached after every cancellation.

API Windows WSL/Linux
JavaScript AbortSignal 10/10 canceled; recovery download reached 100% 10/10 canceled
Python threading.Event 10/10 canceled; recovery download reached 100% 10/10 canceled

The successful recovery downloads reused the same partially downloaded cache after all cancellation attempts, validating cleanup/resume behavior. WSL cancellation used separate empty caches; full recovery was not repeated there to avoid two additional full model downloads.

Carry the legacy AbortSignal and threading.Event contracts into the v2 bindings so callers can stop native downloads instead of receiving progress-only APIs. Preserve completion races and callback exceptions while mapping signal-driven JavaScript cancellation to AbortError.

Files changed:
- sdk_v2/js/native/src/model.cc: bridge AbortSignal state into the native progress callback
- sdk_v2/js/src and test: restore overloads and verify cancellation semantics
- sdk_v2/python/src and test: restore cancel_event and preserve callback errors
- sdk_v2/python/README.md: document cooperative download cancellation

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>

Copilot-Session: 2d2c76d2-0c86-4672-b91b-3fcb54ed7906
Keep optional AbortSignal and callback-or-undefined variables accepted while rejecting duplicate signals, and make the Python cancellation example demonstrate cancellation.\n\nFiles changed:\n- sdk_v2/js/src/imodel.ts\n- sdk_v2/js/src/model.ts\n- sdk_v2/js/test/model-download.types.ts\n- sdk_v2/python/README.md\n\nCo-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>\nCopilot-Session: 2d2c76d2-0c86-4672-b91b-3fcb54ed7906
Copilot AI balanced review requested due to automatic review settings September 18, 2026 23:23
@vercel

vercel Bot commented Sep 18, 2026

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated
foundry-local Ready Ready Preview Sep 19, 2026 1:11am UTC

Request Review

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Copilot review overview

🔵 Needs a closer look

Cross-language cancellation, native threading, and JavaScript reference cleanup warrant final human validation.

Review effort: Balanced
Findings: None

What changed in this PR

Restores cooperative model-download cancellation across the JavaScript and Python v2 SDKs.

Changes:

  • Adds JavaScript AbortSignal support with native progress-callback bridging and AbortError mapping.
  • Adds Python threading.Event cancellation while preserving callback exceptions.
  • Adds runtime, type-level, lifecycle, and unit coverage.
File Description
sdk_v2/​python/​test/​unit/​test_model_download_cancellation.py Tests Python cancellation and callback exceptions.
sdk_v2/​python/​src/​foundry_local_sdk/​imodel.py Implements event-driven cancellation.
sdk_v2/​python/​README.md Documents Python cancellation usage.
sdk_v2/​js/​tsconfig.types.json Includes new type tests.
sdk_v2/​js/​test/​model.test.ts Tests active and pre-start cancellation.
sdk_v2/​js/​test/​model-lifecycle.test.ts Tests cached-model cancellation races.
sdk_v2/​js/​test/​model-download.types.ts Verifies supported overloads.
sdk_v2/​js/​src/​model.ts Validates signals and exposes overloads.
sdk_v2/​js/​src/​imodel.ts Updates the public interface.
sdk_v2/​js/​src/​detail/​native.ts Extends the native binding contract.
sdk_v2/​js/​native/​src/​model.cc Bridges abort events into native cancellation.

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

@bmehta001 bmehta001 self-assigned this Sep 19, 2026
Keep AbortSignal as an optional second argument so existing IModel implementations remain source-compatible while model downloads still support cancellation.

Files changed:
- sdk_v2/js/src/imodel.ts
- sdk_v2/js/src/model.ts
- sdk_v2/js/test/model-download.types.ts
- sdk_v2/js/test/model-lifecycle.test.ts
- sdk_v2/js/test/model.test.ts

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot-Session: 2d2c76d2-0c86-4672-b91b-3fcb54ed7906

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Copilot review overview

🟢 Approval recommended

Cancellation behavior, race handling, error mapping, and backward compatibility are implemented consistently and covered by focused tests.

Review effort: Balanced
Findings: None

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