diff --git a/CHANGELOG.md b/CHANGELOG.md index 30fc08183..6d5bb55c9 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,9 @@ ## Unreleased +- From core `0.13.0`, the Flutter Apple build error or warning for a + mismatched or earlier-build llama.cpp companion names the companion release + of the core's own minor, such as `^0.13.0`, instead of the first `0.0.x` + release pinning the native runtime. - 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 the app cannot load LiteRT-LM and needs diff --git a/hook/build.dart b/hook/build.dart index 9d78db0ab..a9f253095 100644 --- a/hook/build.dart +++ b/hook/build.dart @@ -19,6 +19,7 @@ import 'package:logging/logging.dart'; import 'package:path/path.dart' as path; import 'package:yaml/yaml.dart'; +import 'package:llamadart/src/hook/apple_companion_constraint.dart'; import 'package:llamadart/src/hook/native_archive_digest.dart'; import 'package:llamadart/src/hook/native_bundle_config.dart'; import 'package:llamadart/src/hook/native_release_pins.dart'; @@ -685,7 +686,13 @@ _AppleCompanions? _flutterAppleCompanions({ final runtimes = []; String? liteRtLmMacOsWarning; if (dependencies.contains(_llamaCppFlutterPackageName)) { - _validateAppleCompanion(_llamaCppCompanion, consumerRoot, output, log); + _validateAppleCompanion( + _llamaCppCompanion, + consumerRoot, + packageRoot, + output, + log, + ); runtimes.add(nativeRuntimeLlamaCpp); } if (dependencies.contains(_liteRtLmFlutterPackageName)) { @@ -704,6 +711,7 @@ _AppleCompanions? _flutterAppleCompanions({ _validateAppleCompanion( _stableDiffusionCompanion, consumerRoot, + packageRoot, output, log, ); @@ -728,7 +736,8 @@ _AppleCompanions? _flutterAppleCompanions({ /// through `let [tagVariable]` in the maintained manifest template. /// [compatibleTags] maps earlier tags accepted with a warning to what their /// frameworks lack. [companionVersion] is the first companion release pinning -/// [nativeTag], when the core records it. +/// [nativeTag], when the core records it; [appleCompanionConstraint] decides +/// whether a build error names it. typedef _AppleCompanionContract = ({ String label, String packageName, @@ -765,22 +774,24 @@ const _AppleCompanionContract _stableDiffusionCompanion = ( void _validateAppleCompanion( _AppleCompanionContract contract, Directory consumerRoot, + Directory packageRoot, BuildOutputBuilder output, Logger log, ) { - final companionVersion = contract.companionVersion; - final requiredCompanion = companionVersion == null - ? 'Resolve ${contract.packageName} with a Package.swift pin matching ' - '${contract.nativeRepository}@${contract.nativeTag} and rerun ' - 'flutter pub get. ' - : 'Depend on `${contract.packageName}: ^$companionVersion`, the first ' - 'release pinning ${contract.nativeRepository}@' - '${contract.nativeTag}, in pubspec.yaml and rerun flutter pub ' - 'get; a caret constraint on an earlier 0.0.x release never ' - 'resolves it. '; + final coreVersion = _corePackageVersion(packageRoot); + final nativePin = '${contract.nativeRepository}@${contract.nativeTag}'; + final requiredCompanion = + appleCompanionRequirement( + packageName: contract.packageName, + coreVersion: coreVersion, + nativePin: nativePin, + firstPinningRelease: contract.companionVersion, + ) ?? + 'Resolve ${contract.packageName} with a Package.swift pin matching ' + '$nativePin and rerun flutter pub get.'; Never reject(String reason) => throw StateError( 'Incompatible Apple ${contract.label} companion: $reason ' - '$requiredCompanion' + '$requiredCompanion ' 'Upgrade the core and companion together to a matching released pair; ' 'native tag/path overrides do not replace SPM frameworks. ' 'No in-process native asset was emitted.', @@ -859,12 +870,18 @@ void _validateAppleCompanion( } final missing = contract.compatibleTags[pinnedTag]; if (missing != null) { + final constraint = appleCompanionConstraint( + coreVersion, + firstPinningRelease: contract.companionVersion, + ); + final next = constraint == null + ? 'Upgrade ${contract.packageName} to a release pinning' + : 'Depend on `${contract.packageName}: $constraint` to link'; final upgrade = 'Resolved ${contract.packageName} ${metadata['version']} pins ' '${contract.nativeRepository}@$pinnedTag, an earlier build of the ' 'same ${contract.label} runtime as ${contract.nativeTag}, so this ' - 'build links it. Its frameworks lack $missing. Depend on ' - '`${contract.packageName}: ^${contract.companionVersion}` to link ' + 'build links it. Its frameworks lack $missing. $next ' '${contract.nativeTag}.'; log.warning(upgrade); stderr.writeln('warning: llamadart: $upgrade'); @@ -876,6 +893,23 @@ void _validateAppleCompanion( } } +/// This core's pubspec `version`, which decides the companion constraint a +/// build error or warning names, or `null` when it cannot be read. +/// +/// It is not a build dependency: a cached build emits no error or warning. +String? _corePackageVersion(Directory packageRoot) { + final pubspec = File(path.join(packageRoot.path, 'pubspec.yaml')); + try { + final metadata = loadYaml(pubspec.readAsStringSync()); + final version = metadata is Map ? metadata['version'] : null; + return version is String ? version : null; + } on FileSystemException { + return null; + } on FormatException { + return null; + } +} + /// The root of the one [packageName] the consumer's nearest /// `.dart_tool/package_config.json` resolves; [reject] reports why there is /// none. diff --git a/lib/src/hook/apple_companion_constraint.dart b/lib/src/hook/apple_companion_constraint.dart new file mode 100644 index 000000000..bf96fcbc7 --- /dev/null +++ b/lib/src/hook/apple_companion_constraint.dart @@ -0,0 +1,97 @@ +/// The Apple companion dependency `hook/build.dart` tells an app to declare. +library; + +/// The first core `MAJOR.MINOR` whose Apple companions share it. +/// +/// Companions paired with an earlier core are numbered `0.0.x`. +const appleCompanionLockstepStart = (major: 0, minor: 13); + +final RegExp _versionPattern = RegExp( + r'^(\d+)\.(\d+)\.(\d+)(-[0-9A-Za-z.-]+)?(?:\+[0-9A-Za-z.-]+)?$', +); + +typedef _Version = ({int major, int minor, int patch, bool prerelease}); + +_Version? _parse(String? version) { + final match = version == null ? null : _versionPattern.firstMatch(version); + if (match == null) return null; + return ( + major: int.parse(match[1]!), + minor: int.parse(match[2]!), + patch: int.parse(match[3]!), + prerelease: match[4] != null, + ); +} + +bool _isLockstep(_Version core) { + const start = appleCompanionLockstepStart; + if (core.major != start.major) return core.major > start.major; + if (core.minor != start.minor) return core.minor > start.minor; + // A prerelease of the first lockstep version precedes it. + return core.patch > 0 || !core.prerelease; +} + +/// Whether core [coreVersion] is at or after `appleCompanionLockstepStart.0` +/// in semver precedence, so its Apple companions share its `MAJOR.MINOR`. +/// +/// A prerelease of that first version, such as `0.13.0-dev.1`, is not; a +/// [coreVersion] that is not a version is not either. +bool isAppleCompanionLockstepCore(String coreVersion) { + final core = _parse(coreVersion); + return core != null && _isLockstep(core); +} + +/// The pubspec constraint that resolves an Apple companion core +/// [coreVersion] accepts, or `null` when there is none to name. +/// +/// [firstPinningRelease] is the first companion release pinning the core's +/// native tag. A lockstep core ([isAppleCompanionLockstepCore]) names it when +/// it shares the core's `MAJOR.MINOR`, because the pin moved within that +/// minor, and otherwise the minor's first release: a caret on a 0.x release, +/// or `'>=FROM =$from <${core.major}.${core.minor + 1}.0'"; +} + +/// The sentence an incompatible Apple companion build error uses to tell the +/// app which [packageName] release to depend on, or `null` when +/// [appleCompanionConstraint] names none. +/// +/// [nativePin] is the `repository@tag` the core requires. +String? appleCompanionRequirement({ + required String packageName, + required String? coreVersion, + required String nativePin, + String? firstPinningRelease, +}) { + final constraint = appleCompanionConstraint( + coreVersion, + firstPinningRelease: firstPinningRelease, + ); + if (constraint == null) return null; + final core = _parse(coreVersion)!; + if (_isLockstep(core)) { + return 'Depend on `$packageName: $constraint`, the core ' + '${core.major}.${core.minor}.x companion releases pinning $nativePin, ' + 'in pubspec.yaml and rerun flutter pub get.'; + } + return 'Depend on `$packageName: $constraint`, the first release pinning ' + '$nativePin, in pubspec.yaml and rerun flutter pub get; a caret ' + 'constraint on an earlier 0.0.x release never resolves it.'; +} diff --git a/lib/src/hook/native_release_pins.dart b/lib/src/hook/native_release_pins.dart index 746a035d0..fc7399bae 100644 --- a/lib/src/hook/native_release_pins.dart +++ b/lib/src/hook/native_release_pins.dart @@ -10,9 +10,12 @@ const llamaCppTag = 'v0.6.0-3'; /// The first `llamadart_llama_cpp_flutter` release whose `Package.swift` pins /// [llamaCppTag]. /// -/// An incompatible-companion build error names it, because a caret constraint -/// on an earlier `0.0.x` companion can never resolve it. The native pin sync -/// sets it whenever it moves the companion pin. +/// Incompatible-companion build errors name it, because a caret constraint on +/// an earlier companion can never resolve it. A core from +/// `appleCompanionLockstepStart` on names it only when it shares the core's +/// `MAJOR.MINOR`, and its own minor's first release otherwise. The native pin +/// sync sets it to the companion's next release whenever it moves the +/// companion pin. const llamaCppAppleCompanionVersion = '0.0.23'; /// Earlier `leehack/llamadart-native` tags that a published Apple llama.cpp diff --git a/test/support/hook_core_version.dart b/test/support/hook_core_version.dart new file mode 100644 index 000000000..db78d82ba --- /dev/null +++ b/test/support/hook_core_version.dart @@ -0,0 +1,28 @@ +import 'dart:io'; + +import 'package:path/path.dart' as path; +import 'package:test/test.dart'; + +/// The `version` this checkout's core `pubspec.yaml` declares. +String readCheckoutCoreVersion() => RegExp( + r'^version:\s*(\S+)\s*$', + multiLine: true, +).firstMatch(File('pubspec.yaml').readAsStringSync())!.group(1)!; + +/// Runs [body] with the build hook's package root at a core declaring +/// [coreVersion], or at this checkout when it is `null`. +/// +/// `testCodeBuildHook` takes the package root from [Directory.current], so +/// this overrides it for [body] alone. +Future withHookCoreVersion( + String? coreVersion, + Future Function() body, +) async { + if (coreVersion == null) return body(); + final root = await Directory.systemTemp.createTemp('llamadart_core_'); + addTearDown(() => root.deleteSync(recursive: true)); + File( + path.join(root.path, 'pubspec.yaml'), + ).writeAsStringSync('name: llamadart\nversion: $coreVersion\n'); + return IOOverrides.runZoned(body, getCurrentDirectory: () => root); +} diff --git a/test/unit/hook/apple_companion_constraint_test.dart b/test/unit/hook/apple_companion_constraint_test.dart new file mode 100644 index 000000000..9a308e860 --- /dev/null +++ b/test/unit/hook/apple_companion_constraint_test.dart @@ -0,0 +1,148 @@ +@TestOn('vm') +library; + +import 'package:llamadart/src/hook/apple_companion_constraint.dart'; +import 'package:test/test.dart'; + +const _nativePin = 'leehack/llamadart-native@v0.6.0-3'; + +String? _requirement(String? coreVersion, {String? firstPinningRelease}) => + appleCompanionRequirement( + packageName: 'llamadart_llama_cpp_flutter', + coreVersion: coreVersion, + nativePin: _nativePin, + firstPinningRelease: firstPinningRelease, + ); + +void main() { + group('appleCompanionConstraint', () { + test('a core before lockstep names the first pinning 0.0.x release', () { + for (final core in ['0.12.0', '0.12.2', '0.9.1']) { + expect( + appleCompanionConstraint(core, firstPinningRelease: '0.0.23'), + '^0.0.23', + reason: core, + ); + expect(appleCompanionConstraint(core), isNull, reason: core); + } + }); + + test('a lockstep core names its minor\'s first release when the pin ' + 'predates the minor', () { + for (final (core, expected) in [ + ('0.13.0', '^0.13.0'), + ('0.13.4', '^0.13.0'), + ('0.14.1', '^0.14.0'), + ]) { + for (final firstPinning in ['0.0.23', '0.13.1']) { + if (firstPinning == '0.13.1' && core.startsWith('0.13.')) continue; + expect( + appleCompanionConstraint(core, firstPinningRelease: firstPinning), + expected, + reason: '$core with $firstPinning', + ); + } + } + }); + + test('a lockstep core names the first same-minor release pinning its ' + 'tag', () { + for (final (core, first) in [ + ('0.13.2', '0.13.1'), + ('0.13.1', '0.13.1'), + ('0.14.0', '0.14.0'), + ]) { + expect( + appleCompanionConstraint(core, firstPinningRelease: first), + '^$first', + reason: '$core with $first', + ); + } + }); + + test('a 1.x core names a range a caret would widen', () { + expect( + appleCompanionConstraint('1.2.3', firstPinningRelease: '1.2.1'), + "'>=1.2.1 <1.3.0'", + ); + expect( + appleCompanionConstraint('1.2.3', firstPinningRelease: '1.1.4'), + "'>=1.2.0 <1.3.0'", + ); + }); + + test('a prerelease of the first lockstep core precedes lockstep', () { + expect( + appleCompanionConstraint('0.13.0-dev.1', firstPinningRelease: '0.0.23'), + '^0.0.23', + ); + for (final (core, expected) in [ + ('0.13.0+build.1', '^0.13.0'), + ('0.13.1-dev.1', '^0.13.0'), + ('0.14.0-dev.1', '^0.14.0'), + ]) { + expect( + appleCompanionConstraint(core, firstPinningRelease: '0.0.23'), + expected, + reason: core, + ); + } + }); + + test('an unreadable core version names nothing', () { + for (final core in [null, '', 'latest', '0.13']) { + expect( + appleCompanionConstraint(core, firstPinningRelease: '0.0.23'), + isNull, + reason: core, + ); + } + }); + }); + + group('appleCompanionRequirement', () { + test('core 0.12.x names the first release pinning the native tag', () { + expect( + _requirement('0.12.2', firstPinningRelease: '0.0.23'), + 'Depend on `llamadart_llama_cpp_flutter: ^0.0.23`, the first release ' + 'pinning $_nativePin, in pubspec.yaml and rerun flutter pub get; a ' + 'caret constraint on an earlier 0.0.x release never resolves it.', + ); + }); + + test('core 0.13.x names the companions of its minor', () { + for (final (core, first, constraint) in [ + ('0.13.0', '0.0.23', '^0.13.0'), + ('0.13.2', '0.0.23', '^0.13.0'), + ('0.13.2', '0.13.1', '^0.13.1'), + ]) { + final requirement = _requirement(core, firstPinningRelease: first); + expect( + requirement, + 'Depend on `llamadart_llama_cpp_flutter: $constraint`, the core ' + '0.13.x companion releases pinning $_nativePin, in pubspec.yaml and ' + 'rerun flutter pub get.', + reason: '$core with $first', + ); + expect(requirement, isNot(contains('0.0.')), reason: core); + } + }); + + test('names nothing when no constraint is known', () { + for (final core in ['0.12.2', '0.13.0', '0.13.2', '1.2.3']) { + expect(appleCompanionConstraint(core), isNull, reason: core); + expect(_requirement(core), isNull, reason: core); + } + expect(_requirement(null, firstPinningRelease: '0.0.23'), isNull); + }); + }); + + test('isAppleCompanionLockstepCore follows semver precedence', () { + for (final core in ['0.13.0', '0.13.0+build.1', '0.13.1-dev.1', '1.0.0']) { + expect(isAppleCompanionLockstepCore(core), isTrue, reason: core); + } + for (final core in ['0.12.9', '0.13.0-dev.1', '0.0.23', 'latest', '']) { + expect(isAppleCompanionLockstepCore(core), isFalse, reason: core); + } + }); +} 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 3e1bdbc98..f247fbc61 100644 --- a/test/unit/hook/build_hook_litert_lm_integration_test.dart +++ b/test/unit/hook/build_hook_litert_lm_integration_test.dart @@ -6,12 +6,14 @@ import 'dart:io'; import 'package:code_assets/code_assets.dart'; import 'package:hooks/hooks.dart'; +import 'package:llamadart/src/hook/apple_companion_constraint.dart'; import 'package:llamadart/src/hook/native_release_pins.dart'; import 'package:logging/logging.dart'; import 'package:path/path.dart' as path; import 'package:test/test.dart'; import '../../../hook/build.dart' as build_hook; +import '../../support/hook_core_version.dart'; void main() { final nativeTag = _readPinnedConst('llamaCppTag'); @@ -393,45 +395,87 @@ void main() { ); } + // The checkout core's guidance comes from the helper; the fixed cores pin + // the literal text, so a hook that stops reading its own version, or names + // a 0.0.x companion from a lockstep core, fails. + final checkoutCore = readCheckoutCoreVersion(); + final coreConstraints = { + null: appleCompanionConstraint( + checkoutCore, + firstPinningRelease: llamaCppAppleCompanionVersion, + )!, + for (final core in ['0.12.2', '0.13.0-dev.1']) + core: '^$llamaCppAppleCompanionVersion', + '0.13.0': '^0.13.0', + '0.13.3': '^0.13.0', + }; + final coreRequirements = { + null: appleCompanionRequirement( + packageName: 'llamadart_llama_cpp_flutter', + coreVersion: checkoutCore, + nativePin: 'leehack/llamadart-native@$llamaCppTag', + firstPinningRelease: llamaCppAppleCompanionVersion, + )!, + for (final core in ['0.12.2', '0.13.0-dev.1']) + core: + 'Depend on `llamadart_llama_cpp_flutter: ' + '^$llamaCppAppleCompanionVersion`, the first release pinning ' + 'leehack/llamadart-native@$llamaCppTag, in pubspec.yaml and rerun ' + 'flutter pub get; a caret constraint on an earlier 0.0.x release ' + 'never resolves it.', + for (final core in ['0.13.0', '0.13.3']) + core: + 'Depend on `llamadart_llama_cpp_flutter: ^0.13.0`, the core 0.13.x ' + 'companion releases pinning leehack/llamadart-native@$llamaCppTag, ' + 'in pubspec.yaml and rerun flutter pub get.', + }; + String coreLabel(String? core) => core ?? 'checkout $checkoutCore'; + for (final target in [OS.iOS, OS.macOS]) { for (final MapEntry(key: tag, value: missing) in llamaCppCompatibleAppleCompanionTags.entries) { - test('Apple $target links a companion pinning compatible $tag with an ' - 'upgrade warning', () async { - final records = []; - final subscription = Logger.root.onRecord.listen( - (record) => records.add(record.message), - ); - addTearDown(subscription.cancel); - final defines = await _flutterAppleUserDefines( - dependencies: const ['llamadart_llama_cpp_flutter'], - companionTag: tag, - companionVersion: '0.0.22', - ); - final stderrLines = await _capturingStderr( - () => testCodeBuildHook( - mainMethod: build_hook.main, - targetOS: target, - targetArchitecture: Architecture.arm64, - targetIOSSdk: target == OS.iOS ? IOSSdk.iPhoneOS : null, - userDefines: defines, - check: (_, output) { - final asset = output.assets.encodedAssets.single.asCodeAsset; - expect(asset.id, 'package:llamadart/llamadart'); - expect(asset.linkMode, isA()); - }, - ), - ); - final warning = - 'Resolved llamadart_llama_cpp_flutter 0.0.22 pins ' - 'leehack/llamadart-native@$tag, an earlier build of the same ' - 'llama.cpp runtime as $llamaCppTag, so this build links it. ' - 'Its frameworks lack $missing. Depend on ' - '`llamadart_llama_cpp_flutter: ^$llamaCppAppleCompanionVersion` ' - 'to link $llamaCppTag.'; - expect(stderrLines, contains('warning: llamadart: $warning')); - expect(records, contains(warning)); - }); + for (final MapEntry(key: core, value: constraint) + in coreConstraints.entries) { + test('Apple $target core ${coreLabel(core)} links a companion pinning ' + 'compatible $tag and names `$constraint`', () async { + final records = []; + final subscription = Logger.root.onRecord.listen( + (record) => records.add(record.message), + ); + addTearDown(subscription.cancel); + final defines = await _flutterAppleUserDefines( + dependencies: const ['llamadart_llama_cpp_flutter'], + companionTag: tag, + companionVersion: '0.0.22', + ); + final stderrLines = await _capturingStderr( + () => withHookCoreVersion( + core, + () => testCodeBuildHook( + mainMethod: build_hook.main, + targetOS: target, + targetArchitecture: Architecture.arm64, + targetIOSSdk: target == OS.iOS ? IOSSdk.iPhoneOS : null, + userDefines: defines, + check: (_, output) { + final asset = output.assets.encodedAssets.single.asCodeAsset; + expect(asset.id, 'package:llamadart/llamadart'); + expect(asset.linkMode, isA()); + }, + ), + ), + ); + final warning = + 'Resolved llamadart_llama_cpp_flutter 0.0.22 pins ' + 'leehack/llamadart-native@$tag, an earlier build of the same ' + 'llama.cpp runtime as $llamaCppTag, so this build links it. ' + 'Its frameworks lack $missing. Depend on ' + '`llamadart_llama_cpp_flutter: $constraint` to link ' + '$llamaCppTag.'; + expect(stderrLines, contains('warning: llamadart: $warning')); + expect(records, contains(warning)); + }); + } test('Apple $target still rejects local Artifacts beside compatible ' '$tag', () async { @@ -495,39 +539,42 @@ void main() { }); for (final tag in ['v0.6.0-1', 'v0.6.0-4', 'v0.5.0-2']) { - test('Apple $target rejects unlisted companion pin $tag and names the ' - 'companion release to depend on', () async { - final defines = await _flutterAppleUserDefines( - dependencies: const ['llamadart_llama_cpp_flutter'], - companionTag: tag, - companionVersion: '0.0.22', - ); - var emitted = false; - await expectLater( - testCodeBuildHook( - mainMethod: build_hook.main, - targetOS: target, - targetArchitecture: Architecture.arm64, - targetIOSSdk: target == OS.iOS ? IOSSdk.iPhoneOS : null, - userDefines: defines, - check: (_, _) => emitted = true, - ), - throwsA( - predicate( - (error) => error.toString().contains( - 'Incompatible Apple llama.cpp companion: Resolved companion ' - '0.0.22 does not uniquely pin the required native runtime ' - '$llamaCppTag. Depend on `llamadart_llama_cpp_flutter: ' - '^$llamaCppAppleCompanionVersion`, the first release ' - 'pinning leehack/llamadart-native@$llamaCppTag, in ' - 'pubspec.yaml and rerun flutter pub get; a caret ' - 'constraint on an earlier 0.0.x release never resolves it.', + for (final MapEntry(key: core, value: requirement) + in coreRequirements.entries) { + test('Apple $target core ${coreLabel(core)} rejects unlisted companion ' + 'pin $tag and names the companion release to depend on', () async { + final defines = await _flutterAppleUserDefines( + dependencies: const ['llamadart_llama_cpp_flutter'], + companionTag: tag, + companionVersion: '0.0.22', + ); + var emitted = false; + await expectLater( + withHookCoreVersion( + core, + () => testCodeBuildHook( + mainMethod: build_hook.main, + targetOS: target, + targetArchitecture: Architecture.arm64, + targetIOSSdk: target == OS.iOS ? IOSSdk.iPhoneOS : null, + userDefines: defines, + check: (_, _) => emitted = true, ), ), - ), - ); - expect(emitted, isFalse); - }); + throwsA( + predicate( + (error) => error.toString().contains( + 'Incompatible Apple llama.cpp companion: Resolved companion ' + '0.0.22 does not uniquely pin the required native runtime ' + '$llamaCppTag. $requirement Upgrade the core and companion ' + 'together', + ), + ), + ), + ); + expect(emitted, isFalse); + }); + } } } diff --git a/test/unit/hook/build_hook_stable_diffusion_integration_test.dart b/test/unit/hook/build_hook_stable_diffusion_integration_test.dart index 8cab7ca0b..ba47ffb2e 100644 --- a/test/unit/hook/build_hook_stable_diffusion_integration_test.dart +++ b/test/unit/hook/build_hook_stable_diffusion_integration_test.dart @@ -13,6 +13,7 @@ import 'package:test/test.dart'; import 'package:llamadart/src/hook/native_release_pins.dart'; import '../../../hook/build.dart' as build_hook; +import '../../support/hook_core_version.dart'; import '../../support/hooks_runner_layout.dart'; const _stableDiffusionAssetId = 'package:llamadart/stable_diffusion'; @@ -806,6 +807,48 @@ dependencies: }, ); } + + // No first stable_diffusion companion release pinning the tag is recorded, + // so even a lockstep core must not claim a constraint that pins it. + for (final core in ['0.12.2', '0.13.0', '0.13.2']) { + final requirement = + 'Resolve llamadart_stable_diffusion_flutter with a Package.swift pin ' + 'matching leehack/stable-diffusion-native@$stableDiffusionReleaseTag ' + 'and rerun flutter pub get.'; + test('core $core: the stable_diffusion companion rejection names the ' + 'native pin and no companion release', () async { + final defines = await _flutterAppleApp( + companions: const [_stableDiffusionCompanionName], + editManifest: (manifest) => manifest.replaceFirst( + 'let stableDiffusionTag = "$stableDiffusionReleaseTag"', + 'let stableDiffusionTag = "v0.1.1"', + ), + ); + await expectLater( + withHookCoreVersion( + core, + () => testCodeBuildHook( + mainMethod: build_hook.main, + targetOS: OS.iOS, + targetArchitecture: Architecture.arm64, + targetIOSSdk: IOSSdk.iPhoneOS, + userDefines: defines, + check: (_, _) => fail('Must not emit in-process assets'), + ), + ), + throwsA( + predicate( + (error) => + error.toString().contains( + 'the required native runtime $stableDiffusionReleaseTag. ' + '$requirement Upgrade the core and companion together', + ) && + !error.toString().contains('Depend on'), + ), + ), + ); + }); + } } const _impliedSkipWarning = diff --git a/test/unit/tooling/verify_release_docs_companion_core_constraint_test.dart b/test/unit/tooling/verify_release_docs_companion_core_constraint_test.dart index 9fa3d9cbd..0e69137a4 100644 --- a/test/unit/tooling/verify_release_docs_companion_core_constraint_test.dart +++ b/test/unit/tooling/verify_release_docs_companion_core_constraint_test.dart @@ -3,6 +3,7 @@ library; import 'dart:io'; +import 'package:llamadart/src/hook/apple_companion_constraint.dart'; import 'package:llamadart/src/hook/native_release_pins.dart'; import 'package:pub_semver/pub_semver.dart'; import 'package:test/test.dart'; @@ -413,6 +414,29 @@ void main() { ); }); + test('starts where the hook names a lockstep companion constraint', () { + final root = companionsAt({}); + + for (final core in [ + '0.12.9', + '0.13.0-dev.1', + '0.13.0', + '0.13.0+build.1', + '0.13.1-dev.1', + '1.0.0', + ]) { + expect( + checkCompanionVersionAlignment(root, Version.parse(core)).isEmpty, + !isAppleCompanionLockstepCore(core), + reason: core, + ); + } + expect( + checkCompanionVersionAlignment(root, Version.parse('0.13.0-dev.1')), + isEmpty, + ); + }); + test('accepts any companion patch within the core minor', () { final root = companionsAt({ _llamaCpp: '0.13.0', diff --git a/tool/native/test_sync_native_release_pins.py b/tool/native/test_sync_native_release_pins.py index 8af30d181..b5e3ba80c 100644 --- a/tool/native/test_sync_native_release_pins.py +++ b/tool/native/test_sync_native_release_pins.py @@ -3243,6 +3243,37 @@ def test_a_pin_move_clears_compatible_tags_and_records_the_companion( self.assertIn("cleared compatible Apple llama.cpp companion tags", result.stdout) self.assertIn("v0.0.1-1", result.stdout) + def test_a_lockstep_pin_move_records_the_next_same_minor_companion( + self, + ) -> None: + # From core 0.13.0 the hook names this constant when it shares the + # core MAJOR.MINOR, so a pin moved inside a minor must record the + # first companion release of that minor that pins it. + current = pins.read_hook_native_tag(self.pins.read_text(encoding="utf-8")) + upstream = pins.parse_native_release_tag(current).upstream_tag + build = int(current.rsplit("-", 1)[1]) if "-" in current else 0 + target = f"{upstream}-{build + 1}" + for extra in ((), ("--bump-companion-versions",)): + with self.subTest(extra=extra): + self.make_repo() + pubspec = self.repo_root / self.COMPANION / "pubspec.yaml" + pubspec.write_text( + pins.replace_pubspec_version( + pubspec.read_text(encoding="utf-8"), "0.13.0", pubspec + ), + encoding="utf-8", + ) + result = self.run_sync(target, *extra) + self.assertEqual(result.returncode, 0, result.stderr) + self.assertIn( + "const llamaCppAppleCompanionVersion = '0.13.1';", + self.pins.read_text(encoding="utf-8"), + ) + self.assertIn( + f"version: {'0.13.1' if extra else '0.13.0'}\n", + pubspec.read_text(encoding="utf-8"), + ) + def test_a_same_tag_resync_keeps_compatible_tags(self) -> None: original = self.pins.read_text(encoding="utf-8") compatible = re.search( diff --git a/tool/testing/verify_release_docs_versions.dart b/tool/testing/verify_release_docs_versions.dart index f0d28c4e8..59141e392 100644 --- a/tool/testing/verify_release_docs_versions.dart +++ b/tool/testing/verify_release_docs_versions.dart @@ -3,6 +3,7 @@ import 'dart:convert'; import 'dart:io'; +import 'package:llamadart/src/hook/apple_companion_constraint.dart'; import 'package:llamadart/src/hook/native_bundle_config.dart'; import 'package:llamadart/src/hook/native_release_pins.dart'; import 'package:pub_semver/pub_semver.dart'; @@ -395,21 +396,18 @@ List checkCompanionCoreConstraints( return errors; } -/// The first core release whose companions share its `MAJOR.MINOR`. -/// -/// Earlier companions are numbered `0.0.x` and stay valid beside an earlier -/// core, so the check starts with the release that renumbers them. -final Version companionVersionAlignmentStart = Version(0, 13, 0); - /// Checks that every companion's `version` shares [coreVersion]'s -/// `MAJOR.MINOR`, from [companionVersionAlignmentStart] on. +/// `MAJOR.MINOR` once [isAppleCompanionLockstepCore] holds for it. /// -/// Companion patch numbers move independently of the core's. +/// Earlier companions are numbered `0.0.x` and stay valid beside an earlier +/// core. The hook decides with the same predicate, so the companion +/// constraint a build error names always admits the companion this check +/// aligns. Companion patch numbers move independently of the core's. List checkCompanionVersionAlignment( Directory repoRoot, Version coreVersion, ) { - if (coreVersion < companionVersionAlignmentStart) return const []; + if (!isAppleCompanionLockstepCore('$coreVersion')) return const []; final errors = []; final series = '${coreVersion.major}.${coreVersion.minor}'; for (final companion in companionSwiftPins) { diff --git a/website/docs/changelog/recent-releases.md b/website/docs/changelog/recent-releases.md index 3f556c660..b1d8d056c 100644 --- a/website/docs/changelog/recent-releases.md +++ b/website/docs/changelog/recent-releases.md @@ -9,6 +9,10 @@ For canonical full release notes, use: ## Unreleased +- From core `0.13.0`, the Flutter Apple build error or warning for a + mismatched or earlier-build llama.cpp companion names the companion release + of the core's own minor, such as `^0.13.0`, instead of the first `0.0.x` + release pinning the native runtime. - A chat template that fails to render now names its source in the `LlamaInferenceException` message: the model's `tokenizer.chat_template` or its `tool_use` variant, `ModelParams.chatTemplate`, or `customTemplate`