Files
craft-ai-integrations/tools/mutation/mutation_native.py
T
Jeremy d470ff7b71 feat(ai-features): list SAM 2.1 on the built-in rten engine, reading the published ONNX files (#84)
* feat(ai-core): share the image.segment request and result contract

* fix(ai-core): take only an inline PNG as the selection mask

* feat(ai-engine-rten): bounded picture decoding, mask encoding and resampling for selection models

* feat(ai-engine-rten): run SAM 2.1 through the shared selection contract

* fix(ai-engine-rten): bound a thin picture's memory, refuse extreme sides, and make the mask size fallible

* feat(ai-features): let each built-in engine follow its own recipe

A built-in engine now plans, offers, registers and checks files from the first usable recipe that
lists it (`Feature::engine_recipe`), instead of every engine acting on the first usable recipe.
A first recipe that lists one engine no longer hides a later recipe that lists another. The plain
built-in offer and engine advice still start from the first usable recipe, and `Feature::engines`
lists every engine some usable recipe names, once each, in catalogue order.

The catalogue is unchanged, so no behaviour changes today; the tests build the two-recipe shape
from the real SAM 3 recipe.

* feat(ai-features): list SAM 2.1 on the built-in rten engine, reading the published ONNX files

SAM 2.1 now lists a `rten` route beside the ComfyUI one: the two published ONNX files
(`cstr/sam2.1-hiera-tiny-ONNX` at a pinned revision, size and SHA-256 pinned), read as they are,
with no conversion and nothing hosted by us. A built-in route may now name `.onnx` weights as well
as `.safetensors`, in the catalogue and when an app is given a weights path. SAM 2.1 stays second
in the list, so nothing changes for a person who has already chosen SAM 3.

The model is in the Apache-2.0 licence of Meta's SAM 2.1; the publisher of the conversion
declares the same. Real-model checks (ignored unless the files are given): two apps get a mask by
asking for Select subject, and a hover through the ordinary job path.

* docs(ai-features): the pinned SAM 2.1 export is the tiny size, not the small one

A review finding: the doc comment on the ONNX file list said small, but the pinned repository is
cstr/sam2.1-hiera-tiny-ONNX and the pinned sizes match the tiny variant. SAM 2.1 also has a
separate small variant, so the wrong word could send a maintainer to the wrong file.

* fix(ai-engine-rten): judge a match on the mask handed on, not on the raw logits

A review finding: matched was set from the model's raw logits before resampling and 8-bit
quantization, so one positive logit that a small picture never samples reported a match with an
all-black mask. The match is now whether any pixel of the emitted mask is above half, using the
same quantization as the PNG. Written first and seen to fail (matched stayed true for a speck at
logit 8 in a 10 by 6 picture). The runner's raw-logit test stays as a shortcut that skips the
resampling; forcing it open gives the same answer, so its mutation case is retired with the reason
in the group header.

* docs(mutation): point the usage text at GROUPS instead of a list that was out of date

A review finding: the usage line named only the early groups and left out rt, which is new here,
and also seg, gs and others. A list in the usage text goes stale each time a group is added, so
it now says to take the group from GROUPS and README.md.
2026-10-21 23:17:47 +01:00

31 lines
4.7 KiB
Python

"""Deliberate-bug cases for group `nf`: planning and offering a built-in runtime's own files.
Loaded by mutation_check.py. Each case breaks one rule and names the test that should notice.
Format: (name, file, old, new, package, test target, test filter).
"""
F = 'crates/ai-features/src/'
FE = 'craft-ai-features'
NF = [
('native-plan-ignores-usable', F + 'feature.rs', '.find(|r| r.usable && r.native.iter().any(|n| n.provider == provider))', '.find(|r| r.native.iter().any(|n| n.provider == provider))', FE, 'native_setup', 'cannot_run_yet'),
('engine-recipe-ignores-the-engine', F + 'feature.rs', '.find(|r| r.usable && r.native.iter().any(|n| n.provider == provider))', '.find(|r| r.usable)', FE, 'engine_recipe', 'each_engine_plans_from'),
('native-plan-takes-the-first-recipe', F + 'plan.rs', 'let recipe = feature.engine_recipe(provider);', 'let recipe = feature.first_usable_recipe();', FE, 'engine_recipe', 'each_engine_plans_from'),
('first-usable-recipe-takes-the-first', F + 'feature.rs', 'self.locals.iter().find(|r| r.usable)', 'self.locals.first()', FE, 'engine_recipe', 'plain_built_in_offer_skips'),
('native-offer-takes-the-first-recipe', F + 'setup.rs', 'Some(provider) => feature.engine_recipe(provider)?,', 'Some(_) => feature.locals.first()?,', FE, 'setup', 'unusable_first_recipe'),
('plain-built-in-offer-takes-the-first-recipe', F + 'setup.rs', 'None => feature.first_usable_recipe()?,', 'None => feature.locals.first()?,', FE, 'engine_recipe', 'plain_built_in_offer_skips'),
('native-option-from-every-recipe', F + 'feature.rs', 'if !out.contains(&route.provider.as_str()) {', 'if true {', FE, 'native_setup', 'first_usable_recipe'),
('native-option-from-the-first-recipe-only', F + 'options.rs', '.engine_recipe(provider)\n .and_then(|r| r.native.iter().find(|n| n.provider == provider))', '.first_usable_recipe()\n .and_then(|r| r.native.iter().find(|n| n.provider == provider))', FE, 'engine_recipe', 'each_engine_plans_from'),
('native-plan-takes-a-route-without-files', F + 'plan.rs', '.find(|n| n.provider == provider && !n.models.is_empty())\n }) else {\n return out;\n };\n fill_files(&mut out, &route.models, inventory);\n check_disk', '.find(|n| n.provider == provider)\n }) else {\n return out;\n };\n fill_files(&mut out, &route.models, inventory);\n check_disk', FE, 'native_setup', 'unfinished_or_unknown'),
('native-plan-takes-any-provider', F + 'plan.rs', '.find(|n| n.provider == provider && !n.models.is_empty())\n }) else {\n return out;\n };\n fill_files(&mut out, &route.models, inventory);\n check_disk', '.find(|n| !n.models.is_empty())\n }) else {\n return out;\n };\n fill_files(&mut out, &route.models, inventory);\n check_disk', FE, 'native_setup', 'unfinished_or_unknown'),
('native-plan-skips-the-disk-check', F + 'plan.rs', ' fill_files(&mut out, &route.models, inventory);\n check_disk(&mut out, inventory);', ' fill_files(&mut out, &route.models, inventory);', FE, 'native_setup', 'free_space'),
('native-offer-ignores-the-provider', F + 'setup.rs', 'selected_provider.is_none_or(|provider| n.provider == provider)', 'true', FE, 'setup', 'selected_provider'),
('built-in-offer-compares-comfyui-files', F + 'setup.rs', 'let compared = if let Some(native) = native {', 'let compared = if let Some(native) = native.filter(|_| false) {', FE, 'setup', 'needs_only_the_model'),
('option-offered-for-a-route-without-files', F + 'options.rs', ' .filter(|n| !n.models.is_empty())\n else {', ' else {', FE, 'native_setup', 'not_unbuilt_burn'),
('native-options-before-later-locals', F + 'options.rs', 'locals.extend(natives);', 'locals.splice(0..0, natives);', FE, 'native_setup', 'follow_every_local'),
('built-in-offer-uses-the-comfyui-terms', F + 'setup.rs', 'agreement: agreement_for(route),', 'agreement: agreement(),', FE, 'setup', 'agrees_to_its_own'),
('native-option-loses-its-model-id', F + 'options.rs', 'model: native.model.clone(),', 'model: String::new(),', FE, 'native_setup', 'candle_with_its_own_size'),
('onnx-is-not-weights', F + 'recipe.rs', ' name.ends_with(".safetensors") || name.ends_with(".onnx")', ' name.ends_with(".safetensors")', FE, 'native_onnx', 'an_onnx_file'),
('onnx-name-is-case-sensitive', F + 'recipe.rs', 'let name = m.filename.to_ascii_lowercase();', 'let name = m.filename.clone();', FE, 'native_onnx', 'an_onnx_file'),
('onnx-path-refused-when-binding', 'crates/ai-client/src/local.rs', '.is_some_and(|e| e.eq_ignore_ascii_case("safetensors") || e.eq_ignore_ascii_case("onnx"));', '.is_some_and(|e| e.eq_ignore_ascii_case("safetensors"));', 'craft-ai-client', 'local_native', 'the_native_route_is_registered'),
]