From 66c2fb7f1da866844bca4540b6960478ac0f519d Mon Sep 17 00:00:00 2001 From: Jhin Lee Date: Thu, 15 Oct 2026 04:55:31 -0400 Subject: [PATCH 1/3] fix: only ask to upgrade a macOS-complete LiteRT-LM companion Companion 0.0.15 links the macOS-complete manifest but pins v0.18.0, so the hook warned that LiteRT-LM stays in the hook cache and needs the build phase, although the app loads the companion's runtime from its bundle. Check the manifest template before the pin and, when only the pin differs, warn just to upgrade the companion. Fixes #1064. --- CHANGELOG.md | 8 ++ MIGRATION.md | 9 +- hook/build.dart | 71 ++++++---- ...art_litert_lm_flutter_0.0.15_Package.swift | 127 ++++++++++++++++++ ...build_hook_litert_lm_integration_test.dart | 83 ++++++++++-- website/docs/getting-started/installation.md | 5 +- 6 files changed, 263 insertions(+), 40 deletions(-) create mode 100644 test/fixtures/litert_lm/llamadart_litert_lm_flutter_0.0.15_Package.swift diff --git a/CHANGELOG.md b/CHANGELOG.md index 4adfb52c0..cab60b415 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,11 @@ +## Unreleased + +- With `llamadart_litert_lm_flutter` `0.0.15`, the Flutter macOS build now + only warns to upgrade the companion to `0.0.16`, instead of claiming that + LiteRT-LM stays in the hook cache and needs the build phase; the app already + loads the companion's runtime from its bundle + ([#1064](https://github.com/leehack/llamadart/issues/1064)). + ## 0.12.2 - Pair this release with Apple companions `llamadart_llama_cpp_flutter` diff --git a/MIGRATION.md b/MIGRATION.md index e637d8ad6..93910eb99 100644 --- a/MIGRATION.md +++ b/MIGRATION.md @@ -17,10 +17,11 @@ Core `0.12.2` pins llama.cpp `v0.6.0-3` and LiteRT-LM `v0.18.0-1`. llama.cpp companion `0.0.22` still builds, with a warning: its `v0.6.0-2` frameworks lack the minimum OS version keys that App Store validation checks, so upgrade before shipping. Any other mismatched llama.cpp companion fails the build with an error naming -`0.0.23`. With LiteRT-LM companion `0.0.15`, the macOS build warns that -LiteRT-LM stays in the hook cache, but the app still loads its arm64 runtime -from the bundle ([#1064](https://github.com/leehack/llamadart/issues/1064)); -iOS does not check the LiteRT-LM pin. `llamadart_stable_diffusion_flutter` +`0.0.23`. With LiteRT-LM companion `0.0.15`, the app still loads its arm64 +runtime from the bundle; core `0.12.2` wrongly warns that LiteRT-LM stays in +the hook cache, and core `0.12.3` only warns to upgrade to `0.0.16` +([#1064](https://github.com/leehack/llamadart/issues/1064)). iOS does not +check the LiteRT-LM pin. `llamadart_stable_diffusion_flutter` `0.0.3` is unchanged. ## `0.12.0` -> `0.12.1`: Flutter macOS LiteRT-LM bundle diff --git a/hook/build.dart b/hook/build.dart index e0633690f..62bd696c7 100644 --- a/hook/build.dart +++ b/hook/build.dart @@ -547,7 +547,7 @@ Future?> _emitAppleSpmAssetsIfEnabled({ required bool includeLlamaCpp, required bool includeLiteRtLm, required List? appleSpmRuntimes, - required String? liteRtLmMacOsCompanionGap, + required _LiteRtLmMacOsCompanionGap? liteRtLmMacOsCompanionGap, required Object? rawNativeRuntimeConfig, required bool hasNativeSourceOverride, required bool hasNativeBackendOverride, @@ -601,12 +601,17 @@ Future?> _emitAppleSpmAssetsIfEnabled({ } if (includeLiteRtLm) { if (code.targetOS == OS.macOS && liteRtLmMacOsCompanionGap != null) { - final fallback = - 'The resolved $_liteRtLmFlutterPackageName does not link the ' - 'complete macOS LiteRT-LM runtime ($liteRtLmMacOsCompanionGap), so ' - 'LiteRT-LM stays in the hook cache, which a sandboxed or relocated ' - 'app cannot load. Upgrade $_liteRtLmFlutterPackageName together ' - 'with $_packageName.'; + final reason = liteRtLmMacOsCompanionGap.reason; + final fallback = liteRtLmMacOsCompanionGap.embedsRuntime + ? 'The resolved $_liteRtLmFlutterPackageName $reason. It still ' + 'embeds its complete macOS LiteRT-LM runtime, which the app ' + 'loads from its bundle. Upgrade $_liteRtLmFlutterPackageName ' + 'to the release pinning $liteRtLmReleaseTag.' + : 'The resolved $_liteRtLmFlutterPackageName does not link the ' + 'complete macOS LiteRT-LM runtime ($reason), so LiteRT-LM ' + 'stays in the hook cache, which a sandboxed or relocated app ' + 'cannot load. Upgrade $_liteRtLmFlutterPackageName together ' + 'with $_packageName.'; log.warning(fallback); stderr.writeln('warning: llamadart: $fallback'); } else { @@ -632,12 +637,12 @@ bool _isAppleTarget(OS os) => os == OS.iOS || os == OS.macOS; /// companion is a dependency and those families come from the hook. /// [stableDiffusion] is decided on its own, so adding only the /// stable_diffusion companion never moves llama_cpp or litert_lm off the hook. -/// [liteRtLmMacOsGap] says why a macOS LiteRT-LM companion cannot supply the -/// runtime, or is `null` when it can or the build is not macOS LiteRT-LM. +/// [liteRtLmMacOsGap] says why a macOS LiteRT-LM companion does not match this +/// core, or is `null` when it does or the build is not macOS LiteRT-LM. typedef _AppleCompanions = ({ List? runtimes, bool stableDiffusion, - String? liteRtLmMacOsGap, + _LiteRtLmMacOsCompanionGap? liteRtLmMacOsGap, }); /// The companions of a Flutter iOS/macOS app build, or `null` for every other @@ -689,7 +694,7 @@ _AppleCompanions? _flutterAppleCompanions({ final dependencies = _pubspecDependencyNames(pubspecSource); final runtimes = []; - String? liteRtLmMacOsGap; + _LiteRtLmMacOsCompanionGap? liteRtLmMacOsGap; if (dependencies.contains(_llamaCppFlutterPackageName)) { _validateAppleCompanion(_llamaCppCompanion, consumerRoot, output, log); runtimes.add(nativeRuntimeLlamaCpp); @@ -697,7 +702,7 @@ _AppleCompanions? _flutterAppleCompanions({ if (dependencies.contains(_liteRtLmFlutterPackageName)) { runtimes.add(nativeRuntimeLiteRtLm); if (code.targetOS == OS.macOS) { - liteRtLmMacOsGap = _liteRtLmMacOsCompanionGap(consumerRoot, output); + liteRtLmMacOsGap = _liteRtLmMacOsCompanionGapFor(consumerRoot, output); } } final stableDiffusion = dependencies.contains( @@ -932,10 +937,16 @@ Directory _resolvedCompanionRoot( return Directory.fromUri(uri); } -/// Why the resolved LiteRT-LM companion cannot supply the macOS runtime -/// through SwiftPM, or `null` when its manifest is the maintained one pinning +/// Why the resolved LiteRT-LM companion does not match this core on macOS. +/// [embedsRuntime] is true when only its pin differs: its manifest is the +/// macOS-complete one, so the app still embeds and loads a complete runtime +/// from its bundle (companion 0.0.15 pins the same upstream release). +typedef _LiteRtLmMacOsCompanionGap = ({String reason, bool embedsRuntime}); + +/// The gap between the resolved LiteRT-LM companion and this core on macOS, +/// or `null` when its manifest is the maintained one pinning /// [liteRtLmReleaseTag]. -String? _liteRtLmMacOsCompanionGap( +_LiteRtLmMacOsCompanionGap? _liteRtLmMacOsCompanionGapFor( Directory consumerRoot, BuildOutputBuilder output, ) { @@ -959,18 +970,16 @@ String? _liteRtLmMacOsCompanionGap( output.dependencies.add(manifest.uri); if (!manifest.existsSync()) gap('Package.swift is missing'); final source = manifest.readAsStringSync().replaceAll('\r\n', '\n'); - final pins = RegExp( + final pinPattern = RegExp( r'^let liteRtLmTag = "([^"\n]+)"$', multiLine: true, - ).allMatches(source).toList(); - if (pins.length != 1 || pins.single.group(1) != liteRtLmReleaseTag) { + ); + final pins = pinPattern.allMatches(source).toList(); + if (pins.length != 1) { gap('it does not pin leehack/litert-lm-native@$liteRtLmReleaseTag'); } final template = source - .replaceFirst( - RegExp(r'^let liteRtLmTag = "[^"\n]+"$', multiLine: true), - 'let liteRtLmTag = "PIN"', - ) + .replaceFirst(pinPattern, 'let liteRtLmTag = "PIN"') .replaceAll( RegExp(r'checksum: "[0-9a-f]{64}"'), 'checksum: "CHECKSUM"', @@ -979,13 +988,25 @@ String? _liteRtLmMacOsCompanionGap( _liteRtLmCompanionManifestTemplateSha256) { gap('its Package.swift is not the macOS-complete manifest'); } + final pin = pins.single.group(1); + if (pin != liteRtLmReleaseTag) { + return ( + reason: + 'pins leehack/litert-lm-native@$pin rather than this core\'s ' + '$liteRtLmReleaseTag', + embedsRuntime: true, + ); + } return null; } on _CompanionGap catch (error) { - return error.reason; + return (reason: error.reason, embedsRuntime: false); } on FileSystemException { - return 'its metadata could not be read'; + return (reason: 'its metadata could not be read', embedsRuntime: false); } on FormatException { - return 'the resolved package configuration is malformed'; + return ( + reason: 'the resolved package configuration is malformed', + embedsRuntime: false, + ); } } diff --git a/test/fixtures/litert_lm/llamadart_litert_lm_flutter_0.0.15_Package.swift b/test/fixtures/litert_lm/llamadart_litert_lm_flutter_0.0.15_Package.swift new file mode 100644 index 000000000..3320967b9 --- /dev/null +++ b/test/fixtures/litert_lm/llamadart_litert_lm_flutter_0.0.15_Package.swift @@ -0,0 +1,127 @@ +// swift-tools-version: 5.9 +import Foundation +import PackageDescription + +let packageRoot = URL(fileURLWithPath: #filePath).deletingLastPathComponent() +let artifactsRoot = packageRoot.appendingPathComponent("Artifacts") +let liteRtLmTag = "v0.18.0" + +func localArtifactPath(_ name: String) -> String? { + let path = artifactsRoot.appendingPathComponent(name).path + return FileManager.default.fileExists(atPath: path) ? "Artifacts/\(name)" : nil +} + +func nativeRepoBinaryTarget( + name: String, + repository: String, + artifactName: String, + tag: String, + checksum: String +) -> Target { + if let path = localArtifactPath("\(name).xcframework") { + return .binaryTarget(name: name, path: path) + } + return .binaryTarget( + name: name, + url: "https://github.com/\(repository)/releases/download/\(tag)/\(artifactName)", + checksum: checksum + ) +} + +let package = Package( + name: "llamadart_litert_lm_flutter", + platforms: [ + .iOS("16.4"), + .macOS("14.0") + ], + products: [ + .library( + name: "llamadart-litert-lm-flutter", + type: .dynamic, + targets: ["llamadart_litert_lm_flutter"] + ) + ], + targets: [ + nativeRepoBinaryTarget( + name: "LiteRtLm", + repository: "leehack/litert-lm-native", + artifactName: "litert-lm-native-apple-LiteRtLm-xcframework-\(liteRtLmTag).zip", + tag: liteRtLmTag, + checksum: "232c864fa72391b99de3251296771b0bd0f9456a4563088e1803323a2a67a328" + ), + nativeRepoBinaryTarget( + name: "CLiteRTLM", + repository: "leehack/litert-lm-native", + artifactName: "litert-lm-native-apple-CLiteRTLM-xcframework-\(liteRtLmTag).zip", + tag: liteRtLmTag, + checksum: "e73a08a5208fe6e26b1e4b38a3a67d07b2a9839a6c3df57b10b2f6d7ed513747" + ), + nativeRepoBinaryTarget( + name: "CLiteRTLMMac", + repository: "leehack/litert-lm-native", + artifactName: "litert-lm-native-apple-CLiteRTLMMac-xcframework-\(liteRtLmTag).zip", + tag: liteRtLmTag, + checksum: "9d122c2ddc776bd3b262849afa75c656daa200e3431efb829f3778b0cd185eac" + ), + nativeRepoBinaryTarget( + name: "LiteRtMetalAccelerator", + repository: "leehack/litert-lm-native", + artifactName: "litert-lm-native-apple-LiteRtMetalAccelerator-xcframework-\(liteRtLmTag).zip", + tag: liteRtLmTag, + checksum: "279a1dd0cb88c777093fc99dc03227d44390ea89aeb98dbf22876ae694941580" + ), + nativeRepoBinaryTarget( + name: "LiteRtTopKMetalSampler", + repository: "leehack/litert-lm-native", + artifactName: "litert-lm-native-apple-LiteRtTopKMetalSampler-xcframework-\(liteRtLmTag).zip", + tag: liteRtLmTag, + checksum: "ca9a079c3d91c41923e529c68aebfebb540cb65b2100787b035d433bcead7ce2" + ), + nativeRepoBinaryTarget( + name: "LiteRt", + repository: "leehack/litert-lm-native", + artifactName: "litert-lm-native-apple-LiteRt-xcframework-\(liteRtLmTag).zip", + tag: liteRtLmTag, + checksum: "87d90748f50ae6048fc7fc5b0de6f14470baef036258180cf52d68f1e031dbaf" + ), + nativeRepoBinaryTarget( + name: "LiteRtTopKWebGpuSampler", + repository: "leehack/litert-lm-native", + artifactName: "litert-lm-native-apple-LiteRtTopKWebGpuSampler-xcframework-\(liteRtLmTag).zip", + tag: liteRtLmTag, + checksum: "4370a377fc5d5b5e7f5d8b106add4df876e99a6c5c052578e2e48c9bd69e1b52" + ), + nativeRepoBinaryTarget( + name: "LiteRtWebGpuAccelerator", + repository: "leehack/litert-lm-native", + artifactName: "litert-lm-native-apple-LiteRtWebGpuAccelerator-xcframework-\(liteRtLmTag).zip", + tag: liteRtLmTag, + checksum: "8c5fbd141736f9ab6f6937a4481ddca6cc1259dbd3441cc12e0cf4de91ccf2a8" + ), + nativeRepoBinaryTarget( + name: "WebgpuDawn", + repository: "leehack/litert-lm-native", + artifactName: "litert-lm-native-apple-WebgpuDawn-xcframework-\(liteRtLmTag).zip", + tag: liteRtLmTag, + checksum: "067cdfdcda01005ffff596f45f28c4093e59920a8f254219563f0462c18e2e3f" + ), + .target( + name: "llamadart_litert_lm_flutter", + dependencies: [ + .target(name: "LiteRtLm", condition: .when(platforms: [.iOS, .macOS])), + .target(name: "CLiteRTLM", condition: .when(platforms: [.iOS])), + .target(name: "CLiteRTLMMac", condition: .when(platforms: [.macOS])), + .target(name: "LiteRtMetalAccelerator", condition: .when(platforms: [.iOS])), + .target(name: "LiteRtTopKMetalSampler", condition: .when(platforms: [.iOS])), + .target(name: "LiteRt", condition: .when(platforms: [.macOS])), + .target(name: "LiteRtTopKWebGpuSampler", condition: .when(platforms: [.macOS])), + .target(name: "LiteRtWebGpuAccelerator", condition: .when(platforms: [.macOS])), + .target(name: "WebgpuDawn", condition: .when(platforms: [.macOS])) + ], + linkerSettings: [ + .unsafeFlags(["-Xlinker", "-reexport_framework", "-Xlinker", "LiteRtLm"], .when(platforms: [.iOS])), + .unsafeFlags(["-Xlinker", "-reexport_framework", "-Xlinker", "CLiteRTLM"], .when(platforms: [.iOS])) + ] + ) + ] +) diff --git a/test/unit/hook/build_hook_litert_lm_integration_test.dart b/test/unit/hook/build_hook_litert_lm_integration_test.dart index 912208e0c..8c0e7290f 100644 --- a/test/unit/hook/build_hook_litert_lm_integration_test.dart +++ b/test/unit/hook/build_hook_litert_lm_integration_test.dart @@ -856,15 +856,6 @@ dependency_overrides: {llamadart_llama_cpp_flutter: {path: resolved companion}} liteRtLmManifest: _publishedLiteRtLmCompanion0014Manifest(), ), ), - ( - 'a manifest pinning another LiteRT-LM tag', - () => _flutterLiteRtLmOnlyUserDefines( - liteRtLmManifest: _liteRtLmCompanionManifest().replaceFirst( - 'let liteRtLmTag = "${_readPinnedConst('liteRtLmReleaseTag')}"', - 'let liteRtLmTag = "v0.17.0-8"', - ), - ), - ), ( 'an edited manifest', () => _flutterLiteRtLmOnlyUserDefines( @@ -907,10 +898,78 @@ dependency_overrides: {llamadart_llama_cpp_flutter: {path: resolved companion}} ), ), ); + expect(stderrLines, isNot(contains(contains('embeds its complete')))); }); } } + for (final (scenario, manifest, pin) in [ + ( + 'the published 0.0.15 manifest', + _publishedLiteRtLmCompanion0015Manifest, + 'v0.18.0', + ), + ( + 'a macOS-complete manifest pinning another LiteRT-LM tag', + () => _liteRtLmCompanionManifest().replaceFirst( + 'let liteRtLmTag = "${_readPinnedConst('liteRtLmReleaseTag')}"', + 'let liteRtLmTag = "v0.17.0-8"', + ), + 'v0.17.0-8', + ), + ]) { + test('build hook only asks to upgrade a Flutter macOS LiteRT-LM companion ' + 'that embeds the complete runtime for $scenario', () async { + final coreTag = _readPinnedConst('liteRtLmReleaseTag'); + final stderrLines = await _capturingStderr( + () async => testCodeBuildHook( + mainMethod: build_hook.main, + targetOS: OS.macOS, + targetArchitecture: Architecture.arm64, + userDefines: await _flutterLiteRtLmOnlyUserDefines( + liteRtLmManifest: manifest(), + ), + check: (_, _) {}, + ), + ); + final warnings = stderrLines + .where((line) => line.startsWith('warning: llamadart:')) + .toList(growable: false); + expect(warnings, [ + 'warning: llamadart: The resolved llamadart_litert_lm_flutter pins ' + 'leehack/litert-lm-native@$pin rather than this core\'s $coreTag. ' + 'It still embeds its complete macOS LiteRT-LM runtime, which the ' + 'app loads from its bundle. Upgrade llamadart_litert_lm_flutter ' + 'to the release pinning $coreTag.', + ]); + }); + } + + test('build hook prints no companion warning for a Flutter macOS app ' + 'without the LiteRT-LM companion', () async { + final stderrLines = await _capturingStderr( + () async => testCodeBuildHook( + mainMethod: build_hook.main, + targetOS: OS.macOS, + targetArchitecture: Architecture.arm64, + userDefines: await _flutterAppleUserDefines( + dependencies: const [], + defines: { + 'llamadart_native_runtimes': ['litert_lm'], + }, + ), + check: (input, output) { + final outputDir = input.outputDirectory.toFilePath(); + expect( + Directory(path.join(outputDir, 'llamadart_bin')).existsSync(), + isTrue, + ); + }, + ), + ); + expect(stderrLines, isNot(contains(contains('warning: llamadart:')))); + }); + for (final (scenario, dependencies, expectedAssetIds) in [ ('the LiteRT-LM companion', const ['llamadart_litert_lm_flutter'], []), ( @@ -1402,6 +1461,12 @@ String _publishedLiteRtLmCompanion0014Manifest() => File( 'test/fixtures/litert_lm/llamadart_litert_lm_flutter_0.0.14_Package.swift', ).readAsStringSync(); +/// The manifest published as companion 0.0.15: the macOS-complete manifest +/// pinning v0.18.0 rather than this core's rebuild of the same release. +String _publishedLiteRtLmCompanion0015Manifest() => File( + 'test/fixtures/litert_lm/llamadart_litert_lm_flutter_0.0.15_Package.swift', +).readAsStringSync(); + Future _nonFlutterConsumerUserDefines({ required List dependencies, Map defines = const {}, diff --git a/website/docs/getting-started/installation.md b/website/docs/getting-started/installation.md index a92321a90..ab559b71b 100644 --- a/website/docs/getting-started/installation.md +++ b/website/docs/getting-started/installation.md @@ -71,8 +71,9 @@ companion's `Package.swift` is the one this core release expects; any other companion keeps macOS LiteRT-LM in the hook cache with a build warning, and the app then needs the [build phase below](#flutter-macos-litert-lm-build-phase). Companion `0.0.15` is the exception: it pins `v0.18.0` rather than this core's -`v0.18.0-1`, so the build prints that warning, but the app still embeds and -loads the companion's complete macOS arm64 runtime and needs no build phase. +`v0.18.0-1`, but the app still embeds and loads the companion's complete macOS +arm64 runtime and needs no build phase, so the build only warns to upgrade to +`0.0.16`. On iOS the build does not check the LiteRT-LM companion pin. `v0.18.0-1` rebuilds the same LiteRT-LM release with arm64-only macOS XCFrameworks. From 690996229fff80904c41696153daed593a7cbf8f Mon Sep 17 00:00:00 2001 From: Jhin Lee Date: Thu, 15 Oct 2026 05:10:39 -0400 Subject: [PATCH 2/3] fix: limit the LiteRT-LM companion upgrade warning to earlier same-release builds --- CHANGELOG.md | 8 +- MIGRATION.md | 9 +- hook/build.dart | 110 ++++++++++-------- ...build_hook_litert_lm_integration_test.dart | 81 ++++++++++--- website/docs/getting-started/installation.md | 4 +- website/docs/platforms/native-build-hooks.md | 7 +- 6 files changed, 143 insertions(+), 76 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index cab60b415..209dc52a4 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,10 +1,10 @@ ## Unreleased - With `llamadart_litert_lm_flutter` `0.0.15`, the Flutter macOS build now - only warns to upgrade the companion to `0.0.16`, instead of claiming that - LiteRT-LM stays in the hook cache and needs the build phase; the app already - loads the companion's runtime from its bundle - ([#1064](https://github.com/leehack/llamadart/issues/1064)). + only warns to upgrade the companion to `0.0.16`, the release pinning + `v0.18.0-1`, instead of claiming that LiteRT-LM stays in the hook cache and + needs the build phase; the app already loads the companion's runtime from its + bundle ([#1064](https://github.com/leehack/llamadart/issues/1064)). ## 0.12.2 diff --git a/MIGRATION.md b/MIGRATION.md index 93910eb99..e637d8ad6 100644 --- a/MIGRATION.md +++ b/MIGRATION.md @@ -17,11 +17,10 @@ Core `0.12.2` pins llama.cpp `v0.6.0-3` and LiteRT-LM `v0.18.0-1`. llama.cpp companion `0.0.22` still builds, with a warning: its `v0.6.0-2` frameworks lack the minimum OS version keys that App Store validation checks, so upgrade before shipping. Any other mismatched llama.cpp companion fails the build with an error naming -`0.0.23`. With LiteRT-LM companion `0.0.15`, the app still loads its arm64 -runtime from the bundle; core `0.12.2` wrongly warns that LiteRT-LM stays in -the hook cache, and core `0.12.3` only warns to upgrade to `0.0.16` -([#1064](https://github.com/leehack/llamadart/issues/1064)). iOS does not -check the LiteRT-LM pin. `llamadart_stable_diffusion_flutter` +`0.0.23`. With LiteRT-LM companion `0.0.15`, the macOS build warns that +LiteRT-LM stays in the hook cache, but the app still loads its arm64 runtime +from the bundle ([#1064](https://github.com/leehack/llamadart/issues/1064)); +iOS does not check the LiteRT-LM pin. `llamadart_stable_diffusion_flutter` `0.0.3` is unchanged. ## `0.12.0` -> `0.12.1`: Flutter macOS LiteRT-LM bundle diff --git a/hook/build.dart b/hook/build.dart index 62bd696c7..bb0835209 100644 --- a/hook/build.dart +++ b/hook/build.dart @@ -185,7 +185,7 @@ void main(List args) async { ); final appleSpmRuntimes = appleCompanions?.runtimes; final stableDiffusionCompanion = appleCompanions?.stableDiffusion ?? false; - final liteRtLmMacOsCompanionGap = appleCompanions?.liteRtLmMacOsGap; + final liteRtLmMacOsCompanionWarning = appleCompanions?.liteRtLmMacOsWarning; final configuredRuntimes = selectNativeRuntimesForBundle( bundle: spec.bundle, rawUserConfig: rawNativeRuntimeConfig, @@ -346,7 +346,7 @@ void main(List args) async { includeLlamaCpp: selectedRuntimes.contains(nativeRuntimeLlamaCpp), includeLiteRtLm: selectedRuntimes.contains(nativeRuntimeLiteRtLm), appleSpmRuntimes: appleSpmRuntimes, - liteRtLmMacOsCompanionGap: liteRtLmMacOsCompanionGap, + liteRtLmMacOsCompanionWarning: liteRtLmMacOsCompanionWarning, rawNativeRuntimeConfig: rawNativeRuntimeConfig, hasNativeSourceOverride: _hasNativeSourceOverride(input.userDefines), hasNativeBackendOverride: @@ -547,7 +547,7 @@ Future?> _emitAppleSpmAssetsIfEnabled({ required bool includeLlamaCpp, required bool includeLiteRtLm, required List? appleSpmRuntimes, - required _LiteRtLmMacOsCompanionGap? liteRtLmMacOsCompanionGap, + required String? liteRtLmMacOsCompanionWarning, required Object? rawNativeRuntimeConfig, required bool hasNativeSourceOverride, required bool hasNativeBackendOverride, @@ -600,20 +600,9 @@ Future?> _emitAppleSpmAssetsIfEnabled({ handledRuntimes.add(nativeRuntimeLlamaCpp); } if (includeLiteRtLm) { - if (code.targetOS == OS.macOS && liteRtLmMacOsCompanionGap != null) { - final reason = liteRtLmMacOsCompanionGap.reason; - final fallback = liteRtLmMacOsCompanionGap.embedsRuntime - ? 'The resolved $_liteRtLmFlutterPackageName $reason. It still ' - 'embeds its complete macOS LiteRT-LM runtime, which the app ' - 'loads from its bundle. Upgrade $_liteRtLmFlutterPackageName ' - 'to the release pinning $liteRtLmReleaseTag.' - : 'The resolved $_liteRtLmFlutterPackageName does not link the ' - 'complete macOS LiteRT-LM runtime ($reason), so LiteRT-LM ' - 'stays in the hook cache, which a sandboxed or relocated app ' - 'cannot load. Upgrade $_liteRtLmFlutterPackageName together ' - 'with $_packageName.'; - log.warning(fallback); - stderr.writeln('warning: llamadart: $fallback'); + if (code.targetOS == OS.macOS && liteRtLmMacOsCompanionWarning != null) { + log.warning(liteRtLmMacOsCompanionWarning); + stderr.writeln('warning: llamadart: $liteRtLmMacOsCompanionWarning'); } else { log.info( 'LiteRT-LM will resolve symbols from the SPM-linked process image.', @@ -637,12 +626,12 @@ bool _isAppleTarget(OS os) => os == OS.iOS || os == OS.macOS; /// companion is a dependency and those families come from the hook. /// [stableDiffusion] is decided on its own, so adding only the /// stable_diffusion companion never moves llama_cpp or litert_lm off the hook. -/// [liteRtLmMacOsGap] says why a macOS LiteRT-LM companion does not match this -/// core, or is `null` when it does or the build is not macOS LiteRT-LM. +/// [liteRtLmMacOsWarning] says how a macOS LiteRT-LM companion does not match +/// this core, or is `null` when it does or the build is not macOS LiteRT-LM. typedef _AppleCompanions = ({ List? runtimes, bool stableDiffusion, - _LiteRtLmMacOsCompanionGap? liteRtLmMacOsGap, + String? liteRtLmMacOsWarning, }); /// The companions of a Flutter iOS/macOS app build, or `null` for every other @@ -694,7 +683,7 @@ _AppleCompanions? _flutterAppleCompanions({ final dependencies = _pubspecDependencyNames(pubspecSource); final runtimes = []; - _LiteRtLmMacOsCompanionGap? liteRtLmMacOsGap; + String? liteRtLmMacOsWarning; if (dependencies.contains(_llamaCppFlutterPackageName)) { _validateAppleCompanion(_llamaCppCompanion, consumerRoot, output, log); runtimes.add(nativeRuntimeLlamaCpp); @@ -702,7 +691,10 @@ _AppleCompanions? _flutterAppleCompanions({ if (dependencies.contains(_liteRtLmFlutterPackageName)) { runtimes.add(nativeRuntimeLiteRtLm); if (code.targetOS == OS.macOS) { - liteRtLmMacOsGap = _liteRtLmMacOsCompanionGapFor(consumerRoot, output); + liteRtLmMacOsWarning = _liteRtLmMacOsCompanionWarning( + consumerRoot, + output, + ); } } final stableDiffusion = dependencies.contains( @@ -727,7 +719,7 @@ _AppleCompanions? _flutterAppleCompanions({ return ( runtimes: runtimes.isEmpty ? null : runtimes, stableDiffusion: stableDiffusion, - liteRtLmMacOsGap: liteRtLmMacOsGap, + liteRtLmMacOsWarning: liteRtLmMacOsWarning, ); } @@ -937,16 +929,10 @@ Directory _resolvedCompanionRoot( return Directory.fromUri(uri); } -/// Why the resolved LiteRT-LM companion does not match this core on macOS. -/// [embedsRuntime] is true when only its pin differs: its manifest is the -/// macOS-complete one, so the app still embeds and loads a complete runtime -/// from its bundle (companion 0.0.15 pins the same upstream release). -typedef _LiteRtLmMacOsCompanionGap = ({String reason, bool embedsRuntime}); - -/// The gap between the resolved LiteRT-LM companion and this core on macOS, -/// or `null` when its manifest is the maintained one pinning -/// [liteRtLmReleaseTag]. -_LiteRtLmMacOsCompanionGap? _liteRtLmMacOsCompanionGapFor( +/// The build warning for a resolved LiteRT-LM companion that does not match +/// this core on macOS, or `null` when its manifest is the maintained one +/// pinning [liteRtLmReleaseTag]. +String? _liteRtLmMacOsCompanionWarning( Directory consumerRoot, BuildOutputBuilder output, ) { @@ -988,28 +974,56 @@ _LiteRtLmMacOsCompanionGap? _liteRtLmMacOsCompanionGapFor( _liteRtLmCompanionManifestTemplateSha256) { gap('its Package.swift is not the macOS-complete manifest'); } - final pin = pins.single.group(1); - if (pin != liteRtLmReleaseTag) { - return ( - reason: - 'pins leehack/litert-lm-native@$pin rather than this core\'s ' - '$liteRtLmReleaseTag', - embedsRuntime: true, - ); - } - return null; + final pin = pins.single.group(1)!; + if (pin == liteRtLmReleaseTag) return null; + if (_isEarlierLiteRtLmRebuild(pin)) { + return 'The resolved $_liteRtLmFlutterPackageName pins ' + 'leehack/litert-lm-native@$pin, an earlier build of this core\'s ' + 'LiteRT-LM release $liteRtLmReleaseTag. It still embeds its ' + 'complete macOS LiteRT-LM runtime, which the app loads from its ' + 'bundle. Upgrade $_liteRtLmFlutterPackageName to the release ' + 'pinning $liteRtLmReleaseTag.'; + } + return 'The resolved $_liteRtLmFlutterPackageName pins ' + 'leehack/litert-lm-native@$pin rather than this core\'s ' + '$liteRtLmReleaseTag, so LiteRT-LM stays in the hook cache. Upgrade ' + '$_liteRtLmFlutterPackageName and $_packageName together to a ' + 'matching released pair.'; } on _CompanionGap catch (error) { - return (reason: error.reason, embedsRuntime: false); + return _liteRtLmIncompleteCompanionWarning(error.reason); } on FileSystemException { - return (reason: 'its metadata could not be read', embedsRuntime: false); + return _liteRtLmIncompleteCompanionWarning( + 'its metadata could not be read', + ); } on FormatException { - return ( - reason: 'the resolved package configuration is malformed', - embedsRuntime: false, + return _liteRtLmIncompleteCompanionWarning( + 'the resolved package configuration is malformed', ); } } +String _liteRtLmIncompleteCompanionWarning(String reason) => + 'The resolved $_liteRtLmFlutterPackageName does not link the complete ' + 'macOS LiteRT-LM runtime ($reason), so LiteRT-LM stays in the hook cache, ' + 'which a sandboxed or relocated app cannot load. Upgrade ' + '$_liteRtLmFlutterPackageName together with $_packageName.'; + +/// Whether [pin] is an earlier build of this core's upstream LiteRT-LM +/// release: the same `vMAJOR.MINOR.PATCH` prefix as [liteRtLmReleaseTag] +/// with a lower wrapper rebuild counter, which a bare `vMAJOR.MINOR.PATCH` +/// has as 0. The native release manifest binds that prefix to one upstream +/// commit, so such a companion embeds the same LiteRT-LM release. +bool _isEarlierLiteRtLmRebuild(String pin) { + final tagPattern = RegExp(r'^(v\d+\.\d+\.\d+)(?:-([1-9]\d*))?$'); + final core = tagPattern.firstMatch(liteRtLmReleaseTag); + final candidate = tagPattern.firstMatch(pin); + if (core == null || candidate == null || candidate[1] != core[1]) { + return false; + } + int rebuild(RegExpMatch tag) => int.parse(tag[2] ?? '0'); + return rebuild(candidate) < rebuild(core); +} + class _CompanionGap implements Exception { _CompanionGap(this.reason); diff --git a/test/unit/hook/build_hook_litert_lm_integration_test.dart b/test/unit/hook/build_hook_litert_lm_integration_test.dart index 8c0e7290f..eff6d9b95 100644 --- a/test/unit/hook/build_hook_litert_lm_integration_test.dart +++ b/test/unit/hook/build_hook_litert_lm_integration_test.dart @@ -903,24 +903,61 @@ dependency_overrides: {llamadart_llama_cpp_flutter: {path: resolved companion}} } } - for (final (scenario, manifest, pin) in [ + final coreLiteRtLmTag = _readPinnedConst('liteRtLmReleaseTag'); + final coreLiteRtLmTagParts = RegExp( + r'^v(\d+)\.(\d+)\.(\d+)(?:-(\d+))?$', + ).firstMatch(coreLiteRtLmTag)!; + final coreUpstream = + 'v${coreLiteRtLmTagParts[1]}.${coreLiteRtLmTagParts[2]}.' + '${coreLiteRtLmTagParts[3]}'; + final coreRebuild = int.parse(coreLiteRtLmTagParts[4] ?? '0'); + final newerUpstream = + 'v${coreLiteRtLmTagParts[1]}.${int.parse(coreLiteRtLmTagParts[2]!) + 1}' + '.0'; + String pinningLiteRtLm(String pin) => + _liteRtLmCompanionManifest().replaceFirst( + 'let liteRtLmTag = "$coreLiteRtLmTag"', + 'let liteRtLmTag = "$pin"', + ); + + for (final (scenario, manifest, pin, upgradeCompanion) in [ ( 'the published 0.0.15 manifest', _publishedLiteRtLmCompanion0015Manifest, 'v0.18.0', + coreUpstream == 'v0.18.0', ), - ( - 'a macOS-complete manifest pinning another LiteRT-LM tag', - () => _liteRtLmCompanionManifest().replaceFirst( - 'let liteRtLmTag = "${_readPinnedConst('liteRtLmReleaseTag')}"', - 'let liteRtLmTag = "v0.17.0-8"', + if (coreRebuild > 0) + ( + 'a macOS-complete manifest pinning the bare upstream release', + () => pinningLiteRtLm(coreUpstream), + coreUpstream, + true, ), + ( + 'a macOS-complete manifest pinning an older LiteRT-LM release', + () => pinningLiteRtLm('v0.17.0-8'), 'v0.17.0-8', + false, + ), + ( + 'a macOS-complete manifest pinning a newer LiteRT-LM release', + () => pinningLiteRtLm(newerUpstream), + newerUpstream, + false, + ), + ( + 'a macOS-complete manifest pinning a newer build of the same release', + () => pinningLiteRtLm('$coreUpstream-${coreRebuild + 1}'), + '$coreUpstream-${coreRebuild + 1}', + false, ), ]) { - test('build hook only asks to upgrade a Flutter macOS LiteRT-LM companion ' - 'that embeds the complete runtime for $scenario', () async { - final coreTag = _readPinnedConst('liteRtLmReleaseTag'); + final advice = upgradeCompanion + ? 'only asks to upgrade the companion' + : 'asks to upgrade core and companion together'; + test('build hook keeps Flutter macOS LiteRT-LM on the hook cache and ' + '$advice for $scenario', () async { final stderrLines = await _capturingStderr( () async => testCodeBuildHook( mainMethod: build_hook.main, @@ -929,18 +966,32 @@ dependency_overrides: {llamadart_llama_cpp_flutter: {path: resolved companion}} userDefines: await _flutterLiteRtLmOnlyUserDefines( liteRtLmManifest: manifest(), ), - check: (_, _) {}, + check: (input, output) { + expect(output.assets.encodedAssets, isEmpty); + final outputDir = input.outputDirectory.toFilePath(); + expect( + Directory(path.join(outputDir, 'llamadart_bin')).existsSync(), + isTrue, + ); + }, ), ); final warnings = stderrLines .where((line) => line.startsWith('warning: llamadart:')) .toList(growable: false); expect(warnings, [ - 'warning: llamadart: The resolved llamadart_litert_lm_flutter pins ' - 'leehack/litert-lm-native@$pin rather than this core\'s $coreTag. ' - 'It still embeds its complete macOS LiteRT-LM runtime, which the ' - 'app loads from its bundle. Upgrade llamadart_litert_lm_flutter ' - 'to the release pinning $coreTag.', + upgradeCompanion + ? 'warning: llamadart: The resolved llamadart_litert_lm_flutter ' + 'pins leehack/litert-lm-native@$pin, an earlier build of ' + 'this core\'s LiteRT-LM release $coreLiteRtLmTag. It still ' + 'embeds its complete macOS LiteRT-LM runtime, which the app ' + 'loads from its bundle. Upgrade llamadart_litert_lm_flutter ' + 'to the release pinning $coreLiteRtLmTag.' + : 'warning: llamadart: The resolved llamadart_litert_lm_flutter ' + 'pins leehack/litert-lm-native@$pin rather than this ' + 'core\'s $coreLiteRtLmTag, so LiteRT-LM stays in the hook ' + 'cache. Upgrade llamadart_litert_lm_flutter and llamadart ' + 'together to a matching released pair.', ]); }); } diff --git a/website/docs/getting-started/installation.md b/website/docs/getting-started/installation.md index ab559b71b..b8fed0c9e 100644 --- a/website/docs/getting-started/installation.md +++ b/website/docs/getting-started/installation.md @@ -72,8 +72,8 @@ companion keeps macOS LiteRT-LM in the hook cache with a build warning, and the app then needs the [build phase below](#flutter-macos-litert-lm-build-phase). Companion `0.0.15` is the exception: it pins `v0.18.0` rather than this core's `v0.18.0-1`, but the app still embeds and loads the companion's complete macOS -arm64 runtime and needs no build phase, so the build only warns to upgrade to -`0.0.16`. +arm64 runtime and needs no build phase, so the build only warns to upgrade the +companion to `0.0.16`, the release pinning `v0.18.0-1`. On iOS the build does not check the LiteRT-LM companion pin. `v0.18.0-1` rebuilds the same LiteRT-LM release with arm64-only macOS XCFrameworks. diff --git a/website/docs/platforms/native-build-hooks.md b/website/docs/platforms/native-build-hooks.md index a4573eebf..3bb5f0ca2 100644 --- a/website/docs/platforms/native-build-hooks.md +++ b/website/docs/platforms/native-build-hooks.md @@ -408,8 +408,11 @@ pinning the core's LiteRT-LM release. Any other companion, including `0.0.14` and earlier, keeps macOS LiteRT-LM in the hook cache with a build warning; a sandboxed, copied or distributed app cannot read that cache and needs the [LiteRT-LM build phase](../getting-started/installation#flutter-macos-litert-lm-build-phase). -Companion `0.0.15` also gets that warning, but it embeds the complete macOS -runtime, which the app loads from its bundle. +A companion with the maintained manifest pinning an earlier build of the same +LiteRT-LM release, such as `0.0.15` (`v0.18.0`), still embeds the complete +macOS runtime, which the app loads from its bundle, so the build only warns to +upgrade the companion to `0.0.16` (`v0.18.0-1`). A maintained manifest pinning +any other LiteRT-LM release warns to upgrade the core and companion together. Without a companion, and for non-Flutter or non-Apple builds, the hook path above applies. From 0a1cb2496363a8ed972007218af4db75f334f588 Mon Sep 17 00:00:00 2001 From: Jhin Lee Date: Thu, 15 Oct 2026 05:22:17 -0400 Subject: [PATCH 3/3] fix: treat malformed LiteRT-LM companion tags as other releases --- CHANGELOG.md | 4 ++-- hook/build.dart | 8 ++++++-- .../build_hook_litert_lm_integration_test.dart | 14 ++++++++++++++ website/docs/platforms/native-build-hooks.md | 3 ++- 4 files changed, 24 insertions(+), 5 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 209dc52a4..56163b19a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,8 +2,8 @@ - With `llamadart_litert_lm_flutter` `0.0.15`, the Flutter macOS build now only warns to upgrade the companion to `0.0.16`, the release pinning - `v0.18.0-1`, instead of claiming that LiteRT-LM stays in the hook cache and - needs the build phase; the app already loads the companion's runtime from its + `v0.18.0-1`, instead of claiming that the app cannot load LiteRT-LM and needs + the build phase; the app already loads the companion's runtime from its bundle ([#1064](https://github.com/leehack/llamadart/issues/1064)). ## 0.12.2 diff --git a/hook/build.dart b/hook/build.dart index bb0835209..9d78db0ab 100644 --- a/hook/build.dart +++ b/hook/build.dart @@ -1020,8 +1020,12 @@ bool _isEarlierLiteRtLmRebuild(String pin) { if (core == null || candidate == null || candidate[1] != core[1]) { return false; } - int rebuild(RegExpMatch tag) => int.parse(tag[2] ?? '0'); - return rebuild(candidate) < rebuild(core); + int? rebuild(RegExpMatch tag) => int.tryParse(tag[2] ?? '0'); + final candidateRebuild = rebuild(candidate); + final coreRebuild = rebuild(core); + return candidateRebuild != null && + coreRebuild != null && + candidateRebuild < coreRebuild; } class _CompanionGap implements Exception { diff --git a/test/unit/hook/build_hook_litert_lm_integration_test.dart b/test/unit/hook/build_hook_litert_lm_integration_test.dart index eff6d9b95..3e1bdbc98 100644 --- a/test/unit/hook/build_hook_litert_lm_integration_test.dart +++ b/test/unit/hook/build_hook_litert_lm_integration_test.dart @@ -952,6 +952,20 @@ dependency_overrides: {llamadart_llama_cpp_flutter: {path: resolved companion}} '$coreUpstream-${coreRebuild + 1}', false, ), + for (final malformed in [ + '$coreUpstream-0', + '$coreUpstream-01', + coreUpstream.substring(0, coreUpstream.lastIndexOf('.')), + coreUpstream.substring(1), + '$coreUpstream-llamadart.1', + '$coreUpstream-99999999999999999999', + ]) + ( + 'a macOS-complete manifest pinning the malformed tag $malformed', + () => pinningLiteRtLm(malformed), + malformed, + false, + ), ]) { final advice = upgradeCompanion ? 'only asks to upgrade the companion' diff --git a/website/docs/platforms/native-build-hooks.md b/website/docs/platforms/native-build-hooks.md index 3bb5f0ca2..7531d1c51 100644 --- a/website/docs/platforms/native-build-hooks.md +++ b/website/docs/platforms/native-build-hooks.md @@ -412,7 +412,8 @@ A companion with the maintained manifest pinning an earlier build of the same LiteRT-LM release, such as `0.0.15` (`v0.18.0`), still embeds the complete macOS runtime, which the app loads from its bundle, so the build only warns to upgrade the companion to `0.0.16` (`v0.18.0-1`). A maintained manifest pinning -any other LiteRT-LM release warns to upgrade the core and companion together. +any other LiteRT-LM release or a later build of the same release, such as +`v0.18.0-2`, warns to upgrade the core and companion together. Without a companion, and for non-Flutter or non-Apple builds, the hook path above applies.