* test(inspect): use a neutral unknown class in the pass-through fixtures The #381 fixtures used rgthree's Any Switch as their example of an unknown node. The table is about to describe that class (#657), so the stand-in becomes PMTest Unknown Multi and any_switch_graph becomes unknown_multi_graph. Resolver results are unchanged. * feat(inspect): resolve rgthree Any Switch by its first proven input (#657) rgthree's Any Switch outputs the first any_* input that is not None. The inspection resolver treated it as an unknown node, so the model-compare sweep (PromptManagerText into any_01, the same text joined after a LoRA trigger into any_02) was ambiguous in Inspect. The table gains a first-present selector strategy and lists the switch under it. The resolver takes the first participating input when it is a literal or its traced path proves a value (every node is a PromptManager, code-traced, default-adapter or encoders/strings/conditioning/composites class). Otherwise each input is one choice, unless all of them trace to one text; limits and upstream divergences propagate, and no PromptManager path is preferred. A missing or null input does not take part. The browser resolver and its table compiler follow the same rules, with the same strict entry check as the server. Only graphs holding the class change their class projection, so only their pending inspection items move; INSPECTION_RESOLVER_VERSION stays 2. * fix(inspect): list a repeated Any Switch outcome once (#657) A switch with three or more inputs can offer one text through two of them (a, b, a). Each input was its own candidate, so one text read as a choice between two, and two equal resolved candidates beside a failed one would count as reviewable. Like an unknown node passing several texts, a first-present divergence now collapses equal outcomes. The browser resolver does the same. The table compiler test also checks the 128-character prefix bound in code points, as Python counts it. * feat(inspect): show a role every candidate shares (#658) An ambiguous file used to withhold both roles, even when every candidate had the same negative. Inspect now judges each role on its own: a role is shown resolved when every candidate is resolved with the same text, stored recipe and left-out parts for it, using the first candidate's text and source. A role that differs, or any unresolved candidate, keeps the ambiguous line, and resolution.state stays ambiguous, so Send pair, ingestion and Needs review behave as before. The browser-local resolver applies the same rule; the footer Send to ComfyUI still needs both roles. A multistage detailer run whose stages share one negative (fixture f19) now shows that negative. * feat(monitor): ask the ladder for PromptManager runs nothing else links A PromptManager output whose metadata and tracked run cannot name its prompt now takes the ladder path foreign outputs use: decided at settle under the monitor policy, or after its run is sealed through the ladder wait. Wait entries carry their run kind; the kind selects the settings gate (SAVE_FOREIGN_RUNS for foreign entries, MONITORING_ENABLED for PromptManager ones) and the marks of a created prompt. Keyless images, placeholder batch entries and unreadable metadata never enter the wait. Every final unlinked outcome hands the file to the review recorder. Refs #659 * test(monitor): pin the final unlinked outcomes that reach Needs review The sweep, joined-text and shared-prefix graphs move to a shared fixture module. New tests pin when the monitor hands a file to its review recorder: after the sealed re-check, a settle lookup that found the run's record, a file outside ComfyUI's output folder and the keyless re-check; never before the run is sealed, and never for a linked file. A PromptManager output waiting for its run now logs that at INFO. Refs #659 * feat(review): offer inspection choices for default-unresolved files Decision.default_unresolved marks a file the default profile itself calls unresolved (a blank positive does not count). With inspection on, record_ambiguous_file now sends such a captured file with a prompt graph straight to the inspection-profile branch: a reviewable ambiguous or saver-anchored result is recorded, anything else is untraced. Apply is unchanged; it re-resolves with the item's recorded profile. Refs #659 * feat(review): one review dispatch and a review worker for monitor outputs review_dispatch holds the scan's per-file Needs review decision and is now also used by the monitor. The scan replaces its inline dispatch with it (and offers default-unresolved files their inspection choices). record_review_file runs the scan's per-file steps for one file, read only: the scan-policy ladder with the sealed history and the recorded-run check. A file the scan would link is skipped; otherwise the dispatch's record outcome is the answer. The monitor queues paths through queue_review_file onto one daemon worker (pm-review-recorder) with a bounded queue that coalesces by path and a bounded deferred set it replays once no job blocker remains. The package teardown stops it right after the monitor and reports it as part review once started. Refs #659 * docs(testing): the monitor queues what the scan would (#659) Rewrite the Needs review paragraph for the 2026-11-19 decision: the final-outcome points, the review worker and its bounds, the deferred replay and the default-unresolved rule. Document the PromptManager-run ladder path, the review step of the package teardown and the proven-value allowlist every future semantics table entry must keep. Refs #659 * fix(review): lock the untraced memory the scan and review worker share Refs #659 * feat(inspect): name the file's Needs review item in the inspection responses Inspect and the run-history current-file inspection gain a review field: {"state": "pending", "item_id": ...} for the newest pending item at the stat the inspection took, {"state": "decided"} once its item was decided, and null otherwise. It is read only when a role is ambiguous and the file is unlinked, by locator and stat (review_item_at), so Inspect never hashes the file. A LoRA root, a failed read and any malformed value give null. Refs #659 * feat(review): a single-item mode for the review controller and a shared option list createReviewController({api, itemId}) holds one named item: it never reads the queue, loads that item's detail, ends in phase decided (decided: true) after a recorded decision or for an item already decided, and re-reads the detail after a review-conflict, review-missing or restore refusal. The queue mode is unchanged. The Sampler context option list moves into renderReviewOptions (review-options.mjs, re-exported by review.mjs), so Needs review and the Inspect picker render the same options without Inspect importing the Needs review page. Refs #659 * feat(inspect): choose the prompt of an ambiguous file in Inspect When the inspection names the file's pending Needs review item, the inspector shows a Choose prompt section after its prompt blocks (Images panel, viewer details, recorded run current file). It reads the item's detail through the review controller's single-item mode: one Sampler context option per candidate (unusable ones disabled with their reason), the chosen option's prompt blocks with Copy and Send, Use this prompt and Leave unresolved over the Needs review apply flow. A recorded decision shows Needs review's toast and re-reads the inspection, so the file shows its link; an item with nothing to choose offers Open in Needs review. Refs #659 * fix(inspect): read the history review item only when Inspect asks The run-history reader locates the file under the scan roots inside the read, so an unambiguous or linked file costs no realpath of the output folders. The docstring names the linked-subfolder limit, and the newest pending item test pins the rowid tie-break. Refs #659 * fix(inspect): end Choose prompt on another read and on a decision made elsewhere The inspector disposes the section when it reads another file, fails or defers, so a decision in flight never re-reads the next file. A decided item hides the section and re-reads the inspection once, also when it was decided in another tab or in Needs review. Refs #659 * test(inspect): the rooted inspect shape gains the review facet Refs #659 * docs(testing): first-present candidates and per-role results (#657, #658) * test(review): keep the review cases ambiguous now that Any Switch resolves Part 1 resolves rgthree's Any Switch, so the sweep fixture links. The review and monitor tests that need a default-unresolved, inspection-ambiguous file now use ambiguous_sweep_graph(): Part A's any_switch_sweep_graph() with its switch class replaced by the never-listed PMTest Unknown Multi (one sweep graph instead of two). A new test pins that the real sweep links at settle from the inspector. Refs #657 #659 * fix(review): start the review worker again after an unexpected error * fix(inspect): pass the inspection refresh to Choose prompt by reference * 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. * feat(review): collapse too many candidates into the most used prompts (#657) More than 16 distinct candidates no longer make a limited item without choices. The choosable ones are grouped by their positive and negative texts, ordered by group size then first appearance, and the first 16 are kept, one real candidate each. The evidence names collapsed: {shown, total, paths} and the item detail returns it. Limited stays for evidence that cannot be projected or is over 8 MiB. REVIEW_EVIDENCE_VERSION is folded into every item version (default items now carry RESOLVER_VERSION + 1, inspection items fold it into their seed), so the next scan supersedes pending, undecided items of the old shape; decided items are kept. RESOLVER_VERSION, the ingestion's provenance version, is unchanged. * feat(inspect): record the Needs review item when Inspect finds none (#657) When Inspect asks for the review facet (an ambiguous role, an unlinked file under an output root) and no item exists at the inspected stat, the images and History current-file inspections run record_review_file, the review worker's own per-file path, in the executor and read the item again. A file the scan would link or that offers nothing to choose records nothing; a deferred or failed recording leaves review null and a failure is logged, never failing the response. * docs(testing): one readable choice, collapsed candidates, Inspect records (#657) * feat(review): say when a file's prompts were shortened to the most used (#657) The item detail now passes the stored evidence's optional `collapsed` counts ({shown, total, paths}) through to the page, null when absent, malformed, or when the detail was capped for size. Needs review and Inspect's Choose prompt show one line under Sampler context: "Showing the n most used of t prompts in this file." * feat(review): list the image's own prompts first in Link to a prompt (#657) With an empty search the dialog now lists the item's choosable candidates under "From this image" (positive / negative, one per prompt pair), then the Library's prompts under "Library". Choosing one of the image's prompts and pressing Link image applies that candidate by its hash, the same decision as Apply association (new controller.applyCandidate); no negative is asked for, the candidate carries its own. Typing searches the Library only. Items with no choosable candidate show the dialog as before. A refusal stays in the dialog with the server's sentence. * feat(review): load more of the queue on scroll (#657) The Needs review continuation now works like the Images page: an IntersectionObserver (250 px ahead) loads the next page when the end of the queue scrolls into view, and the line reads "40 of 45 images · more load as you scroll" without a button. Nothing loads behind an open dialog or the viewer; when one closes, focus returning to the page checks again. A failed page shows the failure line and a Retry button. Without IntersectionObserver the Load more images button stays. * fix(review): ask the inspection profile when the default choice is partial (#657) With one readable choice enough, a default result with one choice beside unresolved branches was recorded as is, so the inspection profile, which may read those branches (a Qwen image-edit sampler), was never asked and a rescan replaced older inspection items with the poorer default ones. Such a partial result now yields to the inspection result when that offers more choosable candidates. * fix(review): collapse only too many prompts, and apply any offered one (#657) Collapsing triggered on more than 16 distinct candidates, sampler ids included, so twenty samplers sharing three texts lost their run-time branches behind a collapsed note. Now only more than 16 choosable texts collapse; otherwise one candidate per text is kept and the branches without a prompt fill the remaining places, as an ordinary item. Apply's still-offered check compared the chosen hash with a fresh collapse, so a reorder that changed the representatives refused Apply. It now checks every projected, de-duplicated candidate hash (candidate_hashes). * fix(inspect): keep Inspect's recording quiet, bounded and current (#657) A busy capture store (the worker copying a fresh output) is an expected outcome: Inspect logs it at debug level and reads the item once more, also after a failed recording. A file Inspect found unrecordable (the scan would link it, or it offers nothing to choose) is remembered by its locator, stat and review versions, at most 4,096 files, so a repeated Inspect does not copy it again. A pending item of an older item version is recorded again, so the current record supersedes it. * docs(testing): partial default choices, bounded candidates, Inspect recording (#657) * fix(review): Link to a prompt drops an image choice it no longer shows (#657) Typing in the search box hid the "From this image" list but kept its chosen prompt, so Link image applied a candidate the reviewer could no longer see. Typing now drops that choice; Link image waits for a Library prompt and sends a Library link. Emptying the box lists the image's prompts again, none chosen. When the item's detail is read again while the dialog is open, the list follows what it offers now; a chosen prompt no longer offered is dropped with "This prompt is no longer offered for this file. Choose again." applyCandidate says the same when the hash cannot be chosen, and an unanswered apply from the dialog asks to press Link image again. * fix(review): keep the panel in place when Info brings it into view at one column (#657) At one column, Info scrolls the panel into view past the queue, which could bring the continuation into view and load the next page, pushing the panel away. As on the Images page, loading now waits for the reader's next wheel, touch move or key press. * docs(review): say why the review worker's recorder is a lambda * test(review): open the dialog of the D5 dialog test beside the queue (#657) At one column (touch) the review panel follows the queue just above the continuation, so scrolling Link to a prompt… into view brings the continuation within the observer's 250 px margin. The next page then loads before the dialog opens whenever the observer's callback runs first (CI shard 4/4, touch), and the test found no continuation to scroll to. That load is the page working as designed: the reader has reached the end of the queue. The test is about loading behind an open dialog, so it now runs at two columns, where Link sits in the sticky panel beside the queue, and it checks that the continuation is out of range before the dialog opens.
1370 lines
55 KiB
Python
1370 lines
55 KiB
Python
"""Needs review items (#249 E2): capture-bound ambiguous evidence.
|
|
|
|
The scan and the live monitor persist one review item per ambiguous file:
|
|
the file is captured, its metadata is read again from the capture, the
|
|
default (ingestion) resolver runs on it, and only an ``ambiguous`` result is
|
|
stored, bound to the capture's byte record. Statuses, counters and links of
|
|
both producers stay what they were.
|
|
"""
|
|
|
|
import asyncio
|
|
import hashlib
|
|
import json
|
|
import logging
|
|
import contextlib
|
|
import os
|
|
import sqlite3
|
|
import unittest
|
|
from pathlib import Path
|
|
from unittest.mock import patch
|
|
|
|
try:
|
|
from tests.open_handles import assert_closed
|
|
from tests.test_api_admin import AdminAPITestCase, _sse_payloads
|
|
except ImportError: # discovered with tests/ as the top-level directory
|
|
from open_handles import assert_closed
|
|
from test_api_admin import AdminAPITestCase, _sse_payloads
|
|
|
|
from PIL import Image, PngImagePlugin
|
|
|
|
from tests.background_stops import close_handler, stop_monitor
|
|
from database.operations import PromptDatabase
|
|
from database.review_schema import canonical_evidence, evidence_digest
|
|
from py.api import PromptManagerAPI
|
|
from py.config import GalleryConfig
|
|
from utils.hashing import generate_prompt_hash
|
|
from utils.image_monitor import ImageGenerationHandler
|
|
from utils.media_capture import CaptureCapacity, CaptureError, CaptureStore
|
|
from utils.prompt_tracker import PromptTracker
|
|
|
|
try: # guarded so a missing module fails assertions, not test loading
|
|
from utils import review_capture
|
|
except ImportError:
|
|
review_capture = None
|
|
|
|
NOW = "2026-10-22T00:00:00+00:00"
|
|
# Two negative prompt delimiters: the A1111 record is ambiguous.
|
|
DELIMITERS = (
|
|
"cat\nNegative prompt: blur\nNegative prompt: noise\nSteps: 20, Sampler: Euler"
|
|
)
|
|
|
|
|
|
def _sampler(positive, negative="bad"):
|
|
return {"positive": positive, "negative": negative}
|
|
|
|
|
|
def ambiguous_graph(*positives):
|
|
"""One KSampler per positive text, each with its own encoder: ambiguous."""
|
|
graph = {
|
|
"1": {"class_type": "CheckpointLoaderSimple", "inputs": {"ckpt_name": "m"}},
|
|
"2": {"class_type": "CLIPTextEncode", "inputs": {"text": "bad"}},
|
|
}
|
|
for index, text in enumerate(positives or ("cat", "dog")):
|
|
encoder, sampler = str(10 + 2 * index), str(11 + 2 * index)
|
|
graph[encoder] = {"class_type": "CLIPTextEncode", "inputs": {"text": text}}
|
|
graph[sampler] = {
|
|
"class_type": "KSampler",
|
|
"inputs": {
|
|
"model": ["1", 0],
|
|
"positive": [encoder, 0],
|
|
"negative": ["2", 0],
|
|
},
|
|
}
|
|
return graph
|
|
|
|
|
|
def unlinkable_graph(*positives):
|
|
"""Ambiguous, and the scan's browser-parser fallback links nothing.
|
|
|
|
Only such a file is queued (an ambiguous file the fallback links is not).
|
|
The parser reads the first sampler (node 3), whose positive input points
|
|
at a node missing from the graph; the resolver offers the other samplers
|
|
as candidates beside that unresolved one.
|
|
"""
|
|
graph = ambiguous_graph(*positives)
|
|
graph["3"] = {
|
|
"class_type": "KSampler",
|
|
"inputs": {"model": ["1", 0], "positive": ["99", 0], "negative": ["2", 0]},
|
|
}
|
|
return graph
|
|
|
|
|
|
def resolved_texts(evidence):
|
|
"""The positive texts of the candidates that resolved, sorted."""
|
|
return sorted(
|
|
c["positive"]["text"] for c in evidence["candidates"] if c["positive"]
|
|
)
|
|
|
|
|
|
def resolved_graph(text="a red square"):
|
|
return ambiguous_graph(text)
|
|
|
|
|
|
def write_png(path, *, prompt=None, parameters=None, color=(255, 0, 0)):
|
|
info = PngImagePlugin.PngInfo()
|
|
if prompt is not None:
|
|
info.add_text("prompt", json.dumps(prompt))
|
|
if parameters is not None:
|
|
info.add_text("parameters", parameters)
|
|
Image.new("RGB", (2, 2), color).save(str(path), pnginfo=info)
|
|
|
|
|
|
def _reject_constant(name):
|
|
raise ValueError(f"non-strict JSON constant {name}")
|
|
|
|
|
|
def digest(path):
|
|
return hashlib.sha256(Path(path).read_bytes()).hexdigest()
|
|
|
|
|
|
# Ceiling of every wait for the review worker (TESTING.md: 30 s)
|
|
WAIT_SECONDS = 30
|
|
|
|
|
|
class Recorded:
|
|
"""Wrap ``api.record_review_file`` (called by the review worker, #659)
|
|
and let a test wait for its calls without sleeping."""
|
|
|
|
def __init__(self, testcase, api, side_effect=None):
|
|
import threading
|
|
|
|
self._real = api.record_review_file
|
|
self._side_effect = side_effect
|
|
self._changed = threading.Condition()
|
|
self.paths, self.outcomes = [], []
|
|
patcher = patch.object(api, "record_review_file", self)
|
|
patcher.start()
|
|
testcase.addCleanup(patcher.stop)
|
|
|
|
def __call__(self, image_path):
|
|
try:
|
|
if self._side_effect is not None:
|
|
outcome = self._side_effect(image_path)
|
|
else:
|
|
outcome = self._real(image_path)
|
|
except Exception:
|
|
outcome = "raised"
|
|
raise
|
|
finally:
|
|
with self._changed:
|
|
self.paths.append(os.path.basename(str(image_path)))
|
|
self.outcomes.append(outcome)
|
|
self._changed.notify_all()
|
|
return outcome
|
|
|
|
def reset(self):
|
|
with self._changed:
|
|
self.paths, self.outcomes = [], []
|
|
|
|
def wait_blocking(self, count):
|
|
with self._changed:
|
|
return self._changed.wait_for(
|
|
lambda: len(self.outcomes) >= count, WAIT_SECONDS
|
|
)
|
|
|
|
async def wait_for(self, count):
|
|
reached = await asyncio.to_thread(self.wait_blocking, count)
|
|
if not reached:
|
|
raise AssertionError(f"the review worker made {self.outcomes}")
|
|
|
|
|
|
class ReviewTestCase(AdminAPITestCase):
|
|
"""Scan helpers over a capture store kept inside the test directory."""
|
|
|
|
async def get_application(self):
|
|
app = await super().get_application()
|
|
self.capture_parent = Path(self.tmpdir) / "captures"
|
|
self.api._captures = CaptureStore(temp_parent=self.capture_parent)
|
|
return app
|
|
|
|
async def tearDownAsync(self):
|
|
# The review worker (#659) ends before its store and folder go.
|
|
self.assertTrue(self.api.stop_review_worker(30), "review worker running")
|
|
self.api.close_annotation_captures()
|
|
await super().tearDownAsync()
|
|
|
|
def _roots(self):
|
|
GalleryConfig.MONITORING_DIRECTORIES = [self._root(self.output_dir)]
|
|
|
|
async def _scan(self, scope="new"):
|
|
resp = await self.client.request(
|
|
"POST", "/prompt_manager/jobs/scan", json={"scope": scope}
|
|
)
|
|
data = await resp.json()
|
|
self.assertEqual(resp.status, 202, data)
|
|
await asyncio.wait_for(self.api._scan_job.task, 20)
|
|
detail = await self.client.request(
|
|
"GET", f"/prompt_manager/jobs/{data['job']['id']}"
|
|
)
|
|
return (await detail.json())["job"]
|
|
|
|
def _items(self):
|
|
conn = self.api.db.model.get_connection()
|
|
return [
|
|
dict(row)
|
|
for row in conn.execute(
|
|
"SELECT i.*, b.sha256, b.size, b.mtime_ns, f.locator "
|
|
"FROM media_review_items i "
|
|
"JOIN media_annotation_bytes b ON b.id=i.byte_id "
|
|
"JOIN media_annotation_files f ON f.id=b.file_id "
|
|
"ORDER BY i.created_at, i.id"
|
|
)
|
|
]
|
|
|
|
def _items_at(self, path):
|
|
locator = os.path.normcase(os.path.realpath(str(path)))
|
|
return [item for item in self._items() if item["locator"] == locator]
|
|
|
|
def _review_revision(self):
|
|
conn = self.api.db.model.get_connection()
|
|
return conn.execute(
|
|
"SELECT review_index_revision FROM media_review_metadata"
|
|
).fetchone()[0]
|
|
|
|
def _capture_files(self):
|
|
if not self.capture_parent.exists():
|
|
return []
|
|
return [p for p in self.capture_parent.rglob("*") if p.is_file()]
|
|
|
|
def _require_module(self):
|
|
self.assertIsNotNone(review_capture, "utils.review_capture is not implemented")
|
|
|
|
def _scan_record(self, path):
|
|
"""Record ``path`` as the scan's recorder does (only scans add items)."""
|
|
from py.api.admin import _scan_roots
|
|
|
|
roots = _scan_roots(self.api._get_all_output_dirs())
|
|
return self.api._record_review_sync(Path(str(path)), roots, inspection=True)
|
|
|
|
|
|
class TestScanRecording(ReviewTestCase):
|
|
async def test_scan_persists_only_ambiguous_files_with_capture_digest(self):
|
|
self._require_module()
|
|
self._roots()
|
|
ambiguous = self.output_dir / "sub" / "two.png"
|
|
ambiguous.parent.mkdir()
|
|
write_png(ambiguous, prompt=unlinkable_graph("cat", "dog"))
|
|
# Ambiguous too, but the browser-parser fallback links it: not queued.
|
|
write_png(self.output_dir / "linked.png", prompt=ambiguous_graph("a", "b"))
|
|
write_png(self.output_dir / "resolved.png", prompt=resolved_graph())
|
|
write_png(self.output_dir / "plain.png")
|
|
write_png(
|
|
self.output_dir / "a1111.png",
|
|
parameters="cat\nNegative prompt: blur\nSteps: 20, Sampler: Euler",
|
|
)
|
|
|
|
job = await self._scan()
|
|
|
|
items = self._items()
|
|
self.assertEqual(len(items), 1, items)
|
|
item = items[0]
|
|
self.assertEqual(item["sha256"], digest(ambiguous))
|
|
self.assertEqual(item["size"], ambiguous.stat().st_size)
|
|
self.assertEqual(item["mtime_ns"], ambiguous.stat().st_mtime_ns)
|
|
self.assertEqual(
|
|
item["locator"], os.path.normcase(os.path.realpath(str(ambiguous)))
|
|
)
|
|
self.assertEqual(
|
|
(item["state"], item["revision"], item["limited"]), ("pending", 0, 0)
|
|
)
|
|
self.assertEqual(
|
|
item["resolver_version"], review_capture.default_item_version()
|
|
)
|
|
self.assertEqual(item["candidate_count"], 3)
|
|
self.assertEqual(item["evidence_hash"], evidence_digest(item["evidence"]))
|
|
evidence = json.loads(item["evidence"])
|
|
self.assertEqual(canonical_evidence(evidence), item["evidence"])
|
|
self.assertEqual(
|
|
evidence["resolver_version"], review_capture.default_item_version()
|
|
)
|
|
self.assertEqual(resolved_texts(evidence), ["cat", "dog"])
|
|
for candidate in evidence["candidates"]:
|
|
self.assertEqual(
|
|
set(candidate),
|
|
{"hash", "sampler_id", "state", "reason", "positive", "negative"},
|
|
)
|
|
if candidate["positive"] is None: # the parser's unresolved sampler
|
|
continue
|
|
self.assertEqual(
|
|
set(candidate["positive"]), {"text", "storage_text", "provenance"}
|
|
)
|
|
self.assertEqual(candidate["negative"]["text"], "bad")
|
|
body = {k: v for k, v in candidate.items() if k != "hash"}
|
|
self.assertEqual(
|
|
candidate["hash"],
|
|
hashlib.sha256(canonical_evidence(body).encode()).hexdigest(),
|
|
)
|
|
self.assertEqual(self._review_revision(), 1)
|
|
self.assertEqual(job["scope"]["review_items"], 1)
|
|
self.assertIn("? 1 image added to Needs review", "\n".join(job["log"]))
|
|
self.assertEqual(self._capture_files(), [])
|
|
self.assertEqual(self.api._captures.stats()["captures"], 0)
|
|
|
|
async def test_scan_statuses_counters_and_outcome_rows_unchanged(self):
|
|
self._require_module()
|
|
self._roots()
|
|
write_png(self.output_dir / "two.png", prompt=unlinkable_graph("cat", "dog"))
|
|
write_png(self.output_dir / "one.png", prompt=resolved_graph())
|
|
write_png(
|
|
self.output_dir / "delims.png",
|
|
parameters=DELIMITERS,
|
|
)
|
|
write_png(self.output_dir / "none.png")
|
|
files = sorted(self.output_dir.iterdir())
|
|
roots = self._scan_roots()
|
|
|
|
def run(api, recorder=None):
|
|
batch = api._extract_batch_metadata_sync(files)
|
|
outcomes = []
|
|
if recorder is None:
|
|
counts = api._ingest_scan_batch_sync(batch, roots, outcomes)
|
|
else:
|
|
from py.api import review_recording
|
|
|
|
with patch.object(review_recording, "record_ambiguous_file", recorder):
|
|
counts = api._ingest_scan_batch_sync(batch, roots, outcomes)
|
|
prompts = api.db.get_recent_prompts()["prompts"]
|
|
return counts, outcomes, sorted(p["text"] for p in prompts)
|
|
|
|
other_db = PromptDatabase(os.path.join(self.tmpdir, "other.db"))
|
|
other = PromptManagerAPI(db=other_db)
|
|
|
|
def broken(*args, **kwargs):
|
|
raise RuntimeError("recording failed")
|
|
|
|
recorded = run(self.api)
|
|
with self.assertLogs("prompt_manager.api", logging.WARNING) as logs:
|
|
without = run(other, broken)
|
|
self.assertEqual(recorded, without)
|
|
# delims.png offers nothing to choose: unresolved, not queued.
|
|
self.assertEqual(recorded[0]["ambiguous"], 1)
|
|
self.assertEqual(recorded[0]["unresolved"], 1)
|
|
self.assertEqual(recorded[0]["added"], 1)
|
|
self.assertTrue(any("Needs review" in line for line in logs.output))
|
|
self.assertEqual(len(self._items()), 1)
|
|
other.close_annotation_captures()
|
|
other_db.close_all()
|
|
|
|
def _scan_roots(self):
|
|
from py.api.admin import _scan_roots
|
|
|
|
return _scan_roots(self.api._get_all_output_dirs())
|
|
|
|
async def test_new_scope_rereads_ambiguous_files_and_stays_idempotent(self):
|
|
self._require_module()
|
|
self._roots()
|
|
write_png(self.output_dir / "two.png", prompt=unlinkable_graph("cat", "dog"))
|
|
write_png(self.output_dir / "one.png", prompt=resolved_graph())
|
|
first = await self._scan("new")
|
|
revision = self._review_revision()
|
|
reads = []
|
|
original = self.api._extract_comfyui_metadata
|
|
|
|
def spy(path):
|
|
reads.append(os.path.basename(path))
|
|
return original(path)
|
|
|
|
self.api._extract_comfyui_metadata = spy
|
|
second = await self._scan("new")
|
|
|
|
self.assertEqual(reads, ["two.png"])
|
|
self.assertEqual(first["scope"]["review_items"], 1)
|
|
# Still in the queue (review_items), but nothing new was added.
|
|
self.assertEqual(second["scope"]["review_items"], 1)
|
|
self.assertEqual(second["scope"]["review_added"], 0)
|
|
self.assertNotIn("added to Needs review", "\n".join(second["log"]))
|
|
self.assertIn("? 1 image already in Needs review", "\n".join(second["log"]))
|
|
self.assertEqual(len(self._items()), 1)
|
|
self.assertEqual(self._review_revision(), revision)
|
|
self.assertEqual(self._capture_files(), [])
|
|
|
|
async def test_queued_fingerprint_skips_the_copy_and_hash(self):
|
|
"""A rescan of a queued file whose size and mtime match its item's
|
|
verified fingerprint neither copies nor hashes it, and the file stays
|
|
in the default "new" scope (it is still read like any unknown file)."""
|
|
self._require_module()
|
|
self._roots()
|
|
path = self.output_dir / "two.png"
|
|
write_png(path, prompt=unlinkable_graph("cat", "dog"))
|
|
await self._scan("new")
|
|
captures = []
|
|
original = CaptureStore.capture
|
|
|
|
def spy(store, root, relative, **kwargs):
|
|
captures.append(relative)
|
|
return original(store, root, relative, **kwargs)
|
|
|
|
reads = []
|
|
extract = self.api._extract_comfyui_metadata
|
|
|
|
def read_spy(file_path):
|
|
reads.append(os.path.basename(file_path))
|
|
return extract(file_path)
|
|
|
|
self.api._extract_comfyui_metadata = read_spy
|
|
with patch.object(CaptureStore, "capture", spy):
|
|
second = await self._scan("new")
|
|
self.assertEqual(captures, [])
|
|
self.assertEqual(reads, ["two.png"])
|
|
self.assertEqual(second["scope"]["review_added"], 0)
|
|
self.assertEqual(second["scope"]["review_items"], 1)
|
|
# A touched file (same bytes, new mtime) is captured again: a
|
|
# duplicate of its item, whose fingerprint is refreshed.
|
|
stat = path.stat()
|
|
os.utime(path, ns=(stat.st_atime_ns, stat.st_mtime_ns + 10**9))
|
|
await self._scan("new")
|
|
self.assertEqual(captures, ["two.png"])
|
|
items = self._items()
|
|
self.assertEqual(len(items), 1)
|
|
self.assertEqual(items[0]["mtime_ns"], path.stat().st_mtime_ns)
|
|
self.assertEqual(self._capture_files(), [])
|
|
|
|
async def test_repeat_scan_keeps_decisions(self):
|
|
self._require_module()
|
|
self._roots()
|
|
write_png(self.output_dir / "two.png", prompt=unlinkable_graph("cat", "dog"))
|
|
await self._scan("all")
|
|
item = self._items()[0]
|
|
candidate = json.loads(item["evidence"])["candidates"][0]["hash"]
|
|
conn = self.api.db.model.get_connection()
|
|
with conn:
|
|
conn.execute(
|
|
"UPDATE media_review_items SET state='selected', revision=1 WHERE id=?",
|
|
(item["id"],),
|
|
)
|
|
conn.execute(
|
|
"INSERT INTO media_review_decisions(id,item_id,decision_revision,"
|
|
"created_at,"
|
|
"outcome,candidate_hash,evidence) VALUES(?,?,?,?,?,?,?)",
|
|
(
|
|
"00000000-0000-4000-8000-0000000000d1",
|
|
item["id"],
|
|
1,
|
|
NOW,
|
|
"selected",
|
|
candidate,
|
|
'{"kind":"user-selection"}',
|
|
),
|
|
)
|
|
|
|
job = await self._scan("all")
|
|
|
|
items = self._items()
|
|
self.assertEqual(len(items), 1)
|
|
self.assertEqual((items[0]["state"], items[0]["revision"]), ("selected", 1))
|
|
self.assertEqual(items[0]["updated_at"], item["updated_at"])
|
|
decisions = conn.execute("SELECT id FROM media_review_decisions").fetchall()
|
|
self.assertEqual(len(decisions), 1)
|
|
self.assertEqual(job["scope"]["review_items"], 0)
|
|
|
|
async def test_changed_bytes_create_new_pending_item_and_keep_old(self):
|
|
self._require_module()
|
|
self._roots()
|
|
path = self.output_dir / "two.png"
|
|
write_png(path, prompt=unlinkable_graph("cat", "dog"))
|
|
await self._scan()
|
|
old = self._items()[0]
|
|
write_png(path, prompt=unlinkable_graph("cat", "dog"), color=(0, 0, 255))
|
|
self.assertNotEqual(digest(path), old["sha256"])
|
|
|
|
job = await self._scan()
|
|
|
|
items = self._items()
|
|
self.assertEqual(len(items), 2)
|
|
self.assertEqual(items[0]["id"], old["id"])
|
|
self.assertEqual(items[0]["sha256"], old["sha256"])
|
|
self.assertEqual(items[1]["sha256"], digest(path))
|
|
self.assertEqual(items[1]["state"], "pending")
|
|
self.assertEqual(items[0]["locator"], items[1]["locator"])
|
|
self.assertEqual(job["scope"]["review_items"], 1)
|
|
|
|
async def test_many_candidates_are_collapsed(self):
|
|
"""Only ``many.png`` is queued: the A1111 delimiter ambiguity offers
|
|
no candidate to choose (``tests/test_review_choosable.py``). Its 17
|
|
prompts are more than 16: the 16 most used are kept (#657 D3,
|
|
``tests/test_review_collapsed.py``)."""
|
|
self._require_module()
|
|
self._roots()
|
|
write_png(
|
|
self.output_dir / "delims.png",
|
|
parameters=DELIMITERS,
|
|
)
|
|
many = [f"prompt {n}" for n in range(17)]
|
|
write_png(self.output_dir / "many.png", prompt=unlinkable_graph(*many))
|
|
|
|
job = await self._scan()
|
|
|
|
items = {Path(item["locator"]).name: item for item in self._items()}
|
|
self.assertEqual(set(items), {"many.png"})
|
|
item = items["many.png"]
|
|
self.assertEqual((item["limited"], item["candidate_count"]), (0, 16))
|
|
evidence = json.loads(item["evidence"])
|
|
self.assertNotIn("limited", evidence)
|
|
self.assertEqual(
|
|
# 17 samplers and the parser's unresolved one.
|
|
evidence["collapsed"],
|
|
{"shown": 16, "total": 17, "paths": 18},
|
|
)
|
|
self.assertEqual(job["scope"]["review_items"], 1)
|
|
|
|
async def test_capacity_errors_are_logged_and_counted_not_fatal(self):
|
|
self._require_module()
|
|
self._roots()
|
|
write_png(self.output_dir / "two.png", prompt=unlinkable_graph("cat", "dog"))
|
|
|
|
def full(*args, **kwargs):
|
|
raise CaptureCapacity("Too many captures are in progress")
|
|
|
|
with patch.object(self.api._captures, "capture", side_effect=full):
|
|
job = await self._scan()
|
|
|
|
self.assertEqual(job["state"], "finished")
|
|
# Nothing links this ambiguous file: Activity counts it under review.
|
|
self.assertEqual(job["counters"]["review"], 1)
|
|
self.assertEqual(job["scope"]["review_items"], 0)
|
|
self.assertEqual(job["scope"]["review_errors"], 1)
|
|
self.assertIn(
|
|
"! 1 image could not be added to Needs review", "\n".join(job["log"])
|
|
)
|
|
self.assertEqual(self._items(), [])
|
|
|
|
async def test_legacy_scan_events_are_unchanged(self):
|
|
self._require_module()
|
|
self._roots()
|
|
write_png(self.output_dir / "two.png", prompt=unlinkable_graph("cat", "dog"))
|
|
resp = await self.client.request("POST", "/prompt_manager/scan")
|
|
payloads = _sse_payloads(await resp.text())
|
|
final = payloads[-1]
|
|
self.assertEqual(final["type"], "complete")
|
|
self.assertNotIn("review_items", final)
|
|
self.assertEqual(len(self._items()), 1)
|
|
|
|
|
|
class TestNonFiniteGraphs(ReviewTestCase):
|
|
"""ComfyUI stamps NaN/Infinity into graphs; review stores them as null."""
|
|
|
|
@staticmethod
|
|
def _nan_stamped(*positives):
|
|
graph = ambiguous_graph(*positives)
|
|
for node in graph.values():
|
|
node["is_changed"] = [float("nan")]
|
|
if node["class_type"] == "KSampler":
|
|
node["inputs"].update({"cfg": float("inf"), "denoise": float("-inf")})
|
|
return graph
|
|
|
|
async def test_scan_records_a_nan_stamped_ambiguous_graph(self):
|
|
self._require_module()
|
|
self._roots()
|
|
path = self.output_dir / "nan.png"
|
|
# json.dumps' default writes the bare NaN/Infinity literals, as ComfyUI.
|
|
write_png(path, prompt=self._nan_stamped("cat NaN", "dog Infinity"))
|
|
self.assertIn(b"NaN", path.read_bytes())
|
|
|
|
job = await self._scan()
|
|
|
|
self.assertEqual(job["scope"]["review_items"], 1, job)
|
|
(item,) = self._items()
|
|
self.assertEqual((item["limited"], item["candidate_count"]), (0, 2))
|
|
evidence = json.loads(item["evidence"], parse_constant=_reject_constant)
|
|
self.assertEqual(canonical_evidence(evidence), item["evidence"])
|
|
self.assertEqual(
|
|
sorted(c["positive"]["text"] for c in evidence["candidates"]),
|
|
["cat NaN", "dog Infinity"],
|
|
)
|
|
|
|
async def test_scan_recorder_records_a_nan_stamped_graph(self):
|
|
self._require_module()
|
|
self._roots()
|
|
path = self.output_dir / "nan.png"
|
|
write_png(path, prompt=self._nan_stamped("cat", "dog"))
|
|
|
|
self.assertEqual(self._scan_record(path), "recorded")
|
|
(item,) = self._items()
|
|
self.assertEqual(item["candidate_count"], 2)
|
|
|
|
def test_canonical_evidence_writes_non_finite_numbers_as_null(self):
|
|
value = {"b": [float("nan"), float("inf")], "a": -float("inf"), "t": "NaN"}
|
|
text = canonical_evidence(value)
|
|
self.assertEqual(text, '{"a":null,"b":[null,null],"t":"NaN"}')
|
|
self.assertEqual(canonical_evidence(json.loads(text)), text)
|
|
|
|
|
|
class TestRecordAmbiguousFile(ReviewTestCase):
|
|
def _record(self, relative):
|
|
return review_capture.record_ambiguous_file(
|
|
self.api.db, self.api._captures, self.output_dir, 0, relative
|
|
)
|
|
|
|
async def test_record_outcomes(self):
|
|
self._require_module()
|
|
write_png(self.output_dir / "two.png", prompt=ambiguous_graph("cat", "dog"))
|
|
write_png(self.output_dir / "one.png", prompt=resolved_graph())
|
|
write_png(
|
|
self.output_dir / "delims.png",
|
|
parameters=DELIMITERS,
|
|
)
|
|
self.assertEqual(self._record("two.png"), "recorded")
|
|
self.assertEqual(self._record("two.png"), "duplicate")
|
|
self.assertEqual(self._record("one.png"), "skipped")
|
|
self.assertEqual(self._record("missing.png"), "skipped")
|
|
self.assertEqual(self._record("delims.png"), "untraced")
|
|
many = [f"prompt {n}" for n in range(17)]
|
|
write_png(self.output_dir / "many.png", prompt=unlinkable_graph(*many))
|
|
self.assertEqual(self._record("many.png"), "recorded") # collapsed (D3)
|
|
self.assertEqual(self._record("many.png"), "duplicate")
|
|
self.assertEqual(self._review_revision(), 2)
|
|
|
|
async def test_restore_between_capture_and_write_skips(self):
|
|
self._require_module()
|
|
write_png(self.output_dir / "two.png", prompt=ambiguous_graph("cat", "dog"))
|
|
with patch.object(
|
|
self.api.db, "get_execution_epoch", return_value="stale-epoch"
|
|
):
|
|
self.assertEqual(self._record("two.png"), "skipped")
|
|
self.assertEqual(self._items(), [])
|
|
|
|
async def test_oversize_evidence_is_limited(self):
|
|
self._require_module()
|
|
big = "x" * (4 * 1024 * 1024 + 10)
|
|
side = {"text": big, "storage_text": big, "provenance": {}}
|
|
resolution = {
|
|
"state": "ambiguous",
|
|
"reason": "Multiple sampler branches",
|
|
"candidates": [
|
|
{
|
|
"state": "resolved",
|
|
"sampler_id": "3",
|
|
"reason": "",
|
|
"positive": side,
|
|
"negative": {"text": "", "storage_text": "", "provenance": {}},
|
|
"candidates": [],
|
|
}
|
|
],
|
|
}
|
|
evidence, count, limited = review_capture.review_evidence(resolution)
|
|
self.assertEqual((count, limited), (0, True))
|
|
self.assertEqual(evidence["candidates"], [])
|
|
self.assertLess(len(canonical_evidence(evidence)), 1024)
|
|
|
|
async def test_nested_ambiguous_candidates_are_flattened_and_deduplicated(self):
|
|
self._require_module()
|
|
|
|
def side(text):
|
|
return {"text": text, "storage_text": text, "provenance": {"n": text}}
|
|
|
|
def leaf(sampler, text):
|
|
return {
|
|
"state": "resolved",
|
|
"sampler_id": sampler,
|
|
"reason": "",
|
|
"positive": side(text),
|
|
"negative": side("bad"),
|
|
"candidates": [],
|
|
"reason_code": None,
|
|
}
|
|
|
|
resolution = {
|
|
"state": "ambiguous",
|
|
"reason": "Multiple sampler branches",
|
|
"candidates": [
|
|
leaf("3", "cat"),
|
|
{
|
|
"state": "ambiguous",
|
|
"sampler_id": None,
|
|
"reason": "stages",
|
|
"positive": None,
|
|
"negative": None,
|
|
"candidates": [leaf("5", "dog"), leaf("3", "cat")],
|
|
},
|
|
],
|
|
}
|
|
evidence, count, limited = review_capture.review_evidence(resolution)
|
|
self.assertEqual((count, limited), (2, False))
|
|
self.assertEqual(
|
|
[c["positive"]["text"] for c in evidence["candidates"]], ["cat", "dog"]
|
|
)
|
|
|
|
async def test_no_inference_or_model_access_during_recording(self):
|
|
self._require_module()
|
|
write_png(self.output_dir / "two.png", prompt=ambiguous_graph("cat", "dog"))
|
|
from py import autotag
|
|
|
|
with (
|
|
patch.object(autotag.AutoTagService, "load_model") as load,
|
|
patch.object(autotag.AutoTagService, "download_model") as download,
|
|
patch.object(autotag, "get_autotag_service") as service,
|
|
patch.object(
|
|
Image.Image, "load", autospec=True, side_effect=AssertionError("decode")
|
|
) as decode,
|
|
):
|
|
self.assertEqual(self._record("two.png"), "recorded")
|
|
load.assert_not_called()
|
|
download.assert_not_called()
|
|
service.assert_not_called()
|
|
decode.assert_not_called()
|
|
|
|
async def test_capture_released_and_handles_closed(self):
|
|
self._require_module()
|
|
write_png(self.output_dir / "two.png", prompt=ambiguous_graph("cat", "dog"))
|
|
self.assertEqual(self._record("two.png"), "recorded")
|
|
self.assertEqual(self.api._captures.stats()["captures"], 0)
|
|
self.assertEqual(self._capture_files(), [])
|
|
write_png(self.output_dir / "three.png", prompt=ambiguous_graph("a", "b"))
|
|
with patch.object(
|
|
review_capture,
|
|
"resolve_prompt_pair",
|
|
side_effect=RuntimeError("resolver failed"),
|
|
):
|
|
with self.assertRaises(RuntimeError):
|
|
self._record("three.png")
|
|
self.assertEqual(self.api._captures.stats()["captures"], 0)
|
|
self.assertEqual(self._capture_files(), [])
|
|
assert_closed(self, self.output_dir, self.capture_parent)
|
|
|
|
|
|
class TestReviewEdges(ReviewTestCase):
|
|
def _side(self, text):
|
|
return {"text": text, "storage_text": text, "provenance": {}}
|
|
|
|
def _leaf(self, sampler, text):
|
|
return {
|
|
"state": "resolved",
|
|
"sampler_id": sampler,
|
|
"reason": "",
|
|
"positive": self._side(text),
|
|
"negative": self._side(""),
|
|
"candidates": [],
|
|
}
|
|
|
|
async def test_evidence_over_the_bound_together_is_limited(self):
|
|
self._require_module()
|
|
half = "y" * (2 * 1024 * 1024 + 10) # twice per side, twice per item
|
|
resolution = {
|
|
"state": "ambiguous",
|
|
"reason": "Multiple sampler branches",
|
|
"candidates": [self._leaf("3", half), self._leaf("5", half + "z")],
|
|
}
|
|
evidence, count, limited = review_capture.review_evidence(resolution)
|
|
self.assertEqual((count, limited, evidence["candidate_total"]), (0, True, 2))
|
|
|
|
async def test_malformed_candidates_are_ignored_or_limited(self):
|
|
self._require_module()
|
|
self.assertEqual(
|
|
review_capture.review_evidence({"state": "ambiguous", "candidates": None}),
|
|
(
|
|
{
|
|
"resolver_version": review_capture.default_item_version(),
|
|
"reason": "",
|
|
"limited": True,
|
|
"candidate_total": 0,
|
|
"candidates": [],
|
|
},
|
|
0,
|
|
True,
|
|
),
|
|
)
|
|
resolution = {
|
|
"state": "ambiguous",
|
|
"reason": "x",
|
|
"candidates": ["junk", self._leaf("3", "cat"), self._leaf("5", "dog")],
|
|
}
|
|
self.assertEqual(review_capture.review_evidence(resolution)[1:], (2, False))
|
|
broken = self._leaf("7", "eel")
|
|
broken["positive"] = {"storage_text": "no text"}
|
|
resolution["candidates"].append(broken)
|
|
self.assertEqual(review_capture.review_evidence(resolution)[1:], (0, True))
|
|
|
|
async def test_unreadable_or_promptless_captures_are_skipped(self):
|
|
self._require_module()
|
|
record = review_capture.record_ambiguous_file
|
|
(self.output_dir / "junk.png").write_bytes(b"not a png at all")
|
|
write_png(self.output_dir / "plain.png")
|
|
info = PngImagePlugin.PngInfo()
|
|
info.add_text("prompt", "{not json")
|
|
Image.new("RGB", (2, 2)).save(str(self.output_dir / "bad.png"), pnginfo=info)
|
|
for name in ("junk.png", "plain.png", "bad.png"):
|
|
self.assertEqual(
|
|
record(self.api.db, self.api._captures, self.output_dir, 0, name),
|
|
"skipped",
|
|
name,
|
|
)
|
|
write_png(self.output_dir / "two.png", prompt=ambiguous_graph())
|
|
with patch.object(self.api._captures, "acquire", return_value=None):
|
|
self.assertEqual(
|
|
record(self.api.db, self.api._captures, self.output_dir, 0, "two.png"),
|
|
"skipped",
|
|
)
|
|
self.assertEqual(self._items(), [])
|
|
self.assertEqual(self._capture_files(), [])
|
|
|
|
async def test_record_review_item_validates_its_facts(self):
|
|
self._require_module()
|
|
epoch = self.api.db.get_execution_epoch()
|
|
# The spelling keeps its case; the locator is folded the native way.
|
|
spelled = os.path.join(self.tmpdir, "Out", "A.png")
|
|
locator = os.path.normcase(spelled)
|
|
other = os.path.normcase(os.path.join(self.tmpdir, "Out", "B.png"))
|
|
good = {
|
|
"locator": locator,
|
|
"spelled_path": spelled,
|
|
"sha256": "a" * 64,
|
|
"size": 1,
|
|
"mtime_ns": 2,
|
|
"epoch": epoch,
|
|
"resolver_version": 1,
|
|
"evidence": {"candidates": []},
|
|
"candidate_count": 0,
|
|
"limited": True,
|
|
}
|
|
for change in (
|
|
{"resolver_version": 0},
|
|
{"resolver_version": True},
|
|
{"candidate_count": 17},
|
|
{"candidate_count": -1},
|
|
{"limited": 1},
|
|
{"limited": True, "candidate_count": 2},
|
|
{"sha256": "A" * 64},
|
|
{"evidence": ["not", "an", "object"]},
|
|
{"spelled_path": ""},
|
|
{"spelled_path": None},
|
|
{"spelled_path": os.path.join(self.tmpdir, "Out", "B.png")},
|
|
{"locator": other},
|
|
):
|
|
with self.assertRaises(ValueError, msg=change):
|
|
self.api.db.record_review_item(**{**good, **change})
|
|
self.assertTrue(self.api.db.record_review_item(**good))
|
|
self.assertFalse(self.api.db.record_review_item(**good))
|
|
items = self.api.db.review_items_for(locator)
|
|
self.assertEqual(len(items), 1)
|
|
self.assertEqual(items[0]["evidence"], {"candidates": []})
|
|
self.assertIs(items[0]["limited"], True)
|
|
self.assertEqual(items[0]["sha256"], "a" * 64)
|
|
self.assertEqual(self.api.db.review_items_for(other), [])
|
|
self.assertEqual(self.api.db.review_index_revision(), 1)
|
|
with self.assertRaises(ValueError):
|
|
self.api.db.review_items_for("")
|
|
conn = self.api.db.model.get_connection()
|
|
with conn:
|
|
conn.execute("DELETE FROM media_review_metadata")
|
|
with self.assertRaises(sqlite3.DatabaseError):
|
|
self.api.db.review_index_revision()
|
|
|
|
|
|
class TestMonitorRecording(ReviewTestCase):
|
|
def _handler(self):
|
|
handler = ImageGenerationHandler(self.api.db, PromptTracker(self.api.db))
|
|
self.addCleanup(close_handler, self, handler)
|
|
return handler
|
|
|
|
def _linked_prompt(self, path):
|
|
conn = self.api.db.model.get_connection()
|
|
row = conn.execute(
|
|
"SELECT prompt_id FROM generated_images WHERE image_path=?", (str(path),)
|
|
).fetchone()
|
|
return row[0] if row else None
|
|
|
|
async def test_monitor_keeps_its_link_and_never_queues_the_linked_file(self):
|
|
"""The monitor's pass is best effort (a failure is logged, the link
|
|
stays); a file the monitor linked is never queued."""
|
|
self._require_module()
|
|
self._roots()
|
|
path = self.output_dir / "two.png"
|
|
write_png(path, prompt=unlinkable_graph("cat", "dog"))
|
|
prompt_id = self.api.db.save_prompt(
|
|
text="latest", prompt_hash=generate_prompt_hash("latest")
|
|
)
|
|
metadata = {"prompt": unlinkable_graph("cat", "dog")}
|
|
context = {"id": prompt_id, "text": "latest", "fallback": True}
|
|
|
|
handler = self._handler()
|
|
|
|
def broken(image_path):
|
|
raise sqlite3.OperationalError("database is locked")
|
|
|
|
handler.review_recorder = broken
|
|
with self.assertLogs("prompt_manager.image_monitor", logging.WARNING) as logs:
|
|
handler.link_image_to_prompt(str(path), context, metadata)
|
|
self.assertEqual(self._linked_prompt(path), prompt_id)
|
|
self.assertTrue(any("Needs review" in line for line in logs.output))
|
|
self.assertEqual(self._items(), [])
|
|
|
|
other = self.output_dir / "three.png"
|
|
write_png(other, prompt=unlinkable_graph("cat", "dog"))
|
|
calls = []
|
|
|
|
def recorder(image_path):
|
|
calls.append(self.api.record_review_file(image_path))
|
|
return calls[-1]
|
|
|
|
handler.review_recorder = recorder
|
|
handler.link_image_to_prompt(str(other), context, metadata)
|
|
self.assertEqual(self._linked_prompt(other), prompt_id)
|
|
self.assertEqual(calls, ["linked"])
|
|
self.assertEqual(self._items(), [])
|
|
self.assertEqual(self._capture_files(), [])
|
|
|
|
async def test_monitor_skips_resolved_and_files_outside_the_roots(self):
|
|
self._require_module()
|
|
self._roots()
|
|
prompt_id = self.api.db.save_prompt(
|
|
text="latest", prompt_hash=generate_prompt_hash("latest")
|
|
)
|
|
context = {"id": prompt_id, "text": "latest"}
|
|
calls = []
|
|
handler = self._handler()
|
|
handler.review_recorder = lambda image_path: calls.append(image_path)
|
|
resolved = self.output_dir / "one.png"
|
|
write_png(resolved, prompt=resolved_graph())
|
|
handler.link_image_to_prompt(
|
|
str(resolved), context, {"prompt": resolved_graph()}
|
|
)
|
|
self.assertEqual(calls, [])
|
|
outside = self.outside_dir / "two.png"
|
|
write_png(outside, prompt=ambiguous_graph())
|
|
self.assertEqual(self.api.record_review_file(str(outside)), "skipped")
|
|
self.assertEqual(self._items(), [])
|
|
|
|
async def test_monitor_recording_is_refused_beside_record_changing_jobs(self):
|
|
"""S1 admission: a ``records`` recording waits for no job; it is refused.
|
|
|
|
While a ``records`` or ``guarded`` job runs, the monitor's pass
|
|
writes nothing and answers ``deferred``; the review worker keeps the
|
|
file and queues it once the job ends, with no other output (#659). A
|
|
``read`` job does not refuse it. Thumbnail work (the job and the
|
|
gallery's holds) is ``guarded`` but writes only previews, never
|
|
records, so it does not defer the pass either (S5 M7).
|
|
"""
|
|
self._require_module()
|
|
from py.api import review_recording
|
|
from py.api.jobs import RESTORE_JOB_KIND, THUMBNAILS_JOB_KIND
|
|
|
|
self._roots()
|
|
calls = Recorded(self, self.api)
|
|
for kind, admission in (
|
|
(RESTORE_JOB_KIND, "records"),
|
|
("backup", "guarded"),
|
|
):
|
|
with self.subTest(admission=admission):
|
|
path = self.output_dir / f"held_{admission}.png"
|
|
write_png(path, prompt=unlinkable_graph("cat", "dog"))
|
|
calls.reset()
|
|
with patch.object(review_recording, "REVIEW_RETRY_SECONDS", 0.05):
|
|
async with self.api._jobs.hold(
|
|
kind, f"{kind} (admin page)", admission=admission
|
|
):
|
|
with self.assertLogs("prompt_manager", logging.INFO) as logs:
|
|
self.assertEqual(
|
|
self.api.queue_review_file(str(path)), "queued"
|
|
)
|
|
await calls.wait_for(1)
|
|
self.assertEqual(calls.outcomes, ["deferred"])
|
|
self.assertEqual(self._items_at(path), [])
|
|
# The job ended: the deferred file is queued, once
|
|
await calls.wait_for(2)
|
|
self.assertEqual(calls.outcomes, ["deferred", "recorded"])
|
|
self.assertEqual(len(self._items_at(path)), 1)
|
|
self.assertTrue(
|
|
any(f"{kind} (admin page)" in line for line in logs.output),
|
|
logs.output,
|
|
)
|
|
self.assertEqual(self.api._review_worker().deferred_count(), 0)
|
|
|
|
path = self.output_dir / "beside_reads.png"
|
|
write_png(path, prompt=unlinkable_graph("cat", "dog"))
|
|
async with self.api._jobs.hold("probe-read", "Read probe", admission="read"):
|
|
async with self.api._jobs.hold(
|
|
THUMBNAILS_JOB_KIND,
|
|
"Thumbnail clear (gallery page)",
|
|
admission="guarded",
|
|
):
|
|
outcome = await asyncio.to_thread(
|
|
self.api.record_review_file, str(path)
|
|
)
|
|
self.assertEqual(outcome, "recorded")
|
|
self.assertEqual(len(self._items_at(path)), 1)
|
|
|
|
async def test_a_running_thumbnail_job_does_not_defer_monitor_recording(self):
|
|
"""S5 M7: a long rebuild never defers the monitor's pass (no records)."""
|
|
self._require_module()
|
|
from py.api.jobs import THUMBNAILS_JOB_KIND
|
|
|
|
self._roots()
|
|
path = self.output_dir / "beside.png"
|
|
write_png(path, prompt=unlinkable_graph("cat", "dog"))
|
|
gate = asyncio.Event()
|
|
|
|
async def runner(job):
|
|
await gate.wait()
|
|
|
|
job = self.api._jobs.start(
|
|
THUMBNAILS_JOB_KIND,
|
|
"Thumbnail generation",
|
|
admission="guarded",
|
|
cancellable=True,
|
|
scope={},
|
|
runner=runner,
|
|
)
|
|
try:
|
|
outcome = await asyncio.to_thread(self.api.record_review_file, str(path))
|
|
finally:
|
|
gate.set()
|
|
await asyncio.wait_for(job.task, 5)
|
|
# Not deferred (thumbnail work writes no records): queued (#659).
|
|
self.assertEqual(outcome, "recorded")
|
|
self.assertEqual(len(self._items()), 1)
|
|
|
|
async def test_monitor_wiring_reaches_new_and_existing_handlers(self):
|
|
self._require_module()
|
|
from utils.image_monitor import ImageMonitor
|
|
|
|
monitor = ImageMonitor(self.api.db, PromptTracker(self.api.db))
|
|
recorder = self.api.record_review_file
|
|
monitor.set_review_recorder(recorder)
|
|
self.assertIs(monitor.review_recorder, recorder)
|
|
monitor.start_monitoring([str(self.output_dir)])
|
|
try:
|
|
self.assertIs(monitor.handler.review_recorder, recorder)
|
|
monitor.set_review_recorder(None)
|
|
self.assertIsNone(monitor.handler.review_recorder)
|
|
finally:
|
|
stop_monitor(self, monitor)
|
|
|
|
|
|
if __name__ == "__main__":
|
|
unittest.main()
|
|
|
|
|
|
class _FoldingPath:
|
|
"""``os.path`` of a case-folding filesystem (Windows, macOS APFS)."""
|
|
|
|
def __getattr__(self, name):
|
|
return getattr(os.path, name)
|
|
|
|
@staticmethod
|
|
def normcase(value):
|
|
return os.path.normcase(value).lower()
|
|
|
|
|
|
class _FoldingOs:
|
|
path = _FoldingPath()
|
|
|
|
def __getattr__(self, name):
|
|
return getattr(os, name)
|
|
|
|
|
|
def _case_folding():
|
|
"""Fold case where locators are formed: recording, the store, the database."""
|
|
import database.review as review_module
|
|
import utils.media_capture as capture_module
|
|
|
|
stack = contextlib.ExitStack()
|
|
for module in (review_module, capture_module, review_capture):
|
|
stack.enter_context(patch.object(module, "os", _FoldingOs()))
|
|
return stack
|
|
|
|
|
|
class TestRecordedSpelling(ReviewTestCase):
|
|
"""A case-only rename refreshes the recorded spelling (I5)."""
|
|
|
|
def _spelled(self):
|
|
conn = self.api.db.model.get_connection()
|
|
return [
|
|
row[0]
|
|
for row in conn.execute("SELECT spelled_path FROM media_review_items")
|
|
]
|
|
|
|
async def test_case_only_rename_refreshes_the_spelling_on_the_skip_path(self):
|
|
self._require_module()
|
|
self._roots()
|
|
upper = self.output_dir / "Two.PNG"
|
|
write_png(upper, prompt=unlinkable_graph("cat", "dog"))
|
|
with _case_folding():
|
|
self.assertEqual(self._scan_record(upper), "recorded")
|
|
(before,) = self._items()
|
|
revision = self._review_revision()
|
|
lower = self.output_dir / "two.png"
|
|
os.rename(upper, lower) # keeps size and mtime_ns
|
|
self.assertEqual(self._scan_record(lower), "duplicate")
|
|
(after,) = self._items()
|
|
self.assertEqual(self._spelled(), [str(lower)])
|
|
self.assertEqual(self._review_revision(), revision + 1)
|
|
# The item itself is unchanged: an open decision stays valid.
|
|
self.assertEqual(
|
|
{k: v for k, v in after.items() if k != "spelled_path"},
|
|
{k: v for k, v in before.items() if k != "spelled_path"},
|
|
)
|
|
from py.api.review import format_review_target
|
|
|
|
real_root = os.path.realpath(str(self.output_dir))
|
|
self.assertEqual(
|
|
format_review_target(
|
|
after["spelled_path"],
|
|
after["locator"],
|
|
[(0, real_root)],
|
|
pathmod=_FoldingPath(),
|
|
),
|
|
(0, "two.png"),
|
|
)
|
|
# The new spelling opens; the recorded one would be refused.
|
|
capture = self.api._captures.capture(
|
|
self.output_dir, "two.png", epoch="e", scope="s"
|
|
)
|
|
self.assertEqual(capture.locator, after["locator"])
|
|
self.api._captures.release(capture.token)
|
|
# Seeing the same spelling again writes nothing.
|
|
self.assertEqual(self._scan_record(lower), "duplicate")
|
|
self.assertEqual(self._review_revision(), revision + 1)
|
|
|
|
async def test_duplicate_insert_refreshes_the_spelling(self):
|
|
self._require_module()
|
|
db = self.api.db
|
|
root = os.path.realpath(str(self.output_dir))
|
|
upper, lower = os.path.join(root, "Two.PNG"), os.path.join(root, "two.png")
|
|
facts = dict(
|
|
sha256="a" * 64,
|
|
size=10,
|
|
mtime_ns=1,
|
|
resolver_version=1,
|
|
evidence={"reason": "", "candidates": []},
|
|
candidate_count=0,
|
|
limited=True,
|
|
)
|
|
with _case_folding():
|
|
locator = _FoldingPath.normcase(upper)
|
|
epoch = db.get_execution_epoch()
|
|
self.assertTrue(
|
|
db.record_review_item(
|
|
locator=locator, spelled_path=upper, epoch=epoch, **facts
|
|
)
|
|
)
|
|
revision = self._review_revision()
|
|
self.assertFalse(
|
|
db.record_review_item(
|
|
locator=locator, spelled_path=lower, epoch=epoch, **facts
|
|
)
|
|
)
|
|
self.assertEqual(self._spelled(), [lower])
|
|
self.assertEqual(self._review_revision(), revision + 1)
|
|
self.assertFalse(db.refresh_review_spelling(locator, lower))
|
|
self.assertEqual(self._review_revision(), revision + 1)
|
|
with self.assertRaises(ValueError):
|
|
db.refresh_review_spelling(locator, os.path.join(root, "other.png"))
|
|
|
|
|
|
class TestReviewWorker(ReviewTestCase):
|
|
"""#659 C5: one daemon thread records monitor outputs, off the image
|
|
worker, one path at a time."""
|
|
|
|
def _worker_thread(self):
|
|
return self.api._review_worker()._thread
|
|
|
|
async def test_a_queued_output_is_recorded_on_the_worker_thread(self):
|
|
self._roots()
|
|
path = self.output_dir / "two.png"
|
|
write_png(path, prompt=unlinkable_graph("cat", "dog"))
|
|
calls = Recorded(self, self.api)
|
|
|
|
self.assertEqual(self.api.queue_review_file(str(path)), "queued")
|
|
await calls.wait_for(1)
|
|
|
|
self.assertEqual(calls.outcomes, ["recorded"])
|
|
self.assertEqual(len(self._items()), 1)
|
|
thread = self._worker_thread()
|
|
self.assertEqual(thread.name, "pm-review-recorder")
|
|
self.assertTrue(thread.daemon)
|
|
self.assertTrue(self.api.review_worker_started())
|
|
|
|
async def test_a_worker_that_dies_is_started_again_by_the_next_output(self):
|
|
"""An unexpected error outside one recording ends the thread with a
|
|
log line, and the next queued output starts a new one (never a dead
|
|
worker that keeps answering ``queued``)."""
|
|
from unittest.mock import patch
|
|
|
|
self._roots()
|
|
worker = self.api._review_worker()
|
|
real = type(worker)._next_locked
|
|
broken = []
|
|
|
|
def next_once_broken(self_):
|
|
if not broken:
|
|
broken.append(True)
|
|
raise ValueError("boom")
|
|
return real(self_)
|
|
|
|
first = self.output_dir / "one.png"
|
|
write_png(first, prompt=unlinkable_graph("cat", "dog"))
|
|
with patch.object(type(worker), "_next_locked", next_once_broken):
|
|
with self.assertLogs(worker._logger, level="ERROR") as logs:
|
|
self.assertEqual(self.api.queue_review_file(str(first)), "queued")
|
|
dead = worker._thread
|
|
await asyncio.to_thread(dead.join, 5)
|
|
self.assertFalse(dead.is_alive())
|
|
self.assertIn("review worker stopped", "\n".join(logs.output))
|
|
self.assertIsNone(worker._thread)
|
|
self.assertTrue(self.api.review_worker_started())
|
|
|
|
calls = Recorded(self, self.api)
|
|
second = self.output_dir / "two.png"
|
|
write_png(second, prompt=unlinkable_graph("bird", "fish"))
|
|
self.assertEqual(self.api.queue_review_file(str(second)), "queued")
|
|
await calls.wait_for(2)
|
|
self.assertEqual(sorted(calls.outcomes), ["recorded", "recorded"])
|
|
|
|
async def test_a_path_queued_while_it_runs_runs_once_more_never_at_once(self):
|
|
import threading
|
|
|
|
entered, release = threading.Event(), threading.Event()
|
|
running, overlaps = [], []
|
|
|
|
def held(image_path):
|
|
if image_path in running:
|
|
overlaps.append(image_path)
|
|
running.append(image_path)
|
|
try:
|
|
if not entered.is_set():
|
|
entered.set()
|
|
release.wait(WAIT_SECONDS)
|
|
return "skipped"
|
|
finally:
|
|
running.remove(image_path)
|
|
|
|
calls = Recorded(self, self.api, side_effect=held)
|
|
self.addCleanup(release.set)
|
|
first, other = str(self.output_dir / "a.png"), str(self.output_dir / "b.png")
|
|
self.api.queue_review_file(first)
|
|
self.assertTrue(await asyncio.to_thread(entered.wait, WAIT_SECONDS))
|
|
for path in (first, first, other): # coalesced: "a" once more
|
|
self.assertEqual(self.api.queue_review_file(path), "queued")
|
|
release.set()
|
|
await calls.wait_for(3)
|
|
|
|
self.assertEqual(calls.paths, ["a.png", "a.png", "b.png"])
|
|
self.assertEqual(overlaps, [])
|
|
|
|
async def test_a_full_queue_drops_its_oldest_path_and_never_blocks(self):
|
|
import threading
|
|
|
|
from py.api import review_recording
|
|
|
|
entered, release = threading.Event(), threading.Event()
|
|
|
|
def held(image_path):
|
|
if not entered.is_set():
|
|
entered.set()
|
|
release.wait(WAIT_SECONDS)
|
|
return "skipped"
|
|
|
|
calls = Recorded(self, self.api, side_effect=held)
|
|
self.addCleanup(release.set)
|
|
with patch.object(review_recording, "REVIEW_QUEUE_LIMIT", 2):
|
|
self.api.queue_review_file(str(self.output_dir / "x.png"))
|
|
self.assertTrue(await asyncio.to_thread(entered.wait, WAIT_SECONDS))
|
|
with self.assertLogs("prompt_manager.api", logging.INFO) as logs:
|
|
for name in ("a.png", "b.png", "c.png"):
|
|
self.assertEqual(
|
|
self.api.queue_review_file(str(self.output_dir / name)),
|
|
"queued",
|
|
)
|
|
release.set()
|
|
await calls.wait_for(3)
|
|
self.assertEqual(calls.paths, ["x.png", "b.png", "c.png"])
|
|
self.assertTrue(any("a.png" in line for line in logs.output), logs.output)
|
|
|
|
async def test_the_deferred_set_keeps_its_newest_paths(self):
|
|
from py.api import review_recording
|
|
|
|
calls = Recorded(self, self.api, side_effect=lambda path: "deferred")
|
|
blocked = patch.object(
|
|
self.api._jobs, "blocker", return_value=object()
|
|
) # a job stays running: no replay
|
|
blocked.start()
|
|
self.addCleanup(blocked.stop)
|
|
with (
|
|
patch.object(review_recording, "REVIEW_DEFERRED_LIMIT", 2),
|
|
self.assertLogs("prompt_manager.api", logging.INFO) as logs,
|
|
):
|
|
for name in ("a.png", "b.png", "c.png"):
|
|
self.api.queue_review_file(str(self.output_dir / name))
|
|
await calls.wait_for(3)
|
|
worker = self.api._review_worker()
|
|
self.assertTrue(await asyncio.to_thread(worker.wait_idle, WAIT_SECONDS))
|
|
self.assertEqual(worker.deferred_count(), 2)
|
|
self.assertEqual(
|
|
[os.path.basename(p) for p in worker.deferred_paths()],
|
|
["b.png", "c.png"],
|
|
)
|
|
self.assertTrue(any("a.png" in line for line in logs.output), logs.output)
|
|
|
|
async def test_a_recorder_failure_is_logged_and_the_worker_goes_on(self):
|
|
def flaky(image_path):
|
|
if image_path.endswith("bad.png"):
|
|
raise sqlite3.OperationalError("database is locked")
|
|
return "skipped"
|
|
|
|
calls = Recorded(self, self.api, side_effect=flaky)
|
|
with self.assertLogs("prompt_manager.api", logging.WARNING) as logs:
|
|
self.api.queue_review_file(str(self.output_dir / "bad.png"))
|
|
self.api.queue_review_file(str(self.output_dir / "good.png"))
|
|
await calls.wait_for(2)
|
|
self.assertEqual(calls.outcomes, ["raised", "skipped"])
|
|
self.assertTrue(any("bad.png" in line for line in logs.output))
|
|
self.assertTrue(self._worker_thread().is_alive())
|
|
|
|
async def test_stop_drops_waiting_paths_joins_and_refuses_new_ones(self):
|
|
import threading
|
|
|
|
entered, release = threading.Event(), threading.Event()
|
|
|
|
def held(image_path):
|
|
entered.set()
|
|
release.wait(WAIT_SECONDS)
|
|
return "skipped"
|
|
|
|
Recorded(self, self.api, side_effect=held)
|
|
self.addCleanup(release.set)
|
|
self.api.queue_review_file(str(self.output_dir / "a.png"))
|
|
self.assertTrue(await asyncio.to_thread(entered.wait, WAIT_SECONDS))
|
|
self.api.queue_review_file(str(self.output_dir / "b.png"))
|
|
thread = self._worker_thread()
|
|
|
|
with self.assertLogs("prompt_manager.api", logging.WARNING):
|
|
self.assertFalse(self.api.stop_review_worker(0.05)) # "a" in hand
|
|
release.set()
|
|
self.assertTrue(self.api.stop_review_worker(WAIT_SECONDS))
|
|
self.assertFalse(thread.is_alive())
|
|
self.assertEqual(
|
|
self.api.queue_review_file(str(self.output_dir / "c.png")), "stopped"
|
|
)
|
|
self.assertEqual(self.api._review_worker().pending_count(), 0)
|
|
|
|
def test_a_worker_never_started_stops_at_once(self):
|
|
self.assertFalse(self.api.review_worker_started())
|
|
self.assertTrue(self.api.stop_review_worker(0))
|
|
self.assertFalse(self.api.review_worker_started())
|
|
|
|
|
|
class TestCaptureStoreShutdown(ReviewTestCase):
|
|
async def test_recording_after_close_creates_no_store(self):
|
|
"""A scan recording still in flight at shutdown leaks nothing (M2),
|
|
and the monitor's pass after it captures nothing at all."""
|
|
self._require_module()
|
|
self._roots()
|
|
path = self.output_dir / "two.png"
|
|
write_png(path, prompt=unlinkable_graph("cat", "dog"))
|
|
await self.api.cleanup_annotation_captures(None) # the shutdown hook
|
|
self.assertIsNone(self.api._captures)
|
|
with patch("utils.media_capture.tempfile.mkdtemp") as mkdtemp:
|
|
with self.assertRaises(CaptureError):
|
|
self._scan_record(path)
|
|
# The monitor's pass records nothing, so it needs no store.
|
|
handler = ImageGenerationHandler(self.api.db, PromptTracker(self.api.db))
|
|
self.addCleanup(close_handler, self, handler)
|
|
handler.review_recorder = self.api.record_review_file
|
|
handler._record_review(str(path))
|
|
self.assertEqual(self.api.record_review_file(str(path)), "skipped")
|
|
mkdtemp.assert_not_called()
|
|
self.assertIsNone(self.api._captures)
|
|
self.assertIsNone(self.api._captures_unsubscribe)
|
|
self.assertEqual(self._items(), [])
|
|
self.api.close_annotation_captures() # idempotent
|