* feat(review): add review queue schema with restore validation
Add the Needs review schema group (media_review_metadata,
media_review_items, media_review_decisions) beside the image annotation
group it cites. review_schema_error validates the catalog, columns,
foreign keys, row values and canonical 8 MiB evidence read-only;
migrate_review_state installs it in a SAVEPOINT after the annotation
group, and verify_database_file requires it under require_library_state.
A review group without a valid annotation group is refused. Decisions
null their pair and image navigation through indexes when those are
deleted. Old backups and annotation-era backups restore and gain the
group; browser fixtures clear the new tables on reset.
* feat(review): persist ambiguous sampler evidence from scans and new outputs
The output scan and the live monitor now queue every file the ingestion
resolver finds ambiguous for Needs review. The file is captured, its metadata
is read again from the capture and resolved with the default profile, and only
an ambiguous result is stored, bound to the capture's annotation byte record
and fenced by its storage epoch. Evidence holds one hashed projection per
selectable candidate; none, more than 16 or over 8 MiB is a limited item.
Recording is idempotent per bytes and evidence and never touches a decision.
Scan statuses, counters, outcome rows and legacy SSE events are unchanged; the
job scope gains review_items/review_errors and the log a Needs review line.
The monitor records after its fallback link, best effort.
* feat(review): explicit association decisions with compatible linking
Needs review gains its queue reads and decisions:
- GET /prompt_manager/review/items lists pending items oldest first with
the exact pending total and an HMAC keyset cursor bound to the storage
epoch and the review index revision.
- GET /prompt_manager/review/items/{id} adds the candidates (role texts,
label, selectability), the dependency fingerprint and the digest.
- POST /prompt_manager/review/items/{id}/decision applies a candidate only
for a capture of the item's bytes that the current file still hashes to
and whose evidence is unchanged, through the ingester's compatible-only
rules in the same transaction as the decision row; a conflict writes
nothing. Leave unresolved records the decision and links nothing.
ingest_resolved_pair keeps its contract; its body moves into
_ingest_resolved_pair_in, which runs in the caller's transaction.
Items keep the file's recorded spelling so targets keep their case on
Windows, and a rescan skips the copy and hash of a file whose size and
mtime match the bytes an item already cites.
* fix(review): build the spelling validation test from native paths
On Windows the locator is case-folded and uses backslashes, so the fixed
POSIX pair /out/a.png no longer folded to itself and the new spelling
check refused the good case. The test now spells a mixed-case native path
and folds it with os.path.normcase, and also refuses a locator of another
file.
* fix(review): Apply sees the file under every spelling and rechecks only its choice
Apply now finds the legacy rows of the same physical file however its root
is reached. The roots are real paths while the monitor links under the
directory it watches, so a symlinked or junctioned root, subst or a mapped
drive used to hide the monitor's row and Apply added a second, differently
linked row. Same-name rows are resolved to their physical identity
(normcase(realpath), else samefile); the dependency fingerprint covers all
of them, two rows refuse the choice, one row is enriched under the
ingester's compatible-only rules, and a same-name row the request did not
classify is a changed association.
Apply re-verifies only what the decision depends on: the bytes are still
ambiguous and still offer the chosen candidate. A resolver change that
rewords the reason or reorders candidates leaves pending items applicable.
The resolver version is one constant in prompt_pair_resolver, shared by
ingestion provenance and Needs review.
The transaction's byte check receives the re-hashed digest (or the body's
for Leave unresolved), every Apply refusal branch is tested with zero
writes, and the root listing and realpath calls run on a worker thread.
* fix(review): validate stored review evidence once per startup
Startup used to run the full review validation twice (the early guard and
the install) and the annotation group's row checks inside each, so every
evidence document was parsed and re-encoded twice. The early guard is now
catalog-only (a review group without annotation tables), and the install
validates the group once, trusting the annotation group it has just
validated. The evidence predicate no longer pattern-scans every document in
SQL, because the streamed canonical pass proves it, and the codec is built
once.
Measured on a database with 40,000 items of 4 KiB evidence: startup open
went from 7.7 s to 2.5 s. A test pins one evidence pass per reopen.
Restore and verify_database_file keep the full validation.
* fix(review): keep one capture worker for interactive requests
Needs review recording from the scan and the monitor shared the capture
store's two workers with the Tag dialog and Apply, so a busy scan could
answer a user's preview or decision with "Too many previews are open".
Background captures now never take the last worker or capture slot, and
wait up to two seconds for room before counting the file as not added.
Interactive captures are admitted or refused at once, as before.
* fix(review): refresh the recorded spelling when a file is seen under a new one
A case-only rename on Windows or macOS keeps size and mtime_ns, so the
rescan skipped the file and the item kept the old spelling. Its target was
then formatted from a name the directory no longer lists, the capture
refused it, and the item stayed pending with no way to apply it. The skip
path and a duplicate insert now record the current spelling for every item
at the locator and move the review index revision; item revisions do not
change, so an open decision stays valid. Tested under case-folding
semantics on every platform.
* fix(review): final capture store close and recoverable stale cursors
Closing the capture store at shutdown now runs under the store lock and is
final: a monitor recording or scan batch still in flight gets a capture
error instead of creating a new temp directory and restore subscription
after the cleanup.
A well-formed queue cursor this process cannot verify (opened before a
ComfyUI restart, whose cursor secret is per process) now answers 409
review-conflict with the current total, like any other stale page, instead
of an uncoded 400. Malformed cursors stay 400.
* fix(review): make only the shutdown cleanup final
The previous commit made every close of the capture store final, which
also refused the explicit closes that swap in a fresh store. Only the host's
on_cleanup hook is final now; an observe after it answers a coded capture
error instead of an unhandled 500.
* refactor(admin): move Needs review recording into its own mixin
_record_scanned_review, _record_review_sync, record_review_file and
_log_review_items move unchanged from py/api/admin.py to
py/api/review_recording.py (ReviewRecordingMixin), keeping admin.py from
growing further past the size ceiling. No behaviour change.
* fix(review): record the review fix round and the skip's residual risks in TESTING.md
The Needs review paragraphs now state the rescan skip's residual risks (an
in-place rewrite that keeps size and mtime_ns is never re-queued; a deleted
or moved file leaves its pending item counted), the physical identity rule
for Apply, the candidate-only recheck and shared resolver version, the
spelling refresh, background capture admission, the final shutdown close,
the stale-cursor answer and the measured startup validation cost.
* fix(review): refuse monitor recording beside record-changing jobs
System S1 (#289) gives every record change a registry admission class. The
scan already records Needs review items under its own records job. The
monitor's recording lasts one file and is not a job, so it now checks the
records admission: while a records or guarded job or hold runs it records
nothing, logs that the file waits for the next scan, and answers deferred.
The fallback link stands and the next "new" scan reads the ambiguous file
again, because a fallback link is not a resolved one.
JobRegistry.blocker and active iterate a snapshot of the jobs, since the
monitor's worker thread now asks them too.
* test(backup): wait for the restore, not a 5 s timer, in the thread test
test_database_is_usable_from_every_thread_after_restore let its worker
wait 5 s for the restore. On a stalled Windows runner (py3.11, where the
previous restore test also took 25 s instead of 0.3 s) the restore took
longer, the wait timed out, and the worker read the old database before
the restore ran. The worker now waits for the event under a 120 s bound
that only a hung test reaches, reports a timeout as an error, and the
main thread releases it even when the restore raises. With a restore
delayed by 6 s the old test fails and the new one passes.
* fix(review): store non-finite evidence as null and count pending items
canonical_evidence now follows main's non-finite policy (#303): NaN and
±Infinity are written as null instead of raising, so a NaN-stamped graph
never turns a review item limited. The System Overview reports the review
queue's pending total in needs_review.pending, read in the same snapshot
as the Library counts.
* fix(review): show the pending count on the System Overview review row
The Overview's review row now reads "N ambiguous image links" with the
concept's line, from needs_review.pending. Its Review metadata action
stays aria-disabled until the Needs review page ships. The system specs
and their concept comparison follow the real count.
* fix(system): answer the Overview and Statistics only after every read ended
asyncio.gather answered at the first failed read while its sibling
executor reads were still inside SQLite. In the suite the test teardown
then deleted the database under the running read, and py3.10 on Ubuntu
segfaulted in get_statistics (test_statistics_failure_is_an_error_envelope).
Both routes now wait for all their reads before answering.
* fix(review): queue only ambiguous files that nothing links, and heal the queue
The maintainer's live scan (19,908 files, scope "new") reported 4,677 files
under Needs review, but the queue held 10,232 items: every file the strict
resolver found ambiguous was queued, including the ones the legacy
browser-parser fallback then linked (all sampled "added" rows) and the ones
already linked to a known prompt (135 of 200 sampled "known" rows).
A file is now queued only when its final outcome is ambiguous and unlinked:
the resolver finds it ambiguous, the fallback links nothing and no saved row
links it already. An ambiguous file that a saved row links is reported as
unchanged ("Already linked to its prompt"), not as Needs review.
The queue heals itself. After each batch, the scan removes the pending items
of every listed file (read, or skipped as known) that a saved row now links.
The monitor does the same for each ambiguous output it links, and never
queues one. Only an undecided item is removed (pending, revision 0, no
decision row), so a decision is never deleted or changed and the schema
needs no new state.
The scan job's scope.review_items now counts this scan's files that are in
the pending queue after it, new or already there, so it agrees with the
review group's ambiguous rows and with the queue total. review_added and
review_settled are new, and the log gains "already in Needs review" and
"left Needs review · now linked" lines. A duplicate whose item is decided is
reported as "decided" and is not counted.
* fix(review): never queue an ambiguous file with nothing to choose
The maintainer's live queue held 39 items whose candidates all read
"unresolved" ("No positive / No negative"). An ambiguous result is now
reviewable only when at least two candidates are resolved with non-blank
positive 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 either. Both are reported unresolved, never linked and never
queued (also the A1111 two-delimiter case, which has no candidates).
A rescan removes the pending items of such files whose own evidence
offers no choice, with the same safe delete as the linked settle
(pending, revision 0, no decision row). A limited item (over 16 candidates) that holds at least two
candidates is kept. The job scope gains review_untraced and the log
"= N images left Needs review · nothing to choose".
* test(scan): pin only the per-source scope keys beside the review counters
* test(system): match the scan scope keys beside the review counters
1162 lines
47 KiB
Python
1162 lines
47 KiB
Python
"""Needs review decisions and queue reads (#249 E3).
|
|
|
|
The queue lists pending items oldest first behind an HMAC cursor bound to the
|
|
storage epoch and the review index revision; ``total`` is the exact pending
|
|
count. A decision is explicit: Apply links the chosen sampler's pair only
|
|
under the ingester's compatible rules and only for the current, re-hashed
|
|
bytes of the reviewed file, in one transaction with its decision row; Leave
|
|
unresolved records the decision and links nothing. Responses never carry a
|
|
server path.
|
|
"""
|
|
|
|
import json
|
|
import ntpath
|
|
import os
|
|
import posixpath
|
|
import sqlite3
|
|
import threading
|
|
from pathlib import Path
|
|
from unittest.mock import patch
|
|
|
|
try:
|
|
from tests.test_review import (
|
|
ReviewTestCase,
|
|
ambiguous_graph,
|
|
digest,
|
|
unlinkable_graph,
|
|
write_png,
|
|
)
|
|
except ImportError: # discovered with tests/ as the top-level directory
|
|
from test_review import (
|
|
ReviewTestCase,
|
|
ambiguous_graph,
|
|
digest,
|
|
unlinkable_graph,
|
|
write_png,
|
|
)
|
|
|
|
from database.review_schema import canonical_evidence, evidence_digest
|
|
from py.config import GalleryConfig
|
|
from utils import review_capture as review_capture_module
|
|
from utils.media_capture import (
|
|
CaptureCapacity,
|
|
CaptureChanged,
|
|
CaptureStore,
|
|
FileFacts,
|
|
)
|
|
from utils.hashing import generate_prompt_hash
|
|
|
|
try: # guarded so a missing module fails assertions, not test loading
|
|
from py.api import review as review_api
|
|
except ImportError:
|
|
review_api = None
|
|
|
|
ITEMS_URL = "/prompt_manager/review/items"
|
|
# Every table a decision may touch; compared whole before and after.
|
|
WRITE_TABLES = (
|
|
"prompts",
|
|
"generated_images",
|
|
"prompt_pairs",
|
|
"image_prompt_pairs",
|
|
"media_review_items",
|
|
"media_review_decisions",
|
|
"media_review_metadata",
|
|
)
|
|
|
|
|
|
class ReviewRoutesTestCase(ReviewTestCase):
|
|
def _require_routes(self):
|
|
self.assertIsNotNone(review_api, "py.api.review is not implemented")
|
|
|
|
def _ambiguous(self, name="two.png", positives=("cat", "dog"), color=(255, 0, 0)):
|
|
path = self.output_dir / name
|
|
path.parent.mkdir(parents=True, exist_ok=True)
|
|
write_png(path, prompt=ambiguous_graph(*positives), color=color)
|
|
return path
|
|
|
|
def _record(self, path):
|
|
self._roots()
|
|
outcome = self._record_raw(path)
|
|
self.assertIn(outcome, ("recorded", "limited"), outcome)
|
|
return outcome
|
|
|
|
def _record_raw(self, path):
|
|
"""Record ``path`` as the producers' recorder does, without their rule.
|
|
|
|
These tests are about decisions on an item; whether the scan or the
|
|
monitor would queue the file (``tests/test_review_outcome.py``) is
|
|
not their subject, and an item can outlive the moment its file gets
|
|
linked (until the next scan or monitor pass settles it).
|
|
"""
|
|
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)
|
|
|
|
async def _json(self, method, url, **kwargs):
|
|
resp = await self.client.request(method, url, **kwargs)
|
|
data = await resp.json()
|
|
self.responses.append(data)
|
|
return resp.status, data
|
|
|
|
async def asyncSetUp(self):
|
|
await super().asyncSetUp()
|
|
self.responses = []
|
|
|
|
async def _list(self, query=""):
|
|
status, data = await self._json("GET", ITEMS_URL + query)
|
|
self.assertEqual(status, 200, data)
|
|
return data
|
|
|
|
async def _detail(self, item_id):
|
|
status, data = await self._json("GET", f"{ITEMS_URL}/{item_id}")
|
|
self.assertEqual(status, 200, data)
|
|
return data
|
|
|
|
async def _observe(self, target):
|
|
status, data = await self._json(
|
|
"POST", "/prompt_manager/image-annotations/observe", json={"target": target}
|
|
)
|
|
self.assertEqual(status, 200, data)
|
|
return data
|
|
|
|
async def _open(self):
|
|
"""The first pending item, its detail and a fresh capture of its file."""
|
|
listed = await self._list()
|
|
item = listed["items"][0]
|
|
detail = await self._detail(item["id"])
|
|
observed = await self._observe(item["target"])
|
|
return item, detail, observed
|
|
|
|
@staticmethod
|
|
def _candidate(detail, positive):
|
|
return next(
|
|
c for c in detail["candidates"] if c["positive"]["text"] == positive
|
|
)
|
|
|
|
def _body(self, detail, observed, choice):
|
|
return {
|
|
"expected_epoch": observed["library_epoch"],
|
|
"expected_revision": detail["revision"],
|
|
"capture_token": observed["capture"]["token"],
|
|
"sha256": observed["capture"]["sha256"],
|
|
"dependency_fingerprint": detail["dependency_fingerprint"],
|
|
"choice": choice,
|
|
}
|
|
|
|
async def _decide(self, item_id, body):
|
|
return await self._json("POST", f"{ITEMS_URL}/{item_id}/decision", json=body)
|
|
|
|
def _snapshot(self):
|
|
conn = self.api.db.model.get_connection()
|
|
return {
|
|
table: [tuple(row) for row in conn.execute(f"SELECT * FROM {table}")]
|
|
for table in WRITE_TABLES
|
|
}
|
|
|
|
def _decisions(self):
|
|
conn = self.api.db.model.get_connection()
|
|
return [
|
|
dict(row)
|
|
for row in conn.execute(
|
|
"SELECT * FROM media_review_decisions ORDER BY decision_revision"
|
|
)
|
|
]
|
|
|
|
def _no_paths(self):
|
|
for data in self.responses:
|
|
text = canonical_evidence({"r": data})
|
|
self.assertNotIn(self.tmpdir, text)
|
|
self.assertNotIn(self.tmpdir.replace(os.sep, "/"), text)
|
|
self.assertNotIn(os.path.normcase(self.tmpdir), text)
|
|
|
|
|
|
class TestQueueReads(ReviewRoutesTestCase):
|
|
async def test_total_is_exact_pending_count(self):
|
|
self._require_routes()
|
|
for index in range(3):
|
|
self._record(self._ambiguous(f"f{index}.png", color=(index, 0, 0)))
|
|
page = await self._list("?limit=1")
|
|
self.assertEqual(page["total"], 3)
|
|
self.assertEqual(len(page["items"]), 1)
|
|
seen = [page["items"][0]["id"]]
|
|
while page["next_cursor"]:
|
|
page = await self._list(f"?limit=1&cursor={page['next_cursor']}")
|
|
self.assertEqual(page["total"], 3)
|
|
seen.extend(item["id"] for item in page["items"])
|
|
rows = self._items()
|
|
self.assertEqual(seen, [row["id"] for row in rows])
|
|
item = (await self._list())["items"][0]
|
|
self.assertEqual(
|
|
set(item),
|
|
{
|
|
"id",
|
|
"state",
|
|
"revision",
|
|
"created_at",
|
|
"filename",
|
|
"target",
|
|
"unavailable",
|
|
"candidate_count",
|
|
"limited",
|
|
},
|
|
)
|
|
self.assertEqual(
|
|
(item["state"], item["revision"], item["filename"]),
|
|
("pending", 0, "f0.png"),
|
|
)
|
|
self.assertEqual(item["candidate_count"], 2)
|
|
self.assertIs(item["unavailable"], False)
|
|
self.assertEqual(item["target"]["path"], "f0.png")
|
|
self.assertEqual(item["target"]["root_index"], 0)
|
|
conn = self.api.db.model.get_connection()
|
|
with conn:
|
|
conn.execute(
|
|
"UPDATE media_review_items SET state='selected', revision=1 "
|
|
"WHERE id=?",
|
|
(seen[0],),
|
|
)
|
|
conn.execute(
|
|
"UPDATE media_review_items SET state='unresolved', revision=1 "
|
|
"WHERE id=?",
|
|
(seen[1],),
|
|
)
|
|
page = await self._list()
|
|
self.assertEqual(page["total"], 1)
|
|
self.assertEqual([i["id"] for i in page["items"]], [seen[2]])
|
|
self.assertIsNone(page["next_cursor"])
|
|
self._no_paths()
|
|
|
|
async def test_empty_queue_and_limit_validation(self):
|
|
self._require_routes()
|
|
page = await self._list()
|
|
self.assertEqual(
|
|
(page["success"], page["total"], page["items"], page["next_cursor"]),
|
|
(True, 0, [], None),
|
|
)
|
|
self.assertEqual(page["library_epoch"], self.api.db.get_execution_epoch())
|
|
for query in ("?limit=0", "?limit=41", "?limit=abc", "?limit=-1", "?limit=1.5"):
|
|
status, data = await self._json("GET", ITEMS_URL + query)
|
|
self.assertEqual(status, 400, query)
|
|
self.assertFalse(data["success"])
|
|
status, data = await self._json("GET", ITEMS_URL + "?cursor=" + "x" * 5000)
|
|
self.assertEqual(status, 400)
|
|
|
|
async def test_cursor_invalidated_by_decision_and_restore(self):
|
|
self._require_routes()
|
|
for index in range(3):
|
|
self._record(self._ambiguous(f"f{index}.png", color=(index, 0, 0)))
|
|
first = await self._list("?limit=1")
|
|
cursor = first["next_cursor"]
|
|
self.assertTrue(cursor)
|
|
# Tampered: one character changed, or a cursor of another shape.
|
|
broken = ("A" if cursor[5] != "A" else "B").join((cursor[:5], cursor[6:]))
|
|
for token in ("!!!", "QUJD", cursor[:40]):
|
|
status, data = await self._json("GET", f"{ITEMS_URL}?cursor={token}")
|
|
self.assertEqual(status, 400, (token, data))
|
|
# Well-formed but not this process's signature (tampered, or opened
|
|
# before a ComfyUI restart): a recoverable conflict (M3).
|
|
status, data = await self._json("GET", f"{ITEMS_URL}?cursor={broken}")
|
|
self.assertEqual(
|
|
(status, data["code"], data["total"]), (409, "review-conflict", 3)
|
|
)
|
|
with patch.object(review_api, "_CURSOR_SECRET", b"\x01" * 32):
|
|
status, data = await self._json("GET", f"{ITEMS_URL}?cursor={cursor}")
|
|
self.assertEqual((status, data["code"]), (409, "review-conflict"), data)
|
|
self.assertEqual(data["library_epoch"], first["library_epoch"])
|
|
# A decision moves the review index revision: the old cursor is stale.
|
|
item, detail, observed = await self._open()
|
|
status, data = await self._decide(
|
|
item["id"], {**self._body(detail, observed, "unresolved")}
|
|
)
|
|
self.assertEqual(status, 200, data)
|
|
status, data = await self._json("GET", f"{ITEMS_URL}?limit=1&cursor={cursor}")
|
|
self.assertEqual(status, 409, data)
|
|
self.assertEqual(data["code"], "review-conflict")
|
|
self.assertEqual(data["total"], 2)
|
|
fresh = (await self._list("?limit=1"))["next_cursor"]
|
|
conn = self.api.db.model.get_connection()
|
|
with conn: # a restore replaces the storage epoch
|
|
conn.execute(
|
|
"UPDATE execution_history_metadata SET "
|
|
"storage_epoch='00000000-0000-4000-8000-000000000000'"
|
|
)
|
|
status, data = await self._json("GET", f"{ITEMS_URL}?limit=1&cursor={fresh}")
|
|
self.assertEqual(status, 409, data)
|
|
self.assertEqual(data["code"], "storage-changed")
|
|
self.assertEqual(data["library_epoch"], "00000000-0000-4000-8000-000000000000")
|
|
self._no_paths()
|
|
|
|
async def test_detail_size_cap_and_limited_item_has_no_candidates(self):
|
|
self._require_routes()
|
|
self._roots()
|
|
limited = self.output_dir / "limited.png"
|
|
# More than 16 candidates with a prompt: limited, still a choice.
|
|
many = [f"prompt {n}" for n in range(17)]
|
|
write_png(limited, prompt=unlinkable_graph(*many))
|
|
self.assertEqual(self._record_raw(str(limited)), "limited")
|
|
self._record(self._ambiguous("two.png"))
|
|
items = (await self._list())["items"]
|
|
self.assertEqual([i["limited"] for i in items], [True, False])
|
|
detail = await self._detail(items[0]["id"])
|
|
self.assertEqual((detail["limited"], detail["candidates"]), (True, []))
|
|
self.assertEqual(detail["candidate_count"], 0)
|
|
detail = await self._detail(items[1]["id"])
|
|
self.assertEqual(len(detail["candidates"]), 2)
|
|
candidate = self._candidate(detail, "cat")
|
|
self.assertEqual(
|
|
set(candidate),
|
|
{"hash", "sampler_id", "state", "reason", "label", "positive", "negative"},
|
|
)
|
|
self.assertEqual(candidate["positive"], {"text": "cat"})
|
|
self.assertEqual(candidate["negative"], {"text": "bad"})
|
|
self.assertEqual(candidate["state"], "resolved")
|
|
self.assertEqual(candidate["label"], "cat / bad")
|
|
self.assertEqual(detail["sha256"], digest(self.output_dir / "two.png"))
|
|
self.assertRegex(detail["dependency_fingerprint"], r"^[0-9a-f]{64}$")
|
|
with patch.object(review_api, "MAX_DETAIL_BYTES", 200):
|
|
capped = await self._detail(items[1]["id"])
|
|
self.assertEqual((capped["limited"], capped["candidates"]), (True, []))
|
|
self.assertEqual(capped["id"], items[1]["id"])
|
|
for missing in ("00000000-0000-4000-8000-000000000000", "not-a-uuid"):
|
|
status, data = await self._json("GET", f"{ITEMS_URL}/{missing}")
|
|
self.assertEqual(status, 404, data)
|
|
self.assertFalse(data["success"])
|
|
self._no_paths()
|
|
|
|
async def test_label_uses_library_title_of_a_saved_prompt(self):
|
|
self._require_routes()
|
|
prompt_id = self.api.db.save_prompt(
|
|
"cat", prompt_hash=generate_prompt_hash("cat")
|
|
)
|
|
conn = self.api.db.model.get_connection()
|
|
with conn:
|
|
conn.execute(
|
|
"INSERT INTO prompt_library_state(prompt_id,title) VALUES(?,?)",
|
|
(prompt_id, "Dunes"),
|
|
)
|
|
self._record(self._ambiguous("two.png"))
|
|
item = (await self._list())["items"][0]
|
|
detail = await self._detail(item["id"])
|
|
self.assertEqual(self._candidate(detail, "cat")["label"], "Dunes / bad")
|
|
self.assertEqual(self._candidate(detail, "dog")["label"], "dog / bad")
|
|
|
|
async def test_unavailable_when_no_current_root_lists_the_file(self):
|
|
self._require_routes()
|
|
self._record(self._ambiguous("two.png"))
|
|
other = self.output_dir / "elsewhere"
|
|
other.mkdir()
|
|
from py.config import GalleryConfig
|
|
|
|
GalleryConfig.MONITORING_DIRECTORIES = [self._root(other)]
|
|
item = (await self._list())["items"][0]
|
|
self.assertEqual(
|
|
(item["unavailable"], item["target"], item["filename"]),
|
|
(True, None, "two.png"),
|
|
)
|
|
detail = await self._detail(item["id"])
|
|
self.assertIs(detail["unavailable"], True)
|
|
self._no_paths()
|
|
|
|
def test_target_formatting_keeps_the_recorded_spelling_on_windows(self):
|
|
self._require_routes()
|
|
spelled = "C:\\Out\\Sub\\Image_0001.PNG"
|
|
locator = ntpath.normcase(spelled)
|
|
found = review_api.format_review_target(
|
|
spelled, locator, [(0, "D:\\Other"), (1, "C:\\Out")], pathmod=ntpath
|
|
)
|
|
self.assertEqual(found, (1, "Sub/Image_0001.PNG"))
|
|
self.assertIsNone(
|
|
review_api.format_review_target(
|
|
spelled, locator, [(0, "C:\\Outer")], pathmod=ntpath
|
|
)
|
|
)
|
|
# A recorded spelling that no longer folds to the locator is refused.
|
|
self.assertIsNone(
|
|
review_api.format_review_target(
|
|
spelled, "c:\\out\\sub\\other.png", [(0, "C:\\Out")], pathmod=ntpath
|
|
)
|
|
)
|
|
self.assertEqual(
|
|
review_api.format_review_target(
|
|
"/out/a/b.png", "/out/a/b.png", [(0, "/out")], pathmod=posixpath
|
|
),
|
|
(0, "a/b.png"),
|
|
)
|
|
|
|
|
|
class TestDecisions(ReviewRoutesTestCase):
|
|
async def test_apply_candidate_links_pair_and_records_decision_atomically(self):
|
|
self._require_routes()
|
|
path = self._ambiguous("two.png")
|
|
self._record(path)
|
|
revision = self._review_revision()
|
|
item, detail, observed = await self._open()
|
|
cat = self._candidate(detail, "cat")
|
|
before = self._snapshot()
|
|
# A failure after the link rolls the link back with everything else.
|
|
with patch.object(
|
|
type(self.api.db),
|
|
"_insert_review_decision",
|
|
side_effect=sqlite3.OperationalError("disk I/O error at /secret/path"),
|
|
):
|
|
status, data = await self._decide(
|
|
item["id"], self._body(detail, observed, cat["hash"])
|
|
)
|
|
self.assertEqual(status, 500, data)
|
|
self.assertEqual(data["code"], "decision-failed")
|
|
self.assertNotIn("secret", data["error"])
|
|
self.assertEqual(self._snapshot(), before)
|
|
|
|
# A failed decision keeps its capture for the retry.
|
|
status, data = await self._decide(
|
|
item["id"], self._body(detail, observed, cat["hash"])
|
|
)
|
|
self.assertEqual(status, 200, data)
|
|
self.assertTrue(data["success"])
|
|
self.assertEqual(data["total"], 0)
|
|
self.assertEqual(
|
|
(data["item"]["id"], data["item"]["state"], data["item"]["revision"]),
|
|
(item["id"], "selected", 1),
|
|
)
|
|
conn = self.api.db.model.get_connection()
|
|
image = conn.execute(
|
|
"SELECT gi.id, p.text FROM generated_images gi "
|
|
"JOIN prompts p ON p.id=gi.prompt_id WHERE gi.image_path=?",
|
|
(str(path),),
|
|
).fetchone()
|
|
self.assertEqual(image[1], "cat")
|
|
pair = conn.execute(
|
|
"SELECT pp.id, p.text, n.text FROM prompt_pairs pp "
|
|
"JOIN prompts p ON p.id=pp.positive_prompt_id "
|
|
"JOIN prompts n ON n.id=pp.negative_prompt_id"
|
|
).fetchall()
|
|
self.assertEqual([tuple(row)[1:] for row in pair], [("cat", "bad")])
|
|
self.assertEqual(data["pair"]["id"], pair[0][0])
|
|
self.assertEqual(data["pair"]["image_id"], image[0])
|
|
links = self.api.db.get_image_prompt_pairs(image[0])
|
|
self.assertEqual(len(links), 1)
|
|
provenance = links[0]["provenance"]
|
|
self.assertEqual(provenance["kind"], "user-selection")
|
|
self.assertEqual(provenance["candidate_hash"], cat["hash"])
|
|
self.assertEqual(provenance["reviewed_digest"], digest(path))
|
|
self.assertEqual(provenance["resolver_version"], 1)
|
|
self.assertEqual(provenance["positive"]["text"], "cat")
|
|
(decision,) = self._decisions()
|
|
self.assertEqual(provenance["decision_id"], decision["id"])
|
|
self.assertEqual(
|
|
(
|
|
decision["item_id"],
|
|
decision["decision_revision"],
|
|
decision["outcome"],
|
|
decision["candidate_hash"],
|
|
decision["pair_id"],
|
|
decision["image_id"],
|
|
),
|
|
(item["id"], 1, "selected", cat["hash"], pair[0][0], image[0]),
|
|
)
|
|
self.assertEqual(self._review_revision(), revision + 1)
|
|
self.assertEqual((await self._list())["total"], 0)
|
|
self.assertEqual(self._capture_files(), [])
|
|
self._no_paths()
|
|
|
|
async def test_apply_conflicting_legacy_owner_returns_association_conflict_with_zero_writes( # noqa: E501
|
|
self,
|
|
):
|
|
self._require_routes()
|
|
path = self._ambiguous("two.png")
|
|
# The monitor's fallback link: the file is linked to "cat" already.
|
|
prompt_id = self.api.db.save_prompt(
|
|
"cat", prompt_hash=generate_prompt_hash("cat")
|
|
)
|
|
image_id = self.api.db.link_image_to_prompt(prompt_id, str(path))
|
|
self._record(path)
|
|
item, detail, observed = await self._open()
|
|
before = self._snapshot()
|
|
dog = self._candidate(detail, "dog")
|
|
status, data = await self._decide(
|
|
item["id"], self._body(detail, observed, dog["hash"])
|
|
)
|
|
self.assertEqual(status, 409, data)
|
|
self.assertEqual(data["code"], "association-conflict")
|
|
self.assertIn("linked", data["error"])
|
|
self.assertEqual(self._snapshot(), before)
|
|
# The compatible choice reuses the existing image row.
|
|
cat = self._candidate(detail, "cat")
|
|
status, data = await self._decide(
|
|
item["id"], self._body(detail, observed, cat["hash"])
|
|
)
|
|
self.assertEqual(status, 200, data)
|
|
self.assertEqual(data["pair"]["image_id"], image_id)
|
|
conn = self.api.db.model.get_connection()
|
|
self.assertEqual(
|
|
conn.execute("SELECT COUNT(*) FROM generated_images").fetchone()[0], 1
|
|
)
|
|
self._no_paths()
|
|
|
|
async def test_changed_dependencies_are_an_association_conflict(self):
|
|
self._require_routes()
|
|
path = self._ambiguous("two.png")
|
|
self._record(path)
|
|
item, detail, observed = await self._open()
|
|
# Another writer links the file after the detail was read.
|
|
prompt_id = self.api.db.save_prompt(
|
|
"cat", prompt_hash=generate_prompt_hash("cat")
|
|
)
|
|
self.api.db.link_image_to_prompt(prompt_id, str(path))
|
|
before = self._snapshot()
|
|
cat = self._candidate(detail, "cat")
|
|
status, data = await self._decide(
|
|
item["id"], self._body(detail, observed, cat["hash"])
|
|
)
|
|
self.assertEqual(status, 409, data)
|
|
self.assertEqual(data["code"], "association-conflict")
|
|
self.assertNotEqual(
|
|
data["dependency_fingerprint"], detail["dependency_fingerprint"]
|
|
)
|
|
self.assertEqual(self._snapshot(), before)
|
|
|
|
async def test_leave_unresolved_records_decision_and_leaves_pending_list(self):
|
|
self._require_routes()
|
|
path = self._ambiguous("two.png")
|
|
self._record(path)
|
|
self._record(self._ambiguous("other.png", color=(0, 0, 9)))
|
|
item, detail, _observed = await self._open()
|
|
body = {
|
|
"expected_epoch": self.api.db.get_execution_epoch(),
|
|
"expected_revision": 0,
|
|
"capture_token": None,
|
|
"sha256": detail["sha256"],
|
|
"dependency_fingerprint": detail["dependency_fingerprint"],
|
|
"choice": "unresolved",
|
|
}
|
|
status, data = await self._decide(item["id"], body)
|
|
self.assertEqual(status, 200, data)
|
|
self.assertEqual(data["item"]["state"], "unresolved")
|
|
self.assertEqual(data["total"], 1)
|
|
self.assertNotIn("pair", data)
|
|
(decision,) = self._decisions()
|
|
self.assertEqual(
|
|
(decision["outcome"], decision["candidate_hash"], decision["pair_id"]),
|
|
("unresolved", None, None),
|
|
)
|
|
conn = self.api.db.model.get_connection()
|
|
for table in ("prompt_pairs", "image_prompt_pairs", "generated_images"):
|
|
self.assertEqual(
|
|
conn.execute(f"SELECT COUNT(*) FROM {table}").fetchone()[0], 0, table
|
|
)
|
|
listed = await self._list()
|
|
self.assertEqual(listed["total"], 1)
|
|
self.assertNotIn(item["id"], [i["id"] for i in listed["items"]])
|
|
# The item still answers its detail, now decided.
|
|
self.assertEqual((await self._detail(item["id"]))["state"], "unresolved")
|
|
self._no_paths()
|
|
|
|
async def test_system_overview_counts_pending_items(self):
|
|
self._require_routes()
|
|
|
|
async def pending():
|
|
status, data = await self._json("GET", "/prompt_manager/system/overview")
|
|
self.assertEqual(status, 200, data)
|
|
return data["needs_review"]
|
|
|
|
self.assertEqual(await pending(), {"pending": 0})
|
|
self._record(self._ambiguous("two.png"))
|
|
self._record(self._ambiguous("other.png", color=(0, 0, 9)))
|
|
self.assertEqual(await pending(), {"pending": 2})
|
|
item, detail, _observed = await self._open()
|
|
body = {
|
|
"expected_epoch": self.api.db.get_execution_epoch(),
|
|
"expected_revision": 0,
|
|
"capture_token": None,
|
|
"sha256": detail["sha256"],
|
|
"dependency_fingerprint": detail["dependency_fingerprint"],
|
|
"choice": "unresolved",
|
|
}
|
|
status, data = await self._decide(item["id"], body)
|
|
self.assertEqual(status, 200, data)
|
|
# A decided item leaves the count: it is the queue's own total.
|
|
self.assertEqual(await pending(), {"pending": data["total"]})
|
|
self.assertEqual(data["total"], 1)
|
|
|
|
async def test_changed_bytes_or_evidence_reject_apply(self):
|
|
self._require_routes()
|
|
path = self._ambiguous("two.png")
|
|
self._record(path)
|
|
item, detail, observed = await self._open()
|
|
cat = self._candidate(detail, "cat")
|
|
before = self._snapshot()
|
|
|
|
# The file is rewritten after the capture: the re-hash refuses it.
|
|
write_png(path, prompt=ambiguous_graph("cat", "dog"), color=(0, 255, 0))
|
|
status, data = await self._decide(
|
|
item["id"], self._body(detail, observed, cat["hash"])
|
|
)
|
|
self.assertEqual((status, data["code"]), (409, "file-changed"), data)
|
|
self.assertEqual(self._snapshot(), before)
|
|
|
|
# A fresh capture of the new bytes is not the reviewed item's bytes.
|
|
observed = await self._observe(item["target"])
|
|
status, data = await self._decide(
|
|
item["id"], self._body(detail, observed, cat["hash"])
|
|
)
|
|
self.assertEqual((status, data["code"]), (409, "file-changed"), data)
|
|
self.assertEqual(
|
|
self._snapshot()["media_review_items"], before["media_review_items"]
|
|
)
|
|
|
|
# Back to the reviewed bytes, but the evidence they yield has changed.
|
|
write_png(path, prompt=ambiguous_graph("cat", "dog"))
|
|
self.assertEqual(digest(path), detail["sha256"])
|
|
observed = await self._observe(item["target"])
|
|
changed = {
|
|
"state": "ambiguous",
|
|
"reason": "multiple samplers",
|
|
"candidates": [],
|
|
}
|
|
with patch("utils.review_capture.resolve_prompt_pair", return_value=changed):
|
|
status, data = await self._decide(
|
|
item["id"], self._body(detail, observed, cat["hash"])
|
|
)
|
|
self.assertEqual((status, data["code"]), (409, "review-conflict"), data)
|
|
|
|
# A choice that is not one of the item's candidates is refused.
|
|
status, data = await self._decide(
|
|
item["id"], self._body(detail, observed, "f" * 64)
|
|
)
|
|
self.assertEqual(status, 400, data)
|
|
# A restore since the capture.
|
|
status, data = await self._decide(
|
|
item["id"],
|
|
{
|
|
**self._body(detail, observed, cat["hash"]),
|
|
"expected_epoch": "00000000-0000-4000-8000-000000000000",
|
|
},
|
|
)
|
|
self.assertEqual((status, data["code"]), (409, "storage-changed"), data)
|
|
# A stale revision.
|
|
status, data = await self._decide(
|
|
item["id"],
|
|
{**self._body(detail, observed, cat["hash"]), "expected_revision": 3},
|
|
)
|
|
self.assertEqual((status, data["code"]), (409, "review-conflict"), data)
|
|
# Apply needs a capture; Leave unresolved does not.
|
|
status, data = await self._decide(
|
|
item["id"],
|
|
{**self._body(detail, observed, cat["hash"]), "capture_token": None},
|
|
)
|
|
self.assertEqual(status, 400, data)
|
|
# A capture of another file.
|
|
other = self._ambiguous("other.png", color=(1, 2, 3))
|
|
self._record(other)
|
|
target = {**item["target"], "path": "other.png"}
|
|
foreign = await self._observe(target)
|
|
status, data = await self._decide(
|
|
item["id"],
|
|
{
|
|
**self._body(detail, observed, cat["hash"]),
|
|
"capture_token": foreign["capture"]["token"],
|
|
"sha256": foreign["capture"]["sha256"],
|
|
},
|
|
)
|
|
self.assertEqual((status, data["code"]), (409, "capture-expired"), data)
|
|
self.assertEqual(self._decisions(), [])
|
|
self.assertEqual(self._items()[0]["state"], "pending")
|
|
self._no_paths()
|
|
|
|
async def test_decision_body_validation(self):
|
|
self._require_routes()
|
|
self._record(self._ambiguous("two.png"))
|
|
item, detail, observed = await self._open()
|
|
good = self._body(detail, observed, "unresolved")
|
|
cases = [
|
|
{**good, "extra": 1},
|
|
{k: v for k, v in good.items() if k != "choice"},
|
|
{**good, "expected_epoch": "nope"},
|
|
{**good, "expected_revision": -1},
|
|
{**good, "expected_revision": True},
|
|
{**good, "capture_token": "bad token!"},
|
|
{**good, "sha256": "A" * 64},
|
|
{**good, "dependency_fingerprint": "x"},
|
|
{**good, "choice": "maybe"},
|
|
{**good, "choice": 7},
|
|
{**good, "sha256": "0" * 64},
|
|
]
|
|
for body in cases:
|
|
with self.subTest(body=body):
|
|
status, data = await self._decide(item["id"], body)
|
|
self.assertEqual(status, 400, data)
|
|
self.assertFalse(data["success"])
|
|
status, data = await self._json(
|
|
"POST", f"{ITEMS_URL}/{item['id']}/decision", data=b"[1"
|
|
)
|
|
self.assertEqual(status, 400, data)
|
|
status, data = await self._decide("00000000-0000-4000-8000-000000000000", good)
|
|
self.assertEqual(status, 404, data)
|
|
self.assertEqual(self._decisions(), [])
|
|
|
|
async def test_duplicate_apply_after_commit_is_review_conflict_with_current_state(
|
|
self,
|
|
):
|
|
self._require_routes()
|
|
self._record(self._ambiguous("two.png"))
|
|
item, detail, observed = await self._open()
|
|
cat = self._candidate(detail, "cat")
|
|
status, data = await self._decide(
|
|
item["id"], self._body(detail, observed, cat["hash"])
|
|
)
|
|
self.assertEqual(status, 200, data)
|
|
after = self._snapshot()
|
|
observed = await self._observe(item["target"])
|
|
status, data = await self._decide(
|
|
item["id"], self._body(detail, observed, cat["hash"])
|
|
)
|
|
self.assertEqual(status, 409, data)
|
|
self.assertEqual(data["code"], "review-conflict")
|
|
self.assertEqual(
|
|
(data["item"]["state"], data["item"]["revision"]), ("selected", 1)
|
|
)
|
|
self.assertEqual(data["total"], 0)
|
|
self.assertEqual(self._snapshot(), after)
|
|
self.assertEqual(len(self._decisions()), 1)
|
|
|
|
async def test_tag_save_never_changes_review_item(self):
|
|
self._require_routes()
|
|
self._record(self._ambiguous("two.png"))
|
|
item, _detail, observed = await self._open()
|
|
before = self._snapshot()
|
|
status, data = await self._json(
|
|
"PUT",
|
|
"/prompt_manager/image-annotations/tags",
|
|
json={
|
|
"target": item["target"],
|
|
"capture_token": observed["capture"]["token"],
|
|
"sha256": observed["capture"]["sha256"],
|
|
"expected_epoch": observed["library_epoch"],
|
|
"expected_tag_revision": observed["tag_revision"],
|
|
"tags": ["sunset"],
|
|
},
|
|
)
|
|
self.assertEqual(status, 200, data)
|
|
after = self._snapshot()
|
|
for table in (
|
|
"media_review_items",
|
|
"media_review_decisions",
|
|
"media_review_metadata",
|
|
"generated_images",
|
|
"image_prompt_pairs",
|
|
):
|
|
self.assertEqual(after[table], before[table], table)
|
|
self.assertEqual((await self._list())["total"], 1)
|
|
|
|
async def test_run_and_usage_counts_unchanged(self):
|
|
self._require_routes()
|
|
path = self._ambiguous("two.png")
|
|
prompt_id = self.api.db.save_prompt(
|
|
"cat", prompt_hash=generate_prompt_hash("cat")
|
|
)
|
|
conn = self.api.db.model.get_connection()
|
|
with conn:
|
|
conn.execute(
|
|
"UPDATE prompts SET run_count=5, last_used_at='2026-01-01T00:00:00Z' "
|
|
"WHERE id=?",
|
|
(prompt_id,),
|
|
)
|
|
self._record(path)
|
|
executions = conn.execute(
|
|
"SELECT name FROM sqlite_master WHERE type='table' "
|
|
"AND name LIKE 'execution_%'"
|
|
).fetchall()
|
|
counts = {
|
|
row[0]: conn.execute(f"SELECT COUNT(*) FROM {row[0]}").fetchone()[0]
|
|
for row in executions
|
|
}
|
|
item, detail, observed = await self._open()
|
|
status, data = await self._decide(
|
|
item["id"],
|
|
self._body(detail, observed, self._candidate(detail, "cat")["hash"]),
|
|
)
|
|
self.assertEqual(status, 200, data)
|
|
rows = conn.execute(
|
|
"SELECT text, run_count, last_used_at FROM prompts"
|
|
).fetchall()
|
|
self.assertIn(("cat", 5, "2026-01-01T00:00:00Z"), [tuple(r) for r in rows])
|
|
self.assertEqual({tuple(r)[1] for r in rows if r[0] != "cat"}, {0})
|
|
self.assertEqual(
|
|
{
|
|
name: conn.execute(f"SELECT COUNT(*) FROM {name}").fetchone()[0]
|
|
for name in counts
|
|
},
|
|
counts,
|
|
)
|
|
(pair,) = self.api.db.list_prompt_pairs()["pairs"]
|
|
self.assertEqual(pair["run_count"], 0)
|
|
|
|
async def test_no_absolute_paths_in_responses(self):
|
|
self._require_routes()
|
|
self._record(self._ambiguous("two.png"))
|
|
item, detail, observed = await self._open()
|
|
await self._json("GET", f"{ITEMS_URL}?cursor=bad")
|
|
await self._decide(item["id"], {**self._body(detail, observed, "f" * 64)})
|
|
with patch.object(
|
|
type(self.api.db),
|
|
"review_queue_page",
|
|
side_effect=sqlite3.OperationalError(f"no such table at {self.tmpdir}"),
|
|
):
|
|
status, data = await self._json("GET", ITEMS_URL)
|
|
self.assertEqual((status, data["code"]), (500, "review-failed"))
|
|
with patch.object(
|
|
type(self.api.db),
|
|
"review_item",
|
|
side_effect=sqlite3.OperationalError(f"locked {self.tmpdir}"),
|
|
):
|
|
status, data = await self._json("GET", f"{ITEMS_URL}/{item['id']}")
|
|
self.assertEqual((status, data["code"]), (500, "review-failed"))
|
|
status, data = await self._decide(
|
|
item["id"], self._body(detail, observed, "unresolved")
|
|
)
|
|
self.assertEqual(status, 200, data)
|
|
self._no_paths()
|
|
|
|
|
|
class TestPhysicalIdentity(ReviewRoutesTestCase):
|
|
"""Apply sees a legacy row of the same file under any spelling (I1).
|
|
|
|
The roots are real paths, but the monitor links a file under the
|
|
directory it watches: a symlink, a Windows junction, ``subst`` or a
|
|
mapped drive reaches the same file under another spelling.
|
|
"""
|
|
|
|
def _monitor_link(self, text, spelled):
|
|
prompt_id = self.api.db.save_prompt(
|
|
text, prompt_hash=generate_prompt_hash(text)
|
|
)
|
|
return self.api.db.link_image_to_prompt(prompt_id, str(spelled))
|
|
|
|
def _rows(self):
|
|
conn = self.api.db.model.get_connection()
|
|
return [
|
|
tuple(row)
|
|
for row in conn.execute(
|
|
"SELECT gi.image_path, p.text FROM generated_images gi "
|
|
"JOIN prompts p ON p.id=gi.prompt_id ORDER BY gi.id"
|
|
)
|
|
]
|
|
|
|
async def _conflict_then_compatible(self, alias_path):
|
|
image_id = self._monitor_link("cat", alias_path)
|
|
item, detail, observed = await self._open()
|
|
before = self._snapshot()
|
|
dog = self._candidate(detail, "dog")
|
|
status, data = await self._decide(
|
|
item["id"], self._body(detail, observed, dog["hash"])
|
|
)
|
|
self.assertEqual((status, data.get("code")), (409, "association-conflict"))
|
|
self.assertEqual(self._snapshot(), before)
|
|
cat = self._candidate(detail, "cat")
|
|
status, data = await self._decide(
|
|
item["id"], self._body(detail, observed, cat["hash"])
|
|
)
|
|
self.assertEqual(status, 200, data)
|
|
# The monitor's row is enriched; no second row for the same file.
|
|
self.assertEqual(data["pair"]["image_id"], image_id)
|
|
self.assertEqual(self._rows(), [(str(alias_path), "cat")])
|
|
self._no_paths()
|
|
|
|
def _symlinked_root(self):
|
|
alias = Path(self.tmpdir) / "alias-output"
|
|
try:
|
|
os.symlink(str(self.output_dir), str(alias), target_is_directory=True)
|
|
except (OSError, NotImplementedError, AttributeError) as exc:
|
|
self.skipTest(f"symlinks unavailable: {exc}")
|
|
return alias
|
|
|
|
async def test_symlinked_root_legacy_row_is_an_association_conflict(self):
|
|
self._require_routes()
|
|
path = self._ambiguous("two.png")
|
|
alias = self._symlinked_root()
|
|
self._record(path)
|
|
await self._conflict_then_compatible(alias / "two.png")
|
|
|
|
async def test_junction_or_mapped_drive_legacy_row_is_seen(self):
|
|
"""A portable fake of a junction: its realpath is the real root."""
|
|
self._require_routes()
|
|
path = self._ambiguous("two.png")
|
|
self._record(path)
|
|
junction = os.path.join(self.tmpdir, "Junction")
|
|
real = os.path.realpath
|
|
|
|
def fake_realpath(value, *args, **kwargs):
|
|
text = os.fspath(value)
|
|
if os.path.normcase(text).startswith(os.path.normcase(junction)):
|
|
text = str(self.output_dir) + text[len(junction) :]
|
|
return real(text, *args, **kwargs)
|
|
|
|
with patch.object(review_api, "_realpath", side_effect=fake_realpath):
|
|
await self._conflict_then_compatible(os.path.join(junction, "two.png"))
|
|
|
|
async def test_two_rows_under_two_spellings_are_an_association_conflict(self):
|
|
self._require_routes()
|
|
path = self._ambiguous("two.png")
|
|
alias = self._symlinked_root()
|
|
self._monitor_link("cat", alias / "two.png")
|
|
self._monitor_link("cat", path)
|
|
self._record(path)
|
|
item, detail, observed = await self._open()
|
|
before = self._snapshot()
|
|
cat = self._candidate(detail, "cat")
|
|
status, data = await self._decide(
|
|
item["id"], self._body(detail, observed, cat["hash"])
|
|
)
|
|
self.assertEqual((status, data.get("code")), (409, "association-conflict"))
|
|
self.assertEqual(self._snapshot(), before)
|
|
|
|
async def test_same_name_in_another_folder_is_not_the_file(self):
|
|
self._require_routes()
|
|
path = self._ambiguous("two.png")
|
|
other = self.output_dir / "elsewhere" / "two.png"
|
|
other.parent.mkdir(parents=True)
|
|
write_png(other, prompt=ambiguous_graph("cat", "dog"), color=(9, 9, 9))
|
|
self._monitor_link("dog", other)
|
|
self._record(path)
|
|
item, detail, observed = await self._open()
|
|
cat = self._candidate(detail, "cat")
|
|
status, data = await self._decide(
|
|
item["id"], self._body(detail, observed, cat["hash"])
|
|
)
|
|
self.assertEqual(status, 200, data)
|
|
self.assertEqual(
|
|
sorted(self._rows()), sorted([(str(other), "dog"), (str(path), "cat")])
|
|
)
|
|
|
|
async def test_a_new_alias_row_after_the_check_is_a_changed_conflict(self):
|
|
self._require_routes()
|
|
path = self._ambiguous("two.png")
|
|
alias = self._symlinked_root()
|
|
self._record(path)
|
|
item, detail, observed = await self._open()
|
|
cat = self._candidate(detail, "cat")
|
|
# The monitor links the file under its alias after the reviewer opened it.
|
|
self._monitor_link("cat", alias / "two.png")
|
|
before = self._snapshot()
|
|
status, data = await self._decide(
|
|
item["id"], self._body(detail, observed, cat["hash"])
|
|
)
|
|
self.assertEqual((status, data.get("code")), (409, "association-conflict"))
|
|
self.assertNotEqual(
|
|
data["dependency_fingerprint"], detail["dependency_fingerprint"]
|
|
)
|
|
self.assertEqual(self._snapshot(), before)
|
|
|
|
|
|
class TestApplyRefusals(ReviewRoutesTestCase):
|
|
"""Every Apply refusal answers a code and writes nothing (I2, M1, M4)."""
|
|
|
|
async def _opened(self):
|
|
path = self._ambiguous("two.png")
|
|
self._record(path)
|
|
item, detail, observed = await self._open()
|
|
return path, item, detail, observed, self._candidate(detail, "cat")
|
|
|
|
async def _refused(self, item, body, status, code=None):
|
|
before = self._snapshot()
|
|
got, data = await self._decide(item["id"], body)
|
|
self.assertEqual(got, status, data)
|
|
if code is not None:
|
|
self.assertEqual(data["code"], code, data)
|
|
self.assertEqual(self._snapshot(), before)
|
|
self.assertEqual(self._decisions(), [])
|
|
return data
|
|
|
|
async def test_resolver_change_keeps_the_chosen_candidate_applicable(self):
|
|
self._require_routes()
|
|
_path, item, detail, observed, cat = await self._opened()
|
|
real = review_capture_module.resolve_prompt_pair
|
|
|
|
def reworded(metadata):
|
|
resolution = real(metadata)
|
|
return {
|
|
**resolution,
|
|
"reason": "a reworded reason from a newer resolver",
|
|
"candidates": list(reversed(resolution["candidates"])),
|
|
}
|
|
|
|
with (
|
|
patch.object(
|
|
review_capture_module, "resolve_prompt_pair", side_effect=reworded
|
|
),
|
|
patch.object(review_capture_module, "RESOLVER_VERSION", 2),
|
|
):
|
|
status, data = await self._decide(
|
|
item["id"], self._body(detail, observed, cat["hash"])
|
|
)
|
|
self.assertEqual(status, 200, data)
|
|
self.assertEqual(data["item"]["state"], "selected")
|
|
|
|
async def test_chosen_candidate_no_longer_offered_is_a_review_conflict(self):
|
|
self._require_routes()
|
|
_path, item, detail, observed, cat = await self._opened()
|
|
real = review_capture_module.resolve_prompt_pair
|
|
|
|
def without_cat(metadata):
|
|
resolution = real(metadata)
|
|
kept = [
|
|
c
|
|
for c in resolution["candidates"]
|
|
if (c.get("positive") or {}).get("text") != "cat"
|
|
]
|
|
return {**resolution, "candidates": kept * 2}
|
|
|
|
with patch.object(
|
|
review_capture_module, "resolve_prompt_pair", side_effect=without_cat
|
|
):
|
|
await self._refused(
|
|
item,
|
|
self._body(detail, observed, cat["hash"]),
|
|
409,
|
|
"review-conflict",
|
|
)
|
|
|
|
async def test_decision_passes_the_verified_digest(self):
|
|
self._require_routes()
|
|
path, item, detail, observed, cat = await self._opened()
|
|
real = type(self.api.db).decide_review_item
|
|
seen = []
|
|
|
|
def spy(db, **kwargs):
|
|
seen.append(kwargs["sha256"])
|
|
return real(db, **kwargs)
|
|
|
|
with patch.object(type(self.api.db), "decide_review_item", spy):
|
|
status, data = await self._decide(
|
|
item["id"], self._body(detail, observed, cat["hash"])
|
|
)
|
|
self.assertEqual(status, 200, data)
|
|
self.assertEqual(seen, [digest(path)])
|
|
|
|
async def test_expired_capture(self):
|
|
self._require_routes()
|
|
_path, item, detail, observed, cat = await self._opened()
|
|
self.api._captures.release(observed["capture"]["token"])
|
|
await self._refused(
|
|
item, self._body(detail, observed, cat["hash"]), 409, "capture-expired"
|
|
)
|
|
|
|
async def test_a_candidate_that_cannot_be_applied(self):
|
|
self._require_routes()
|
|
_path, item, detail, observed, cat = await self._opened()
|
|
conn = self.api.db.model.get_connection()
|
|
(text,) = conn.execute(
|
|
"SELECT evidence FROM media_review_items WHERE id=?", (item["id"],)
|
|
).fetchone()
|
|
evidence = json.loads(text)
|
|
for candidate in evidence["candidates"]:
|
|
if candidate["hash"] == cat["hash"]:
|
|
candidate["state"] = "unresolved"
|
|
text = canonical_evidence(evidence)
|
|
with conn:
|
|
conn.execute(
|
|
"UPDATE media_review_items SET evidence=?, evidence_hash=? WHERE id=?",
|
|
(text, evidence_digest(text), item["id"]),
|
|
)
|
|
data = await self._refused(item, self._body(detail, observed, cat["hash"]), 400)
|
|
self.assertIn("cannot be applied", data["error"])
|
|
|
|
async def test_leave_unresolved_with_another_digest(self):
|
|
self._require_routes()
|
|
_path, item, detail, observed, _cat = await self._opened()
|
|
body = {
|
|
**self._body(detail, observed, "unresolved"),
|
|
"capture_token": None,
|
|
"sha256": "0" * 64,
|
|
}
|
|
data = await self._refused(item, body, 400)
|
|
self.assertIn("does not match the item", data["error"])
|
|
|
|
async def test_root_no_longer_configured_is_source_changed(self):
|
|
self._require_routes()
|
|
_path, item, detail, observed, cat = await self._opened()
|
|
# Another root takes index 0.
|
|
elsewhere = self.output_dir / "elsewhere"
|
|
elsewhere.mkdir()
|
|
GalleryConfig.MONITORING_DIRECTORIES = [self._root(elsewhere)]
|
|
await self._refused(
|
|
item, self._body(detail, observed, cat["hash"]), 409, "source-changed"
|
|
)
|
|
|
|
async def test_file_no_longer_under_its_root_is_source_changed(self):
|
|
self._require_routes()
|
|
_path, item, detail, observed, cat = await self._opened()
|
|
moved = os.path.join(self.tmpdir, "moved-root")
|
|
real = os.path.realpath
|
|
|
|
def other_root(value, *args, **kwargs):
|
|
if os.path.normcase(os.fspath(value)) == os.path.normcase(
|
|
str(self.output_dir)
|
|
):
|
|
return moved
|
|
return real(value, *args, **kwargs)
|
|
|
|
with patch.object(review_api, "_realpath", side_effect=other_root):
|
|
await self._refused(
|
|
item, self._body(detail, observed, cat["hash"]), 409, "source-changed"
|
|
)
|
|
|
|
async def test_verify_failures(self):
|
|
self._require_routes()
|
|
_path, item, detail, observed, cat = await self._opened()
|
|
store = self.api._captures
|
|
body = self._body(detail, observed, cat["hash"])
|
|
with patch.object(store, "verify", side_effect=CaptureChanged("gone")):
|
|
await self._refused(item, body, 409, "file-changed")
|
|
other = FileFacts(item_locator := self._items()[0]["locator"], "1" * 64, 1, 1)
|
|
with patch.object(store, "verify", return_value=other):
|
|
await self._refused(item, body, 409, "file-changed")
|
|
foreign = FileFacts(item_locator + "x", detail["sha256"], 1, 1)
|
|
with patch.object(store, "verify", return_value=foreign):
|
|
data = await self._refused(item, body, 409, "capture-expired")
|
|
self.assertIn("another file", data["error"])
|
|
|
|
|
|
class TestInteractiveCapacity(ReviewRoutesTestCase):
|
|
async def test_background_recording_leaves_a_worker_for_the_tag_dialog(self):
|
|
"""A scan or monitor recording in flight never starves an observe (I4)."""
|
|
self._require_routes()
|
|
self._record(self._ambiguous("two.png"))
|
|
busy = self._ambiguous("busy.png", color=(4, 5, 6))
|
|
item = (await self._list())["items"][0]
|
|
store = self.api._captures
|
|
entered, proceed = threading.Event(), threading.Event()
|
|
original = CaptureStore._read
|
|
outcome = {}
|
|
|
|
def blocking(store_, stream, size):
|
|
if threading.current_thread() is recorder and not entered.is_set():
|
|
entered.set()
|
|
proceed.wait(30)
|
|
return original(store_, stream, size)
|
|
|
|
def record():
|
|
outcome["value"] = self._record_raw(str(busy))
|
|
|
|
recorder = threading.Thread(target=record)
|
|
with patch.object(CaptureStore, "_read", blocking):
|
|
recorder.start()
|
|
try:
|
|
self.assertTrue(entered.wait(30))
|
|
self.assertEqual(store.stats()["workers"], 1)
|
|
# Another background recording finds no worker it may use...
|
|
with patch("utils.media_capture.BACKGROUND_WAIT_SECONDS", 0.01):
|
|
with self.assertRaises(CaptureCapacity):
|
|
self._record_raw(str(busy))
|
|
# ...while the interactive observe still gets the last one.
|
|
observed = await self._observe(item["target"])
|
|
self.assertEqual(
|
|
observed["capture"]["sha256"], digest(self.output_dir / "two.png")
|
|
)
|
|
finally:
|
|
proceed.set()
|
|
recorder.join(30)
|
|
self.assertFalse(recorder.is_alive())
|
|
self.assertEqual(outcome["value"], "recorded")
|