Fastest and only macOS Dictation app with on-device STT and custom trained AI enhancement model. Windows, iOS testflight! Android, Linux waitlist open! A local Wispr Flow alternative. Follow https://x.com/fluidvoiceapp for more
fix: re-validate cached model files so corrupt HTML payloads are re-downloaded (#353)
postosof41bb5062a73
diff --git a/Sources/Fluid/Networking/ModelDownloader.swift b/Sources/Fluid/Networking/ModelDownloader.swiftindex cfd32f05..125fe6ab 100644--- a/Sources/Fluid/Networking/ModelDownloader.swift+++ b/Sources/Fluid/Networking/ModelDownloader.swift@@ -104,13 +104,13 @@ final class HuggingFaceModelDownloader {let files = try await listFilesRecursively(relativePath: item.path)for rel in files {let dest = targetRoot.appendingPathComponent(rel)- if FileManager.default.fileExists(atPath: dest.path) == false {+ if self.needsDownload(relativePath: rel, at: dest) {pendingFiles.append(rel)}}} else {let dest = targetRoot.appendingPathComponent(item.path)- if FileManager.default.fileExists(atPath: dest.path) == false {+ if self.needsDownload(relativePath: item.path, at: dest) {pendingFiles.append(item.path)}}@@ -170,6 +170,36 @@ final class HuggingFaceModelDownloader {self.requiredItemsList}+ /// Decides whether `relativePath` needs to be (re)downloaded into `destination`.+ ///+ /// A file is pending when it is missing, OR when it is present but its cached content+ /// looks like an HTML/markup payload — a corrupt artifact cached before download-time+ /// content validation existed (see #353). `fileExists` alone would leave such a payload+ /// stuck forever, because `downloadFile` (and its validator) only runs for pending files.+ /// A present markup file is deleted here so a clean copy is fetched; on a read error the+ /// file is left in place and treated as valid, so we never delete on uncertainty.+ private func needsDownload(relativePath: String, at destination: URL) -> Bool {+ guard FileManager.default.fileExists(atPath: destination.path) else {+ return true+ }+ guard Self.cachedFileIsMarkup(at: destination) else {+ return false+ }+ DebugLogger.shared.warning(+ "[ModelDL] Cached file is an HTML/markup page, not model data; deleting to re-download: \(relativePath)",+ source: "ModelDownloader"+ )+ do {+ try FileManager.default.removeItem(at: destination)+ } catch {+ DebugLogger.shared.error(+ "[ModelDL] Failed to delete corrupt cached file \(relativePath): \(error.localizedDescription)",+ source: "ModelDownloader"+ )+ }+ return true+ }+private func downloadDirectory(relativePath: String, to destination: URL) async throws {try FileManager.default.createDirectory(at: destination, withIntermediateDirectories: true)@@ -253,6 +283,23 @@ final class HuggingFaceModelDownloader {}}+ /// Returns `true` if a file already on disk is an HTML/markup payload rather than real+ /// model data — a corrupt artifact cached before download-time validation existed (#353).+ ///+ /// This is the cached-file analog of `validateDownloadedFile`'s byte-sniff: it reuses the+ /// same `looksLikeHTML` check on a small leading prefix (model files can be gigabytes, so+ /// only 512 bytes are read). There is no `URLResponse` for a cached file, so only the+ /// content is inspected, not a `Content-Type`. Returns `false` (treat as valid) on any+ /// read error, so an unreadable file is never deleted on uncertainty.+ static func cachedFileIsMarkup(at fileURL: URL) -> Bool {+ guard let handle = try? FileHandle(forReadingFrom: fileURL) else {+ return false+ }+ defer { try? handle.close() }+ let prefix = (try? handle.read(upToCount: 512)) ?? Data()+ return Self.looksLikeHTML(prefix)+ }+/// Returns `true` if `data` begins with an HTML / XML markup marker, ignoring a leading/// UTF-8 BOM and ASCII whitespace.///diff --git a/Tests/FluidDictationIntegrationTests/DictationE2ETests.swift b/Tests/FluidDictationIntegrationTests/DictationE2ETests.swiftindex 818c1c1e..8f001ed6 100644--- a/Tests/FluidDictationIntegrationTests/DictationE2ETests.swift+++ b/Tests/FluidDictationIntegrationTests/DictationE2ETests.swift@@ -609,6 +609,31 @@ final class DictationE2ETests: XCTestCase {)}+ func testCachedFileIsMarkup_detectsCachedCorruptHTMLAndAcceptsModelData() throws {+ // Guards the #353 cached-file path: a corrupt HTML payload already on disk (cached+ // before download-time validation existed) must be detected so it is re-downloaded,+ // while a real model artifact must not be flagged, and an unreadable path must be+ // treated as valid (never deleted on uncertainty).+ let dir = FileManager.default.temporaryDirectory+ .appendingPathComponent("FluidVoice-CachedMarkupTest-\(UUID().uuidString)", isDirectory: true)+ try FileManager.default.createDirectory(at: dir, withIntermediateDirectories: true)+ defer { try? FileManager.default.removeItem(at: dir) }++ // A cached HTML/proxy page persisted as a model file must be detected as markup.+ let htmlURL = dir.appendingPathComponent("coremldata.bin")+ try Data("<!DOCTYPE html><html><body>Blocked by proxy</body></html>".utf8).write(to: htmlURL)+ XCTAssertTrue(HuggingFaceModelDownloader.cachedFileIsMarkup(at: htmlURL))++ // A real JSON vocab payload must not be flagged.+ let jsonURL = dir.appendingPathComponent("parakeet_v3_vocab.json")+ try Data("{\"0\": \"<pad>\", \"1\": \"the\"}".utf8).write(to: jsonURL)+ XCTAssertFalse(HuggingFaceModelDownloader.cachedFileIsMarkup(at: jsonURL))++ // An unreadable / missing path must be treated as valid (conservative on read error).+ let missingURL = dir.appendingPathComponent("does-not-exist.bin")+ XCTAssertFalse(HuggingFaceModelDownloader.cachedFileIsMarkup(at: missingURL))+ }+private static func modelDirectoryForRun() -> URL {// Use a stable path on CI so GitHub Actions cache can speed up runs.if ProcessInfo.processInfo.environment["GITHUB_ACTIONS"] == "true" ||