Files
craft-ai-integrations/tools/mutation/mutation_generic_uploads.py
Jeremy 611195b50e feat(ai-comfyui): send a selection picture once and resend it only when it is gone (#82)
* feat(ai-comfyui): import a selection workflow and run it with the picture sent first

Adds the `image.segment` capability to imported ComfyUI workflows. A selection workflow maps a
text prompt and one source picture; the picture is uploaded, the graph is queued with the name
ComfyUI gave it, and every queued graph gets a sink name of its own so a repeat is not answered
from cache with no output. Clicks or a box are refused in plain words before anything is sent,
and a stored workflow file may declare a source binding and no mask.

The answer is the graph's image as it comes. Checking the mask is the next change.

* feat(ai-comfyui): check a selection workflow's mask and answer in the shared shape

A selection workflow's image is now a mask the shared contract can trust. It must be the size of
the picture sent (a JPEG may come back turned a quarter, because ComfyUI's loader applies the
EXIF turn), is reduced to one 8-bit channel, and is followed by the shared `Found` result. A mask
with no pixel above half is an empty mask and `matched: false`, never a failed job. Picture sizes
are read from the PNG or JPEG header and bounded.

This is not calibrated absence detection: the example Florence graph finds a box for any phrase.

* feat(ai-comfyui): send a selection picture once and resend it only when it is gone

A repeat selection on the same picture reuses the file already in ComfyUI's scratch area, keyed by
a hash of the bytes. Only the last four pictures are remembered. If ComfyUI has cleared its
scratch area (a restart), the first refusal of a reused name drops the entry and sends the
picture again once; a refusal of a fresh upload is never retried. A failed selection job clears
the cache, since the file may be what went wrong. Adds the ignored live test against a real
ComfyUI.

* test(ai-comfyui): count refused prompts without the deprecated fetch_update

Newer Rust renames AtomicUsize::fetch_update to try_update and warns, which the strict Clippy job
turns into an error on CI. The scripted server handles one job at a time here, so a load and a
fetch_sub do the same job and build on both toolchains.

* fix(ai-comfyui): hand on a binary mask, white where selected and black elsewhere

A review finding: the first channel was kept as the graph saved it, so pixels from 128 to 254 reached
the app as partly selected, while the shared contract says selected pixels are white. Every pixel
above half is now 255 and the rest 0. Written first and seen to fail (the answer kept
[0, 100, 128, 200, 255]); mutation cases added for a grey kept as selected and a low grey kept.

* docs(ai-comfyui): state the selection mask's byte limit, test it, and rename a mutation case

Two review findings. The mask is read inline under the provider's output limit (never more than
the 32 MiB inline cap) and never to the output folder, because the checks need its bytes in hand;
that is now written in the module header and the provider, and a test shows a mask over a lowered
limit fails the job and hands on nothing, with a mutation case for the limit being ignored. The
case that only removed the media-type guard is renamed so it no longer implies JPEG answers are
covered. The limit test passed on first run, as the behaviour already existed: it is a guard, not a
red-first fix.

* fix(ai-comfyui): forget the cached pictures when any selection job fails

A review finding: only an error status from ComfyUI cleared the upload cache, so a job whose mask
was fetched but refused (wrong size, not a PNG) left its picture cached and the next selection
could reuse a stale scratch name. The cache is now cleared once, after the job's state is settled,
whenever a selection job ends failed. Written first and seen to fail (a wrong-size mask left one
upload for two selections); the existing mutation case is re-aimed at the single clear.
2026-10-21 23:09:58 +01:00

26 lines
1.7 KiB
Python

"""Deliberate-bug cases for group `gu`: the picture upload cache of the generic `image.segment` route
on ComfyUI (what keys it, how many it keeps, the resend after a cleared scratch area or a failed
job).
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).
Not covered: the live test (`tests/generic_segment_live.rs`, ignored) is the only check of a real
graph.
"""
SR = 'crates/ai-comfyui/src/segment_route.rs'
PV = 'crates/ai-comfyui/src/provider.rs'
CC = 'craft-ai-comfyui'
FIND = 'if let Some((_, name)) = self.uploads().iter().find(|(k, _)| *k == key) {'
STALE = 'Err(e) if reused && e.kind == ErrorKind::InvalidRequest => {'
GU_CASES = [
('cache-key-ignores-the-bytes', SR, 'image.data.hash(&mut hasher);', '', CC, 'lib', 'the_cache_key_follows'),
('picture-sent-every-time', PV, FIND, 'if let Some((_, name)) = self.uploads().iter().find(|_| false) {', CC, 'segment_route', 'a_picture_is_sent_once'),
('nothing-is-forgotten', PV, 'if uploads.len() > REMEMBERED_UPLOADS {', 'if false {', CC, 'segment_route', 'the_oldest_picture'),
('stale-upload-not-retried', PV, STALE, 'Err(e) if false && reused && e.kind == ErrorKind::InvalidRequest => {', CC, 'segment_route', 'a_cleared_scratch'),
('fresh-refusal-retried', PV, STALE, 'Err(e) if e.kind == ErrorKind::InvalidRequest => {', CC, 'segment_route', 'a_refusal_for_a_fresh'),
('stale-name-kept', PV, ' self.uploads().retain(|(k, _)| *k != key);\n', '', CC, 'segment_route', 'a_cleared_scratch'),
('failed-job-keeps-the-upload', PV, ' self.uploads().clear();\n', '', CC, 'segment_route', 'a_failed_job_makes'),
]