* 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.
816 lines
33 KiB
Python
816 lines
33 KiB
Python
"""Capture-bound Needs review items for ambiguous files (#249 E2).
|
|
|
|
The output scan, and the live monitor's review worker at a file's final
|
|
unlinked outcome (#659, maintainer decision 2026-11-19: the monitor queues
|
|
what the scan would), hand every unlinked file the ingestion resolver finds
|
|
``ambiguous`` to :func:`record_ambiguous_file`. It captures the file
|
|
(the bounded private copy of ``utils.media_capture``, hashed while copied),
|
|
reads the metadata again from the capture, resolves it with the default
|
|
(ingestion) profile and stores only an ``ambiguous`` result, bound to the
|
|
capture's byte record. The evidence a reviewer chooses from is therefore the
|
|
evidence of exactly those bytes. An ambiguous result is stored only when it
|
|
is ``reviewable``: at least one candidate carries a resolved prompt text
|
|
(#657 addendum D1). With none the file could not be traced. With exactly
|
|
one, the untraced branches may still have made the file, so the file is
|
|
never linked from it, but the user may confirm it: the other branches stay
|
|
listed, disabled (the resolver never flattens unknown semantics into a
|
|
guess).
|
|
Nothing here links a file, loads a model or decodes pixels; the capture is
|
|
released before the database write.
|
|
|
|
Evidence is small on purpose (startup validates every row): the resolver's
|
|
reason and, per candidate sampler, ``{sampler_id, state, reason, positive,
|
|
negative}`` where each side is ``{text, storage_text, provenance}``. A
|
|
candidate's ``hash`` is the SHA-256 of its canonical JSON without the hash.
|
|
More than ``MAX_CANDIDATES`` candidates are collapsed (#657 addendum D3): the
|
|
``MAX_CANDIDATES`` most used choosable prompts are kept, one candidate each,
|
|
and the evidence names ``collapsed: {shown, total, paths}``. Evidence that
|
|
cannot be projected or is over the 8 MiB bound makes a ``limited`` item
|
|
without candidates (a result without any candidate is not ``reviewable`` and
|
|
is never stored).
|
|
|
|
Inspection-profile choices (ruling a, after the maintainer's Qwen image-edit
|
|
batches): when the default profile is ambiguous without a choice, or calls a
|
|
file with a prompt graph ``unresolved`` (#659 C1, e.g. "Unsupported latent
|
|
ancestry"), the scan and the review worker (``inspection=True``) ask
|
|
``INSPECTION_PROFILE``,
|
|
which knows more encoder and string nodes. With a choosable
|
|
candidate there, the item is recorded from that profile. A default choice
|
|
beside unresolved branches (``partial_choice``) yields to the inspection
|
|
profile's when that offers more choosable candidates (``offers_more``, #657
|
|
review): one readable choice no longer hides the branches only the
|
|
inspection profile reads. The inspection item's evidence names
|
|
``profile: "inspection"`` and ``semantics``, the fingerprint of what the table
|
|
says about the graph's node classes, and its ``resolver_version`` is
|
|
``inspection_review_version(metadata)``, which folds the profile,
|
|
``RESOLVER_VERSION`` and that fingerprint together. A table edit about those
|
|
classes moves the version the way a resolver change does (I2); an edit about
|
|
other classes moves nothing. A pending item stays applicable while the bytes
|
|
still offer its chosen candidate, and a rescan's new item supersedes it only
|
|
while it is pending and undecided (never beside a decision, see
|
|
``PromptDatabase.record_review_item``). Apply re-resolves with the
|
|
profile the item recorded (``captured_review(..., item_profile(evidence))``).
|
|
The inspection profile only ever produces review choices; ingestion's own
|
|
links stay on the default profile, which never loads the table (D1).
|
|
|
|
The saver anchor (#390): the inspection profile resolves a file with its
|
|
name, as the workspace inspector does since #383, so the one saver whose
|
|
``filename_prefix`` names the file anchors its branch. When that anchor alone
|
|
resolves a file the default profile left ambiguous and nothing links
|
|
(``saver_anchored``), the scan queues it with the anchor's pair as its one
|
|
candidate (evidence ``anchor: "saver"``), never linking it: a file never
|
|
falls through both the link and the queue (maintainer ruling). No match,
|
|
several matches or an unfinished walk anchor nothing and the file is queued
|
|
as before. Apply re-resolves with the file's name, so only the anchored
|
|
branch can be applied. ``REVIEW_RULE_VERSION`` is folded into inspection
|
|
item versions, so inspection items recorded before the anchor are recorded
|
|
again by the next scan.
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
import hashlib
|
|
import json
|
|
import logging
|
|
import os
|
|
from dataclasses import astuple
|
|
from pathlib import Path
|
|
from typing import Any
|
|
|
|
try:
|
|
from ..database.library_commands import LibraryCommandError
|
|
from ..database.review_schema import (
|
|
MAX_CANDIDATES,
|
|
MIN_CHOOSABLE,
|
|
canonical_evidence,
|
|
choosable,
|
|
)
|
|
from .inspection_semantics import (
|
|
SemanticsError,
|
|
get_semantics,
|
|
semantics_class_version,
|
|
)
|
|
from .media_capture import CaptureCapacity, CaptureError
|
|
from .prompt_inspector import MetadataReadError, read_inspection_file
|
|
from .prompt_pair_resolver import (
|
|
INSPECTION_PROFILE,
|
|
INSPECTION_RESOLVER_VERSION,
|
|
RESOLVER_VERSION,
|
|
resolve_prompt_pair,
|
|
)
|
|
except ImportError: # imported with the repo root on sys.path
|
|
from database.library_commands import LibraryCommandError
|
|
from database.review_schema import (
|
|
MAX_CANDIDATES,
|
|
MIN_CHOOSABLE,
|
|
canonical_evidence,
|
|
choosable,
|
|
)
|
|
from utils.inspection_semantics import (
|
|
SemanticsError,
|
|
get_semantics,
|
|
semantics_class_version,
|
|
)
|
|
from utils.media_capture import CaptureCapacity, CaptureError
|
|
from utils.prompt_inspector import MetadataReadError, read_inspection_file
|
|
from utils.prompt_pair_resolver import (
|
|
INSPECTION_PROFILE,
|
|
INSPECTION_RESOLVER_VERSION,
|
|
RESOLVER_VERSION,
|
|
resolve_prompt_pair,
|
|
)
|
|
|
|
logger = logging.getLogger("prompt_manager.review_capture")
|
|
|
|
# Capture scope label: review captures are never handed to a client.
|
|
REVIEW_SCOPE = "needs-review"
|
|
# Nested ambiguous stages are flattened at most this deep.
|
|
MAX_CANDIDATE_DEPTH = 8
|
|
# The resolver profile an item's evidence was built with (``profile``).
|
|
DEFAULT = "default"
|
|
INSPECTION = "inspection"
|
|
# The ``anchor`` an item recorded from the saver anchor names (#390).
|
|
SAVER_ANCHOR = "saver"
|
|
ANCHOR_REASON = "The saver that wrote this file names its branch"
|
|
# Inspection item versions: above every plausible ``RESOLVER_VERSION`` and,
|
|
# with 40 bits of the folded digest, below 2**53 (exact as a JSON number).
|
|
INSPECTION_VERSION_BASE = 1 << 40
|
|
_INSPECTION_VERSION_HEX = 10
|
|
# How the scan and Apply use the inspection profile, folded into inspection
|
|
# item versions. 1: they resolve with the file's name (the saver anchor, #390).
|
|
REVIEW_RULE_VERSION = 1
|
|
# How a resolution is stored as evidence, folded into every item version so
|
|
# the next record of a file supersedes its pending, undecided items of the
|
|
# older shape (decided ones are never touched). 0: the shape before it was
|
|
# versioned; 1: more than ``MAX_CANDIDATES`` candidates are collapsed, not
|
|
# limited (#657 addendum D3). ``RESOLVER_VERSION`` stays the ingestion's.
|
|
REVIEW_EVIDENCE_VERSION = 1
|
|
|
|
|
|
def default_item_version() -> int:
|
|
"""The ``resolver_version`` of an item recorded from the default profile.
|
|
|
|
``RESOLVER_VERSION`` plus ``REVIEW_EVIDENCE_VERSION``: both only ever
|
|
grow, so a bump of either gives a version no older item carries. Items
|
|
recorded before the evidence version existed carry ``RESOLVER_VERSION``.
|
|
"""
|
|
return RESOLVER_VERSION + REVIEW_EVIDENCE_VERSION
|
|
|
|
|
|
# Above this many distinct node classes the whole table is the fingerprint.
|
|
MAX_FINGERPRINT_CLASSES = 1024
|
|
|
|
|
|
def graph_classes(metadata: Any) -> frozenset | None:
|
|
"""The node classes of ``metadata``'s prompt graph (empty without one).
|
|
|
|
None when the graph names more than ``MAX_FINGERPRINT_CLASSES`` classes.
|
|
"""
|
|
graph = metadata.get("prompt") if isinstance(metadata, dict) else None
|
|
if not isinstance(graph, dict):
|
|
return frozenset()
|
|
classes = set()
|
|
for node in graph.values():
|
|
name = node.get("class_type") if isinstance(node, dict) else None
|
|
if isinstance(name, str):
|
|
classes.add(name)
|
|
if len(classes) > MAX_FINGERPRINT_CLASSES:
|
|
return None
|
|
return frozenset(classes)
|
|
|
|
|
|
def semantics_version(metadata: Any = None) -> str | None:
|
|
"""The semantics table fingerprint that ``metadata``'s graph depends on.
|
|
|
|
The table restricted to the graph's node classes
|
|
(``inspection_semantics.class_version``), so an edit about other classes
|
|
leaves it alone; the whole table's version without ``metadata`` or for a
|
|
graph of very many classes. None when the table is unusable.
|
|
"""
|
|
try:
|
|
semantics = get_semantics()
|
|
except SemanticsError:
|
|
return None
|
|
classes = graph_classes(metadata) if metadata is not None else None
|
|
if classes is None:
|
|
return semantics.version
|
|
return semantics_class_version(semantics, classes)
|
|
|
|
|
|
def inspection_review_version(metadata: Any = None) -> int | None:
|
|
"""The ``resolver_version`` of an item recorded from the inspection profile.
|
|
|
|
Folds the profile with its budgets, ``RESOLVER_VERSION``,
|
|
``INSPECTION_RESOLVER_VERSION``, ``REVIEW_RULE_VERSION``,
|
|
``REVIEW_EVIDENCE_VERSION`` and ``semantics_version(metadata)``, so a
|
|
change of either resolver, of how review uses the profile or stores its
|
|
evidence, of the profile's bounds or of the table about the graph's
|
|
classes moves it. None when the table cannot be loaded.
|
|
"""
|
|
table = semantics_version(metadata)
|
|
if table is None:
|
|
return None
|
|
profile = repr(astuple(INSPECTION_PROFILE))
|
|
seed = f"{profile}:{RESOLVER_VERSION}:{INSPECTION_RESOLVER_VERSION}:{table}"
|
|
if REVIEW_RULE_VERSION: # 0 is the seed items had before the rule existed
|
|
seed += f":review-rule-{REVIEW_RULE_VERSION}"
|
|
if REVIEW_EVIDENCE_VERSION: # 0: before the evidence shape was versioned
|
|
seed += f":evidence-{REVIEW_EVIDENCE_VERSION}"
|
|
folded = hashlib.sha256(seed.encode("utf-8")).hexdigest()
|
|
return INSPECTION_VERSION_BASE + int(folded[:_INSPECTION_VERSION_HEX], 16)
|
|
|
|
|
|
def resolve_inspection(metadata: dict, file_name: str | None = None) -> dict:
|
|
"""``metadata`` resolved with the inspection profile (bounded work).
|
|
|
|
``file_name`` (the file's last path part) anchors the file on the one
|
|
saver that wrote it, as in the workspace inspector (#390).
|
|
"""
|
|
return resolve_prompt_pair(
|
|
metadata, profile=INSPECTION_PROFILE, file_name=file_name
|
|
)
|
|
|
|
|
|
def inspection_choice(metadata: dict, file_name: str | None = None) -> dict | None:
|
|
"""The inspection profile's result when the file is a review case.
|
|
|
|
The scan and the review worker ask only for a file nothing links that
|
|
the default profile found ambiguous without a choice, or unresolved
|
|
(#659 C1). The result is ambiguous with a
|
|
choice, or ``resolved`` when ``file_name`` anchors the file on its saver
|
|
and only that anchor resolves it (``saver_anchored``): the item then
|
|
offers the anchor's pair. None otherwise.
|
|
"""
|
|
if inspection_review_version(metadata) is None:
|
|
return None
|
|
resolution = resolve_inspection(metadata, file_name)
|
|
if resolution.get("state") == "resolved":
|
|
return _anchor_of(metadata, file_name, resolution)
|
|
if resolution.get("state") != "ambiguous" or not reviewable(resolution):
|
|
return None
|
|
return resolution
|
|
|
|
|
|
def _anchor_of(metadata: Any, file_name: str | None, named: dict) -> dict | None:
|
|
"""``named`` tagged ``anchor: "saver"`` when only the saver anchor
|
|
resolves the file, to a pair with a positive text to choose; else None.
|
|
|
|
``named`` is ``metadata`` resolved with ``file_name``; the profile runs
|
|
once more without the name only when it is resolved and choosable.
|
|
"""
|
|
if not file_name or named.get("state") != "resolved":
|
|
return None
|
|
try:
|
|
offered = choosable(_projection(named))
|
|
except (KeyError, TypeError):
|
|
return None
|
|
if not offered or resolve_inspection(metadata).get("state") == "resolved":
|
|
return None
|
|
return {**named, "anchor": SAVER_ANCHOR}
|
|
|
|
|
|
def saver_anchored(metadata: Any, file_name: str | None) -> bool:
|
|
"""True when the saver that wrote ``file_name`` resolves its branch (#390).
|
|
|
|
The inspection profile resolves ``metadata`` with the file's name, and
|
|
without it the file does not resolve: the anchor alone decides it. A file
|
|
the default profile left ambiguous is then queued with the anchor's pair
|
|
as its one candidate. No name, an unusable table, no matching saver,
|
|
several, or an unfinished walk anchor nothing (False). The profile
|
|
without the name runs only when the named run resolved.
|
|
"""
|
|
return _anchored(metadata, file_name) is not None
|
|
|
|
|
|
def _anchored(metadata: Any, file_name: str | None) -> dict | None:
|
|
"""The anchored result ``saver_anchored`` describes, else None."""
|
|
if not file_name or not isinstance(metadata, dict):
|
|
return None
|
|
if inspection_review_version(metadata) is None:
|
|
return None
|
|
return _anchor_of(metadata, file_name, resolve_inspection(metadata, file_name))
|
|
|
|
|
|
def item_profile(evidence: Any) -> str:
|
|
"""The profile an item's stored evidence was built with."""
|
|
if isinstance(evidence, dict) and evidence.get("profile") == INSPECTION:
|
|
return INSPECTION
|
|
return DEFAULT
|
|
|
|
|
|
def item_versions(metadata: Any = None) -> tuple:
|
|
"""The item versions a file with ``metadata`` may already be queued under.
|
|
|
|
Without ``metadata``, only the default item version.
|
|
"""
|
|
version = inspection_review_version(metadata) if metadata is not None else None
|
|
default = default_item_version()
|
|
return (default,) if version is None else (default, version)
|
|
|
|
|
|
def _same_records(captured: dict, scanned: dict) -> bool:
|
|
"""True when the capture holds the records the scan resolved."""
|
|
keys = (set(captured) | set(scanned)) - {"file_info"}
|
|
return all(captured.get(key) == scanned.get(key) for key in keys)
|
|
|
|
|
|
def record_ambiguous_file(
|
|
db,
|
|
store,
|
|
root,
|
|
root_index: int,
|
|
relative: str,
|
|
*,
|
|
inspection: bool = False,
|
|
scanned: tuple | None = None,
|
|
) -> str:
|
|
"""Persist the ambiguous evidence of one rooted file.
|
|
|
|
With ``inspection`` (the scan and the review worker), a default result
|
|
without a choice, or an ``unresolved`` one of a file with a prompt graph
|
|
(#659 C1), is resolved again with the inspection profile, and recorded
|
|
from it when that offers a choice (or only the saver anchor resolves
|
|
it); otherwise the answer is ``untraced``. ``scanned`` is the
|
|
scan's ``(metadata, inspection_resolution_or_None)`` for the file: its
|
|
metadata names the item versions that count, and when the capture holds
|
|
the same records its resolution is reused instead of resolving again.
|
|
|
|
A new item supersedes the file's other items of the same bytes (an older
|
|
resolver or table version) when every one is still pending and
|
|
undecided; when one carries a decision, nothing new is stored.
|
|
|
|
Returns ``recorded`` (new item with candidates), ``limited`` (new item
|
|
without candidates), ``duplicate`` (these bytes already carry this
|
|
evidence, or the file's current stat is the verified fingerprint of
|
|
bytes an item cites, so it is not even copied, and that item is still
|
|
pending), ``decided`` (as ``duplicate``, but the item carries a decision,
|
|
so the file is not in the queue), ``untraced`` (ambiguous in its captured
|
|
bytes, but not ``reviewable``: nothing to choose, nothing stored) or
|
|
``skipped`` (the file
|
|
changed, vanished, is not ambiguous in its captured bytes, or storage was
|
|
restored meanwhile).
|
|
``CaptureCapacity`` and database errors propagate to the caller, which
|
|
logs and counts them.
|
|
"""
|
|
versions = item_versions(scanned[0] if scanned is not None else None)
|
|
spelled_path, queued = _queued_spelling(db, root, relative, versions)
|
|
if queued is not None and not (
|
|
inspection and _default_item_to_anchor(db, queued, scanned, relative)
|
|
):
|
|
return _already_queued(db, *queued, versions)
|
|
epoch = db.get_execution_epoch()
|
|
try:
|
|
# Background: the last worker stays free for an interactive request.
|
|
capture = store.capture(
|
|
root,
|
|
relative,
|
|
epoch=epoch,
|
|
scope=REVIEW_SCOPE,
|
|
root_index=root_index,
|
|
background=True,
|
|
)
|
|
except CaptureCapacity:
|
|
raise
|
|
except CaptureError as error:
|
|
logger.debug("Review capture skipped (%s): %s", error.code, error)
|
|
return "skipped"
|
|
try:
|
|
captured = captured_review(store, capture)
|
|
finally:
|
|
store.release(capture.token)
|
|
resolution = captured[1] if captured is not None else None
|
|
if resolution is None:
|
|
return "skipped"
|
|
# #659 C1: a graph the default profile cannot read at all may still be
|
|
# one the inspection profile offers a choice in.
|
|
unreadable = (
|
|
inspection
|
|
and resolution.get("state") == "unresolved"
|
|
and isinstance(captured[0].get("prompt"), dict)
|
|
)
|
|
if not unreadable and resolution.get("state") != "ambiguous":
|
|
return "skipped"
|
|
profile, version = DEFAULT, default_item_version()
|
|
named = _captured_inspection(captured[0], relative, scanned) if inspection else None
|
|
anchored = named is not None and named[0].get("anchor") == SAVER_ANCHOR
|
|
if anchored:
|
|
# Only the saver anchor resolves it (#390): offer that one pair.
|
|
profile, version = INSPECTION, named[1]
|
|
resolution = {
|
|
"state": "ambiguous",
|
|
"reason": ANCHOR_REASON,
|
|
"candidates": [named[0]],
|
|
}
|
|
elif unreadable or not reviewable(resolution):
|
|
if named is None:
|
|
return "untraced"
|
|
profile, (resolution, version) = INSPECTION, named
|
|
if resolution.get("state") != "ambiguous" or not reviewable(resolution):
|
|
return "untraced"
|
|
elif named is not None and offers_more(named[0], resolution):
|
|
# The default result left branches unread that the inspection
|
|
# profile reads (#657 review): offer its fuller choice.
|
|
profile, (resolution, version) = INSPECTION, named
|
|
evidence, count, limited = review_evidence(
|
|
resolution, profile, version, captured[0], anchored=anchored
|
|
)
|
|
try:
|
|
inserted = db.record_review_item(
|
|
locator=capture.locator,
|
|
spelled_path=spelled_path,
|
|
sha256=capture.sha256,
|
|
size=capture.size,
|
|
mtime_ns=capture.mtime_ns,
|
|
epoch=capture.epoch,
|
|
resolver_version=version,
|
|
evidence=evidence,
|
|
candidate_count=count,
|
|
limited=limited,
|
|
)
|
|
except LibraryCommandError:
|
|
return "skipped"
|
|
if not inserted:
|
|
return _already_queued(
|
|
db, capture.locator, capture.size, capture.mtime_ns, versions
|
|
)
|
|
return "limited" if limited else "recorded"
|
|
|
|
|
|
def _default_item_to_anchor(db, queued: tuple, scanned, relative: str) -> bool:
|
|
"""True when the bytes' only pending item is a current default item
|
|
(recorded without the anchor, as before #390) and the saver anchor alone
|
|
resolves the scanned records.
|
|
|
|
The file is then captured again so the anchored item supersedes that
|
|
pending, undecided item. Decided bytes and bytes already queued under
|
|
the current inspection version are never re-read.
|
|
"""
|
|
if scanned is None or scanned[0] is None:
|
|
return False
|
|
version = inspection_review_version(scanned[0])
|
|
if version is None or db.review_pending_at(*queued, version):
|
|
return False
|
|
if not db.review_pending_at(*queued, default_item_version()):
|
|
return False
|
|
return _anchored(scanned[0], _file_name(relative)) is not None
|
|
|
|
|
|
def _captured_inspection(metadata: dict, relative: str, scanned) -> tuple | None:
|
|
"""``(resolution, item_version)`` of the captured records under the
|
|
inspection profile, anchored on the file's name; None when the table is
|
|
unusable.
|
|
|
|
A result tagged ``anchor: "saver"`` means the saver anchor alone
|
|
resolves the file (#390); any other resolved result is returned as
|
|
``unresolved`` (no review case, as before). The scan's result is reused
|
|
when the capture holds the same records (``inspection_choice`` applies
|
|
the same rule).
|
|
"""
|
|
version = inspection_review_version(metadata)
|
|
if version is None:
|
|
return None
|
|
if (
|
|
scanned is not None
|
|
and scanned[1] is not None
|
|
and _same_records(metadata, scanned[0])
|
|
):
|
|
return scanned[1], version # the same records: no second run
|
|
name = _file_name(relative)
|
|
resolution = resolve_inspection(metadata, name)
|
|
if resolution.get("state") == "resolved":
|
|
# Resolved without the anchor too: no review case, as before.
|
|
resolution = _anchor_of(metadata, name, resolution) or {
|
|
**resolution,
|
|
"state": "unresolved",
|
|
}
|
|
return resolution, version
|
|
|
|
|
|
def _file_name(relative: str) -> str:
|
|
"""The last part of a ``/``-separated relative path (the saved name)."""
|
|
return relative.rsplit("/", 1)[-1]
|
|
|
|
|
|
def _already_queued(db, locator: str, size: int, mtime_ns: int, versions) -> str:
|
|
"""``duplicate`` while an existing item is pending, else ``decided``."""
|
|
for version in versions:
|
|
if db.review_pending_at(locator, size, mtime_ns, version):
|
|
return "duplicate"
|
|
return "decided"
|
|
|
|
|
|
def _queued_spelling(db, root, relative: str, versions) -> tuple[str, tuple | None]:
|
|
"""The file's spelling, and ``(locator, size, mtime_ns)`` when queued.
|
|
|
|
The second value is None unless the file's current stat is already
|
|
queued.
|
|
|
|
The spelling is the capture's own candidate path (the real root joined
|
|
with the listed relative path), so it folds to the capture's locator. A
|
|
file whose size and ``mtime_ns`` equal the verified fingerprint of bytes
|
|
an item already cites is not copied or hashed again (maintainer ruling:
|
|
the rescan stays cheap and the file stays in the default scope); its
|
|
items take the current spelling. A stat failure leaves the decision to
|
|
the capture. ``versions`` are the item versions that count; bytes whose
|
|
item carries a decision count under any version (a new version never
|
|
adds an item beside a decision).
|
|
"""
|
|
spelled = os.path.join(os.path.realpath(os.fspath(root)), *relative.split("/"))
|
|
try:
|
|
facts = os.lstat(spelled)
|
|
except OSError:
|
|
return spelled, None
|
|
locator = os.path.normcase(spelled)
|
|
if db.review_decided_at(locator, facts.st_size, facts.st_mtime_ns) or any(
|
|
db.review_fingerprint_queued(locator, facts.st_size, facts.st_mtime_ns, v)
|
|
for v in versions
|
|
):
|
|
# Not copied again, but a case-only rename keeps the stat: record
|
|
# the current spelling so the item's target still opens.
|
|
db.refresh_review_spelling(locator, spelled)
|
|
return spelled, (locator, facts.st_size, facts.st_mtime_ns)
|
|
return spelled, None
|
|
|
|
|
|
def captured_review(
|
|
store, capture, profile: str = DEFAULT, file_name: str | None = None
|
|
) -> tuple[dict, dict] | None:
|
|
"""``(metadata, resolution)`` of the capture's own bytes, else None.
|
|
|
|
``metadata`` is what the scan hands the ingester: prompt and workflow
|
|
JSON-decoded and ``file_info`` (size, dimensions, format) from the
|
|
capture. ``resolution`` comes from ``profile`` (``DEFAULT`` or
|
|
``INSPECTION``; the latter anchored on ``file_name``, the file's own
|
|
name, as the item was recorded). With ``file_name`` (Apply), a default
|
|
result its saver anchor alone resolves (``saver_anchored``) is that
|
|
anchored result, so only the anchored branch is still offered. None
|
|
when the capture is gone or carries no prompt metadata.
|
|
"""
|
|
if store.acquire(capture.token) is None:
|
|
return None
|
|
try:
|
|
try:
|
|
read = read_inspection_file(Path(capture.path))
|
|
except MetadataReadError:
|
|
return None
|
|
finally:
|
|
store.release_use(capture.token)
|
|
canonical = dict(read.metadata)
|
|
for key in ("prompt", "workflow"):
|
|
if isinstance(canonical.get(key), str):
|
|
try:
|
|
canonical[key] = json.loads(canonical[key])
|
|
except (ValueError, TypeError, RecursionError):
|
|
# Malformed metadata stays as text for the resolver to reject.
|
|
pass
|
|
if not canonical.get("prompt") and not canonical.get("parameters"):
|
|
return None
|
|
info = {"size": capture.size}
|
|
if read.width is not None and read.height is not None:
|
|
info["dimensions"] = [read.width, read.height]
|
|
if read.format is not None:
|
|
info["format"] = read.format
|
|
canonical["file_info"] = info
|
|
if profile == INSPECTION:
|
|
named = resolve_inspection(canonical, file_name)
|
|
return canonical, _anchor_of(canonical, file_name, named) or named
|
|
resolution = resolve_prompt_pair(canonical)
|
|
if file_name is not None and resolution.get("state") == "ambiguous":
|
|
anchored = _anchored(canonical, file_name)
|
|
if anchored is not None:
|
|
# Its saver names its branch (#390): only that one is offered.
|
|
return canonical, anchored
|
|
return canonical, resolution
|
|
|
|
|
|
def reviewable(resolution: Any) -> bool:
|
|
"""True when an ambiguous result offers a choice: ``MIN_CHOOSABLE`` of its
|
|
selectable candidates (nested ambiguous stages flattened) are choosable.
|
|
|
|
Counted before the evidence is bounded, so a ``limited`` item (more than
|
|
``MAX_CANDIDATES`` candidates) with prompts to choose is still queued.
|
|
"""
|
|
if not isinstance(resolution, dict) or resolution.get("state") != "ambiguous":
|
|
return False
|
|
count = 0
|
|
for candidate in _flattened(resolution.get("candidates"), 0):
|
|
if choosable(candidate):
|
|
count += 1
|
|
if count >= MIN_CHOOSABLE:
|
|
return True
|
|
return False
|
|
|
|
|
|
def _choosable_count(resolution: Any) -> int:
|
|
if not isinstance(resolution, dict):
|
|
return 0
|
|
return sum(1 for c in _flattened(resolution.get("candidates"), 0) if choosable(c))
|
|
|
|
|
|
def partial_choice(resolution: Any) -> bool:
|
|
"""True when an ambiguous result left a branch without a prompt to
|
|
choose, or offers fewer than two: another profile may read more."""
|
|
candidates = list(_flattened((resolution or {}).get("candidates"), 0))
|
|
offered = sum(1 for c in candidates if choosable(c))
|
|
return offered < 2 or offered < len(candidates)
|
|
|
|
|
|
def offers_more(inspected: Any, default: Any) -> bool:
|
|
"""True when the inspection result ``inspected`` is a review case that
|
|
offers more choosable candidates than the partial ``default`` result."""
|
|
if not isinstance(inspected, dict) or inspected.get("state") != "ambiguous":
|
|
return False
|
|
if not partial_choice(default) or not reviewable(inspected):
|
|
return False
|
|
return _choosable_count(inspected) > _choosable_count(default)
|
|
|
|
|
|
def review_evidence(
|
|
resolution: dict,
|
|
profile: str = DEFAULT,
|
|
version: int | None = None,
|
|
metadata: Any = None,
|
|
anchored: bool = False,
|
|
) -> tuple[dict, int, bool]:
|
|
"""``(evidence, candidate_count, limited)`` for one ambiguous result.
|
|
|
|
Inspection evidence also names its ``profile`` and ``semantics``, the
|
|
table fingerprint of ``metadata``'s graph it was resolved with;
|
|
``version`` is the item's version. ``anchored`` evidence (the saver
|
|
anchor's one pair, #390) also names ``anchor: "saver"``.
|
|
"""
|
|
base = {
|
|
"resolver_version": default_item_version() if version is None else version,
|
|
"reason": _text(resolution.get("reason")),
|
|
}
|
|
if profile == INSPECTION:
|
|
base["profile"] = INSPECTION
|
|
base["semantics"] = semantics_version(metadata)
|
|
if anchored:
|
|
base["anchor"] = SAVER_ANCHOR
|
|
candidates, total, projected = _projected(resolution)
|
|
if not projected:
|
|
return _limited(base, total), 0, True
|
|
if len(candidates) > MAX_CANDIDATES:
|
|
candidates, collapsed = _bounded(candidates)
|
|
if collapsed is not None:
|
|
base["collapsed"] = collapsed
|
|
if not candidates:
|
|
return _limited(base, total), 0, True
|
|
evidence = {**base, "candidates": candidates}
|
|
try:
|
|
canonical_evidence(evidence)
|
|
except ValueError:
|
|
return _limited(base, total), 0, True
|
|
return evidence, len(candidates), False
|
|
|
|
|
|
def _projected(resolution: dict) -> tuple[list, int, bool]:
|
|
"""``(candidates, total, projected)``: each selectable candidate of
|
|
``resolution`` projected with its ``hash``, duplicates dropped.
|
|
|
|
``projected`` is False when a candidate cannot be projected; ``total``
|
|
then counts the candidates read up to it.
|
|
"""
|
|
candidates: list[dict] = []
|
|
seen: set[str] = set()
|
|
total = 0
|
|
for candidate in _flattened(resolution.get("candidates"), 0):
|
|
total += 1
|
|
try:
|
|
projected = _projection(candidate)
|
|
digest = hashlib.sha256(
|
|
canonical_evidence(projected).encode("utf-8")
|
|
).hexdigest()
|
|
except (ValueError, TypeError, KeyError):
|
|
return candidates, total, False
|
|
if digest in seen:
|
|
total -= 1
|
|
continue
|
|
seen.add(digest)
|
|
candidates.append({**projected, "hash": digest})
|
|
return candidates, total, True
|
|
|
|
|
|
def candidate_hashes(resolution: Any) -> frozenset:
|
|
"""Every candidate hash ``resolution`` offers, before any bound or
|
|
collapse (Apply's still-offered check); empty when it cannot be
|
|
projected."""
|
|
if not isinstance(resolution, dict):
|
|
return frozenset()
|
|
candidates, _total, projected = _projected(resolution)
|
|
return frozenset(c["hash"] for c in candidates) if projected else frozenset()
|
|
|
|
|
|
def _limited(base: dict, total: int) -> dict:
|
|
base = {key: value for key, value in base.items() if key != "collapsed"}
|
|
return {**base, "limited": True, "candidate_total": total, "candidates": []}
|
|
|
|
|
|
def _bounded(candidates: list) -> tuple[list, dict | None]:
|
|
"""At most ``MAX_CANDIDATES`` of more than that many candidates.
|
|
|
|
One candidate per choosable (positive, negative) text, its first
|
|
occurrence. With more texts than fit, the most used are shown and
|
|
``collapsed`` describes them (``_collapsed``). Otherwise every text is
|
|
kept, the branches without a prompt fill the remaining places in their
|
|
order, and ``collapsed`` is None (#657 review: an ordinary item).
|
|
"""
|
|
groups = _text_groups(candidates)
|
|
if not groups:
|
|
return [], None # nothing to choose: limited, never stored
|
|
if len(groups) > MAX_CANDIDATES:
|
|
return _collapsed(candidates, groups)
|
|
shown = {id(group[0]) for group in groups.values()}
|
|
room = MAX_CANDIDATES - len(groups)
|
|
kept = []
|
|
for candidate in candidates:
|
|
if id(candidate) in shown:
|
|
kept.append(candidate)
|
|
elif room > 0 and not choosable(candidate):
|
|
kept.append(candidate)
|
|
room -= 1
|
|
return kept, None
|
|
|
|
|
|
def _text_groups(candidates: list) -> dict[str, list]:
|
|
"""The choosable candidates grouped by their (positive, negative)
|
|
texts, in order of first appearance."""
|
|
groups: dict[str, list] = {}
|
|
for candidate in candidates:
|
|
if not choosable(candidate):
|
|
continue
|
|
key = canonical_evidence(
|
|
{
|
|
"positive": _side_text(candidate["positive"]),
|
|
"negative": _side_text(candidate["negative"]),
|
|
}
|
|
)
|
|
groups.setdefault(key, []).append(candidate)
|
|
return groups
|
|
|
|
|
|
def _collapsed(candidates: list, groups: dict[str, list]) -> tuple[list, dict]:
|
|
"""``(shown, collapsed)`` for more than ``MAX_CANDIDATES`` texts (D3).
|
|
|
|
The ``groups`` of choosable candidates (``_text_groups``) are ordered by
|
|
size, largest first, then by first
|
|
appearance, and the first ``MAX_CANDIDATES`` are shown, each by its first
|
|
occurrence (a real sampler and hash, so Apply finds it again).
|
|
``collapsed`` is ``{shown, total, paths}``: groups shown, distinct
|
|
choosable texts and candidates in all.
|
|
"""
|
|
# A stable sort: equal sizes keep their first-appearance order.
|
|
ranked = sorted(groups.values(), key=len, reverse=True)
|
|
shown = [group[0] for group in ranked[:MAX_CANDIDATES]]
|
|
collapsed = {"shown": len(shown), "total": len(groups), "paths": len(candidates)}
|
|
return shown, collapsed
|
|
|
|
|
|
def _side_text(side: Any) -> Any:
|
|
return side.get("text") if isinstance(side, dict) else None
|
|
|
|
|
|
def _flattened(candidates: Any, depth: int):
|
|
"""Selectable candidates; a nested ambiguous stage yields its own."""
|
|
if not isinstance(candidates, list):
|
|
return
|
|
for candidate in candidates:
|
|
if not isinstance(candidate, dict):
|
|
continue
|
|
nested = candidate.get("candidates")
|
|
if (
|
|
candidate.get("state") == "ambiguous"
|
|
and isinstance(nested, list)
|
|
and nested
|
|
and depth < MAX_CANDIDATE_DEPTH
|
|
):
|
|
yield from _flattened(nested, depth + 1)
|
|
else:
|
|
yield candidate
|
|
|
|
|
|
def _projection(candidate: dict) -> dict:
|
|
return {
|
|
"sampler_id": candidate.get("sampler_id"),
|
|
"state": _text(candidate.get("state")),
|
|
"reason": _text(candidate.get("reason")),
|
|
"positive": _side(candidate.get("positive")),
|
|
"negative": _side(candidate.get("negative")),
|
|
}
|
|
|
|
|
|
def _side(side: Any) -> dict | None:
|
|
if not isinstance(side, dict):
|
|
return None
|
|
text = side["text"]
|
|
return {
|
|
"text": text,
|
|
"storage_text": side.get("storage_text", text),
|
|
"provenance": side.get("provenance"),
|
|
}
|
|
|
|
|
|
def _text(value: Any) -> str:
|
|
return value if isinstance(value, str) else ""
|