From 57f4268f596b8bf6300e0e2e82d1b340f2e70d8d Mon Sep 17 00:00:00 2001 From: Vito Sansevero Date: Fri, 20 Nov 2026 07:51:37 -0800 Subject: [PATCH] feat(review): queue an ambiguous file with one readable choice (#657) An ambiguous result is reviewable when at least one candidate carries a resolved prompt (MIN_CHOOSABLE = 1). The sweep with an LLM enhancer on Any Switch any_01 now records an item, by the scan and the monitor, with the run-time branch listed beside the readable one. A file with no choosable candidate still records nothing. --- database/review.py | 2 +- database/review_schema.py | 18 ++--- py/api/admin.py | 2 +- py/api/review_recording.py | 6 +- tests/sweep_graphs.py | 23 +++++++ tests/test_review_choosable.py | 119 ++++++++++++++++++++++++--------- utils/review_capture.py | 14 ++-- 7 files changed, 135 insertions(+), 49 deletions(-) diff --git a/database/review.py b/database/review.py index 280189e2..ae218f20 100644 --- a/database/review.py +++ b/database/review.py @@ -558,7 +558,7 @@ class ReviewOperations: """Remove the pending, undecided items that offer nothing to choose. ``locators`` are files the caller found ambiguous without a choice - (fewer than two candidates carry a prompt). Of their items, one leaves + (no candidate carries a prompt). Of their items, one leaves the queue only when its own stored evidence offers no choice either (``evidence_offers_choice``) and it is still ``pending`` at revision 0 with no decision row, the same safe delete as diff --git a/database/review_schema.py b/database/review_schema.py index c0c45d76..7b98cc94 100644 --- a/database/review_schema.py +++ b/database/review_schema.py @@ -215,22 +215,24 @@ def choosable(candidate: Any) -> bool: # An item is worth a decision only when at least this many candidates offer -# a prompt; with fewer there is nothing to choose between (maintainer test of -# #284/#288: 39 items whose candidates were all "unresolved"). -MIN_CHOOSABLE = 2 +# a prompt; with none there is nothing to choose (maintainer test of +# #284/#288: 39 items whose candidates were all "unresolved"). One is enough +# (#657 addendum D1, 2026-11-20): the other branches stay listed, disabled, +# and the user may still leave the file unresolved. +MIN_CHOOSABLE = 1 def evidence_offers_choice(evidence: Any) -> bool: """False when stored evidence provably offers nothing to choose. - Unlimited evidence must list ``MIN_CHOOSABLE`` choosable candidates. A - limited item stores none, only ``candidate_total``; it may offer a choice - (more than ``MAX_CANDIDATES`` prompts) unless that total is too small. + Unlimited evidence must list ``MIN_CHOOSABLE`` choosable candidates (the + saver anchor's one pair, #390, counts like any other). A limited item + stores none, only ``candidate_total``; it may offer a choice unless that + total is too small. """ if not isinstance(evidence, dict): return False - # The saver anchor's one pair (#390) is a suggestion to confirm. - needed = 1 if evidence.get("anchor") == "saver" else MIN_CHOOSABLE + needed = MIN_CHOOSABLE if evidence.get("limited"): total = evidence.get("candidate_total") return type(total) is int and total >= needed diff --git a/py/api/admin.py b/py/api/admin.py index 53ef20c2..b6444023 100644 --- a/py/api/admin.py +++ b/py/api/admin.py @@ -1767,7 +1767,7 @@ class AdminRoutesMixin: ``ambiguous`` is a list, ``media_file`` is appended to it; only a file that then returns ``"ambiguous"`` (nothing linked it) is a Needs review case. An ambiguous result with nothing to choose - (``reviewable`` is False: fewer than two candidates carry a prompt) + (``reviewable`` is False: no candidate carries a prompt) goes to ``untraced`` instead and returns ``"unresolved"`` when nothing links it. The parsed records of either ambiguous file go to ``records["canonical"]`` when ``records`` is a dict, and the ladder's diff --git a/py/api/review_recording.py b/py/api/review_recording.py index ccf4893c..25bc6985 100644 --- a/py/api/review_recording.py +++ b/py/api/review_recording.py @@ -6,7 +6,7 @@ final outcome is ambiguous and unlinked. The strict resolver must find it ``graph_prompt_text``) must not link it, and no saved row may link it already. A file the fallback links, an exact pair the ingester stored and an already linked file are never queued. Neither is an ambiguous file with -nothing to choose: fewer than two of its candidates carry a prompt +nothing to choose: none of its candidates carries a prompt (``utils.review_capture.reviewable``); the scan reports it unresolved ("could not be traced") unless the inspection profile finds a choice in it. A file the default profile calls unresolved gets the same inspection-profile chance @@ -398,8 +398,8 @@ class ReviewRecordingMixin: def _settle_untraced_reviews(self, media_files, roots, review=None): """Remove the pending items without a choice of untraced ``media_files``. - ``media_files`` are files found ambiguous with fewer than two - candidates that carry a prompt. Blocking; best effort like the linked + ``media_files`` are files found ambiguous without a candidate that + carries a prompt. Blocking; best effort like the linked settle (a failure is logged and counted in ``review["errors"]``). Returns how many items left. """ diff --git a/tests/sweep_graphs.py b/tests/sweep_graphs.py index 002f648c..b069319a 100644 --- a/tests/sweep_graphs.py +++ b/tests/sweep_graphs.py @@ -94,3 +94,26 @@ def ambiguous_sweep_graph(switch_class=UNKNOWN_SWITCH): graph = switch_graphs.any_switch_sweep_graph() graph["14"]["class_type"] = switch_class return graph + + +# The class of the maintainer's LLM prompt enhancer (no table lists it). +ENHANCER = "KikoPromptEnhancer" +# The one readable text of ``enhanced_sweep_graph`` (the joined ``any_02``). +ENHANCED_TEXT = f"{switch_graphs.SWEEP_PREFIX}, {switch_graphs.POSITIVE}" + + +def enhanced_sweep_graph(): + """The sweep with an LLM enhancer (17) between PromptManagerText (12) and + the switch's ``any_01`` (#657 addendum D1, live test 2026-11-20). + + The inspection profile is ambiguous with two candidates: ``any_01`` is + decided at run time (unresolved), ``any_02`` resolves to + ``ENHANCED_TEXT``. One readable choice: the file is reviewable. + """ + graph = switch_graphs.any_switch_sweep_graph() + graph["17"] = { + "class_type": ENHANCER, + "inputs": {"prompt": ["12", 0], "seed": 1, "model": "local-llm"}, + } + graph["14"]["inputs"]["any_01"] = ["17", 0] + return graph diff --git a/tests/test_review_choosable.py b/tests/test_review_choosable.py index a829ef2a..46e06ffc 100644 --- a/tests/test_review_choosable.py +++ b/tests/test_review_choosable.py @@ -3,22 +3,25 @@ The maintainer's live queue (#284 / #288) held 39 items whose 11 candidates all read "unresolved", "No positive / No negative": the ingestion resolver found several sampler branches but could trace no prompt on any of them. -An ambiguous result is reviewable only when at least two candidates carry a -resolved prompt text. With none, the file could not be traced; with exactly -one, the other branches may still have made the file, so the one text is -not certain either (the resolver never flattens unknown semantics). Both -stay unresolved and are never queued, and a rescan removes the pending, +An ambiguous result is reviewable when at least one candidate carries a +resolved prompt text (#657 addendum D1, 2026-11-20: one readable choice is +enough; the other branches stay listed, disabled, so the user sees them and +can still Leave unresolved). With none, the file could not be traced: it +stays unresolved and is never queued, and a rescan removes the pending, undecided items such files already have. """ +import json import os import unittest from unittest.mock import patch try: + from tests.sweep_graphs import ENHANCED_TEXT, enhanced_sweep_graph from tests.test_review import DELIMITERS, write_png from tests.test_review_outcome import OutcomeTestCase except ImportError: # discovered with tests/ as the top-level directory + from sweep_graphs import ENHANCED_TEXT, enhanced_sweep_graph from test_review import DELIMITERS, write_png from test_review_outcome import OutcomeTestCase @@ -67,6 +70,11 @@ class ChoosableTestCase(OutcomeTestCase): write_png(self.output_dir / name, prompt=graph) return self.output_dir / name + @staticmethod + def _evidence(item): + evidence = item["evidence"] + return json.loads(evidence) if isinstance(evidence, str) else evidence + class TestChoosableCandidates(unittest.TestCase): def _resolution(self, *states): @@ -78,18 +86,24 @@ class TestChoosableCandidates(unittest.TestCase): ) return {"state": "ambiguous", "candidates": candidates} - def test_two_resolved_candidates_are_reviewable(self): - resolution = self._resolution("resolved", "resolved", "unresolved") - self.assertTrue(review_capture.reviewable(resolution)) + def test_one_or_more_resolved_candidates_are_reviewable(self): + for states in ( + ("resolved", "resolved", "unresolved"), + ("resolved", "unresolved"), + ("unresolved", "resolved"), + ): + with self.subTest(states=states): + resolution = self._resolution(*states) + self.assertTrue(review_capture.reviewable(resolution)) - def test_none_or_one_resolved_candidate_is_not_reviewable(self): - for states in ((), ("unresolved",) * 11, ("resolved", "unresolved")): + def test_no_resolved_candidate_is_not_reviewable(self): + for states in ((), ("unresolved",) * 11): with self.subTest(states=states): resolution = self._resolution(*states) self.assertFalse(review_capture.reviewable(resolution)) def test_blank_or_missing_text_does_not_count(self): - resolution = self._resolution("resolved", "resolved") + resolution = self._resolution("resolved", "unresolved") resolution["candidates"][0]["positive"] = {"text": " "} self.assertFalse(review_capture.reviewable(resolution)) resolution["candidates"][0]["positive"] = None @@ -110,18 +124,21 @@ class TestChoosableCandidates(unittest.TestCase): class TestStoredEvidenceOffersChoice(unittest.TestCase): - def test_unlimited_evidence_needs_two_choosable_candidates(self): + def test_unlimited_evidence_needs_one_choosable_candidate(self): side = {"text": "cat"} two = [{"state": "resolved", "positive": side}] * 2 self.assertTrue(evidence_offers_choice({"candidates": two})) one = two[:1] + [{"state": "unresolved", "positive": None}] - self.assertFalse(evidence_offers_choice({"candidates": one})) + self.assertTrue(evidence_offers_choice({"candidates": one})) + none = [{"state": "unresolved", "positive": None}] * 2 + self.assertFalse(evidence_offers_choice({"candidates": none})) - def test_limited_evidence_offers_a_choice_only_with_two_candidates(self): - self.assertTrue( - evidence_offers_choice({"limited": True, "candidate_total": 18}) - ) - for total in (0, 1, None, "18", True): + def test_limited_evidence_offers_a_choice_with_one_candidate(self): + for total in (1, 18): + with self.subTest(total=total): + evidence = {"limited": True, "candidate_total": total} + self.assertTrue(evidence_offers_choice(evidence)) + for total in (0, None, "18", True): with self.subTest(total=total): evidence = {"limited": True, "candidate_total": total} self.assertFalse(evidence_offers_choice(evidence)) @@ -148,16 +165,19 @@ class TestNothingToChooseIsNotQueued(ChoosableTestCase): self.assertEqual(job["counters"]["review"], 1, job["counters"]) self.assertEqual(self._capture_files(), []) - async def test_exactly_one_candidate_with_text_stays_unresolved(self): - """One traced branch beside untraced ones is not certain: no link.""" + async def test_exactly_one_candidate_with_text_is_queued_not_linked(self): + """One traced branch beside untraced ones is not certain: no link, + but it is a choice to confirm (D1), with the other branch listed.""" self._roots() self._write("one.png", opaque_graph("cat", traced=("dog",))) job = await self._scan() - self.assertEqual(self._items(), []) + (item,) = self._items() + states = sorted(c["state"] for c in self._evidence(item)["candidates"]) + self.assertEqual(states, ["resolved", "unresolved"]) (row,) = job["outcomes"]["review"] - self.assertIn("could not be traced", row["detail"]) + self.assertIn("more than one prompt could match", row["detail"]) self.assertEqual(job["counters"].get("added", 0), 0, job["counters"]) self.assertEqual(job["counters"].get("known", 0), 0, job["counters"]) @@ -188,8 +208,7 @@ class TestNothingToChooseIsNotQueued(ChoosableTestCase): def test_recorder_reports_untraced_and_stores_nothing(self): self._roots() self._write("none.png", opaque_graph("cat", "dog")) - self._write("one.png", opaque_graph("cat", traced=("dog",))) - for name in ("none.png", "one.png"): + for name in ("none.png",): with self.subTest(name=name): outcome = review_capture.record_ambiguous_file( self.api.db, self.api._captures, self.output_dir, 0, name @@ -206,6 +225,46 @@ class TestNothingToChooseIsNotQueued(ChoosableTestCase): self.assertEqual(self._items(), []) +class TestOneReadableChoiceIsQueued(ChoosableTestCase): + """#657 addendum D1: the sweep with an LLM enhancer on ``any_01``. + + The inspection profile finds two candidates, one decided at run time + and one readable; the file is queued with both listed, by the scan and + by the monitor, and is never linked. + """ + + def _assert_one_choice(self, item): + evidence = self._evidence(item) + self.assertEqual(evidence["profile"], "inspection") + by_state = {} + for candidate in evidence["candidates"]: + by_state.setdefault(candidate["state"], []).append(candidate) + (readable,) = by_state["resolved"] + self.assertEqual(readable["positive"]["text"], ENHANCED_TEXT) + (runtime,) = by_state["unresolved"] + self.assertEqual(runtime["reason"], "Text is decided at run time") + + async def test_the_scan_queues_it(self): + self._roots() + self._write("sweep.png", enhanced_sweep_graph()) + + job = await self._scan() + + (item,) = self._items() + self._assert_one_choice(item) + self.assertEqual(job["scope"]["review_items"], 1) + self.assertFalse(self.api.db.review_file_linked((str(self.output_dir),))) + + def test_the_monitor_queues_it(self): + self._roots() + path = self._write("sweep.png", enhanced_sweep_graph()) + + self.assertEqual(self.api.record_review_file(str(path)), "recorded") + (item,) = self._items() + self._assert_one_choice(item) + self.assertFalse(self.api.db.review_file_linked((str(path),))) + + class TestUntracedItemsLeaveTheQueue(ChoosableTestCase): async def test_rescan_removes_pending_items_without_a_choice(self): """The maintainer's 39 items: queued before the rule, gone on rescan.""" @@ -220,14 +279,14 @@ class TestUntracedItemsLeaveTheQueue(ChoosableTestCase): job = await self._scan("all") - (item,) = self._items() - self.assertEqual(os.path.basename(item["locator"]), "two.png") - self.assertEqual(self._pending(), 1) + names = sorted(os.path.basename(i["locator"]) for i in self._items()) + self.assertEqual(names, ["one.png", "two.png"]) + self.assertEqual(self._pending(), 2) self.assertGreater(self._review_revision(), revision) - self.assertEqual(job["scope"]["review_untraced"], 2) - self.assertEqual(job["scope"]["review_items"], 1) + self.assertEqual(job["scope"]["review_untraced"], 1) + self.assertEqual(job["scope"]["review_items"], 2) self.assertIn( - "= 2 images left Needs review · nothing to choose", "\n".join(job["log"]) + "= 1 image left Needs review · nothing to choose", "\n".join(job["log"]) ) async def test_decided_items_of_untraced_files_are_never_touched(self): diff --git a/utils/review_capture.py b/utils/review_capture.py index 3eb8bc3f..7f7d118b 100644 --- a/utils/review_capture.py +++ b/utils/review_capture.py @@ -9,10 +9,12 @@ reads the metadata again from the capture, resolves it with the default (ingestion) profile and stores only an ``ambiguous`` result, bound to the capture's byte record. The evidence a reviewer chooses from is therefore the evidence of exactly those bytes. An ambiguous result is stored only when it -is ``reviewable``: at least two candidates carry a resolved prompt text. With -none the file could not be traced; with exactly one, the untraced branches -may still have made the file, so that text is not certain and the file stays -unresolved (the resolver never flattens unknown semantics into a guess). +is ``reviewable``: at least one candidate carries a resolved prompt text +(#657 addendum D1). With none the file could not be traced. With exactly +one, the untraced branches may still have made the file, so the file is +never linked from it, but the user may confirm it: the other branches stay +listed, disabled (the resolver never flattens unknown semantics into a +guess). Nothing here links a file, loads a model or decodes pixels; the capture is released before the database write. @@ -29,8 +31,8 @@ batches): when the default profile is ambiguous without a choice, or calls a file with a prompt graph ``unresolved`` (#659 C1, e.g. "Unsupported latent ancestry"), the scan and the review worker (``inspection=True``) ask ``INSPECTION_PROFILE``, -which knows more encoder and string nodes. With at least two choosable -candidates there, the item is recorded from that profile: its evidence names +which knows more encoder and string nodes. With a choosable +candidate there, the item is recorded from that profile: its evidence names ``profile: "inspection"`` and ``semantics``, the fingerprint of what the table says about the graph's node classes, and its ``resolver_version`` is ``inspection_review_version(metadata)``, which folds the profile,