From 69b1063b451813f5e787561b6293d382b77cef1f Mon Sep 17 00:00:00 2001 From: "er.ivanov" Date: Mon, 30 Nov 2026 16:20:26 +0200 Subject: [PATCH] fix(memory): download ONNX Runtime from NuGet when GitHub returns 404 microsoft/onnxruntime was removed from GitHub in November 2026, so the release URL baked into ensure_ort_dll() returns 404 and the ONNX embedding models (bge-small, jina-code-v2, granite-97m) cannot start: VectorEngine initialization fails in MemoryManager::initialize and semantic search silently degrades to name matching while embedding_status still claims the embeddings are "building in the background". Keep the GitHub release zip as the primary source, and retry the same ORT_VERSION from the Microsoft.ML.OnnxRuntime nupkg, extracting runtimes/win-x64/native/onnxruntime.dll into the existing cache path. Extraction is split into extract_member_from_zip() so member lookup and its diagnostic can be tested without network access; two unit tests cover the extraction and the missing-member case. CHANGELOG gets an [Unreleased] entry. Verified on Windows 11 x64 with the cache emptied: the server downloaded the nupkg itself, cached onnxruntime.dll (11,567,648 bytes, sha256 52f8ebe8f08f369a44fed6d1cb680c7c89169795e1c2949ee25b88b538ef0948), then reported "Saved 714 symbol vectors ... model=BGE-Small-EN-v1.5 (384d, 512-tok context)" and symbol_search answered match_reason "Semantic". --- CHANGELOG.md | 14 ++ .../src/embedding/fastembed_embed.rs | 143 +++++++++++++++--- 2 files changed, 133 insertions(+), 24 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 7a3615c..4cfa00d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,20 @@ All notable changes to CodeGraph are documented here. Versions follow [Semantic Versioning](https://semver.org/). Releases from 0.18.1 on are tagged `vX.Y.Z`; earlier ones `vscode/vX.Y.Z`. +## [Unreleased] + +### Fixed + +- **Windows: ONNX Runtime download falls back to NuGet.** `microsoft/onnxruntime` + was removed from GitHub in November 2026, so the release URL baked into the + binary returns 404 and the ONNX embedding models (`bge-small`, `jina-code-v2`, + `granite-97m`) could not start — semantic search was silently unavailable while + the structural tools kept working. `ensure_ort_dll()` now retries the same + version from the `Microsoft.ML.OnnxRuntime` nupkg (`runtimes/win-x64/native/onnxruntime.dll`) + before giving up. A DLL already present next to the executable or in + `~/.codegraph/fastembed_cache/ort/` is still used as-is, so nothing changes for + working installs. + ## [0.21.1] - 2026-10-10 VS Code extension only; the engine stays 0.21.0. diff --git a/crates/codegraph-memory/src/embedding/fastembed_embed.rs b/crates/codegraph-memory/src/embedding/fastembed_embed.rs index a63fe0d..3450a7a 100644 --- a/crates/codegraph-memory/src/embedding/fastembed_embed.rs +++ b/crates/codegraph-memory/src/embedding/fastembed_embed.rs @@ -582,53 +582,148 @@ fn ensure_ort_dll(cache_dir: &std::path::Path) -> Result<()> { std::fs::create_dir_all(&dll_dir) .map_err(|e| MemoryError::model(format!("Failed to create ORT cache dir: {e}")))?; - // Download the official release zip - let url = format!( + // Primary source: the official GitHub release zip. GitHub removed + // microsoft/onnxruntime on ~2026-11-17 (assets and repo return 404), so + // fall back to the NuGet package, which ships the same DLL layout as + // runtimes/win-x64/native/onnxruntime.dll. + let gh_url = format!( "https://github.com/microsoft/onnxruntime/releases/download/v{ORT_VERSION}/onnxruntime-win-x64-{ORT_VERSION}.zip" ); + let dll_name_in_zip = format!("onnxruntime-win-x64-{ORT_VERSION}/lib/onnxruntime.dll"); + + if let Err(gh_err) = extract_zip_member(&gh_url, &dll_name_in_zip, &dll_dir, &dll_path) { + log::warn!("ONNX Runtime GitHub download failed ({gh_err}); trying NuGet..."); + let nuget_url = format!( + "https://api.nuget.org/v3-flatcontainer/microsoft.ml.onnxruntime/{ORT_VERSION}/microsoft.ml.onnxruntime.{ORT_VERSION}.nupkg" + ); + extract_zip_member( + &nuget_url, + "runtimes/win-x64/native/onnxruntime.dll", + &dll_dir, + &dll_path, + )?; + } + + // Clean up zip + let _ = std::fs::remove_file(dll_dir.join("onnxruntime.zip")); + + log::info!( + "ONNX Runtime v{} DLL installed at {}", + ORT_VERSION, + dll_path.display() + ); + std::env::set_var("ORT_DYLIB_PATH", &dll_path); - let response = ureq::get(&url) + Ok(()) +} + +/// Download `url` (a .zip or .nupkg) and extract `member` to `out`. +/// The archive is streamed to a temp file next to `out` and removed afterwards. +#[cfg(target_os = "windows")] +fn extract_zip_member( + url: &str, + member: &str, + tmp_dir: &std::path::Path, + out: &std::path::Path, +) -> Result<()> { + let response = ureq::get(url) .call() .map_err(|e| MemoryError::model(format!("Failed to download ONNX Runtime: {e}")))?; - // Stream to a temp file - let zip_path = dll_dir.join("onnxruntime.zip"); + let zip_path = tmp_dir.join("onnxruntime.zip"); let mut zip_file = std::fs::File::create(&zip_path) .map_err(|e| MemoryError::model(format!("Failed to create temp zip: {e}")))?; std::io::copy(&mut response.into_reader(), &mut zip_file) .map_err(|e| MemoryError::model(format!("Failed to write zip: {e}")))?; drop(zip_file); - // Extract onnxruntime.dll from the zip - let zip_file = std::fs::File::open(&zip_path) + let result = extract_member_from_zip(&zip_path, member, out); + + let _ = std::fs::remove_file(&zip_path); + result +} + +/// Copy a single entry out of an archive that is already on disk. +/// Split from the download so the extraction can be tested without a network. +#[cfg(target_os = "windows")] +fn extract_member_from_zip( + zip_path: &std::path::Path, + member: &str, + out: &std::path::Path, +) -> Result<()> { + let zip_file = std::fs::File::open(zip_path) .map_err(|e| MemoryError::model(format!("Failed to open zip: {e}")))?; let mut archive = zip::ZipArchive::new(zip_file) .map_err(|e| MemoryError::model(format!("Failed to read zip: {e}")))?; - - let dll_name_in_zip = format!("onnxruntime-win-x64-{ORT_VERSION}/lib/onnxruntime.dll"); - - let mut dll_entry = archive.by_name(&dll_name_in_zip).map_err(|e| { + let mut entry = archive.by_name(member).map_err(|e| { MemoryError::model(format!( - "onnxruntime.dll not found in zip at '{dll_name_in_zip}': {e}" + "onnxruntime.dll not found in zip at '{member}': {e}" )) })?; - - let mut out_file = std::fs::File::create(&dll_path) + let mut out_file = std::fs::File::create(out) .map_err(|e| MemoryError::model(format!("Failed to create DLL file: {e}")))?; - std::io::copy(&mut dll_entry, &mut out_file) + std::io::copy(&mut entry, &mut out_file) .map_err(|e| MemoryError::model(format!("Failed to extract DLL: {e}")))?; + Ok(()) +} - // Clean up zip - let _ = std::fs::remove_file(&zip_path); +#[cfg(all(test, target_os = "windows"))] +mod ort_extract_tests { + use super::extract_member_from_zip; + use std::io::Write; + + fn write_archive(path: &std::path::Path, members: &[(&str, &[u8])]) { + let file = std::fs::File::create(path).expect("create archive"); + let mut writer = zip::ZipWriter::new(file); + for (name, bytes) in members { + writer + .start_file(*name, zip::write::SimpleFileOptions::default()) + .expect("start entry"); + writer.write_all(bytes).expect("write entry"); + } + writer.finish().expect("finish archive"); + } - log::info!( - "ONNX Runtime v{} DLL installed at {}", - ORT_VERSION, - dll_path.display() - ); - std::env::set_var("ORT_DYLIB_PATH", &dll_path); + #[test] + fn extracts_the_requested_member_only() { + let dir = tempfile::tempdir().expect("tempdir"); + let zip_path = dir.path().join("pkg.nupkg"); + write_archive( + &zip_path, + &[ + ("runtimes/win-x64/native/onnxruntime.dll", b"ORT-DLL-BYTES"), + ("other.txt", b"ignore"), + ], + ); - Ok(()) + let out = dir.path().join("onnxruntime.dll"); + extract_member_from_zip(&zip_path, "runtimes/win-x64/native/onnxruntime.dll", &out) + .expect("extract member"); + + assert_eq!( + std::fs::read(&out).expect("read extracted file"), + b"ORT-DLL-BYTES" + ); + } + + #[test] + fn missing_member_is_an_error() { + let dir = tempfile::tempdir().expect("tempdir"); + let zip_path = dir.path().join("pkg.nupkg"); + write_archive(&zip_path, &[("other.txt", b"x")]); + + let err = extract_member_from_zip( + &zip_path, + "runtimes/win-x64/native/onnxruntime.dll", + &dir.path().join("out.dll"), + ) + .expect_err("missing member must fail"); + + assert!( + format!("{err}").contains("not found in zip"), + "unexpected error: {err}" + ); + } } #[cfg(test)]