* feat(inspect): add a declarative inspection node semantics table
* fix(inspect): bound and isolate the semantics table
Bound the semantics loader: at most 1 MiB is read, containers may nest
at most eight levels, and a RecursionError from the JSON parser becomes
a SemanticsError instead of escaping.
The table is no longer loaded at import. get_semantics() reads it on
first use and caches only a valid result, and the resolver imports it
lazily, only for a profile with semantics on. The default ingestion
profile never imports the module, so a missing or corrupt table cannot
break scan, monitor or admin import. Inspection of a prompt graph with
an unusable table is unresolved with the reason "Inspection node
semantics are unavailable" and an error log, rather than a crash.
INSPECTION_PROFILE now has semantics on; no strategy consumes the table
yet, so resolution is unchanged.
* feat(inspect): resolve guider-based samplers
Under INSPECTION_PROFILE the resolver discovers samplers from the
semantics table and follows SamplerCustomAdvanced through its guider
link (Reroutes allowed) to a listed guider:
- CFGGuider: positive and negative, so the pair resolves.
- BasicGuider: positive only. The state is "absent" with the reason
"Guider has no negative input", the existing missing-negative path.
- Any other guider, including DualCFGGuider (D17), is unsupported-node
and names that guider.
Guider hops draw visits, scan and depth from the existing budgets. A
SamplerCustomAdvanced upstream in latent ancestry is an earlier stage,
so the result stays multistage-ambiguous. Positive and negative
provenance record guider_id. FaceDetailer (detailer-stage) is not a
candidate yet: deciding it needs output ancestry. DEFAULT_PROFILE never
loads the table and is unchanged.
Vector changes (inspection profile only, default vectors unchanged):
- f07b: no-sampler -> resolved (CFGGuider).
- f17: no-sampler -> unsupported-node (FluxGuidance, Task 2.3).
- f01b: no-sampler -> unsupported-node (EmptyFlux2LatentImage latent,
Task 2.5).
- f01: no-sampler -> ambiguous (24 SamplerCustomAdvanced branches).
The no-sampler tests (resolver and inspect route) now use a sampler
class the inspector does not know, because f17 has a supported one.
* feat(inspect): trace conditioning through supported pass-through and selector nodes
* feat(inspect): treat detailers as sampler stages
* fix(inspect): choose selectors that share one condition together
* fix(inspect): bound detailer stage chains by the depth limit
* feat(inspect): support common text encoders and string sources
* fix(inspect): keep pair execution evidence behind unreadable string sources
* feat(inspect): accept any non-sampler latent origin
* fix(inspect): queue each node once in breadth-first ancestry walks
The latent and detailer image walks queued every link before checking
whether its node was already seen, and popped from the list head. A dense
shared fan-in grew the queue far past the visit budget before any visit
was charged. Both walks now use a deque and enqueue each node at most
once, charging every examined edge as scanned work; outcomes are
unchanged.
* fix(inspect): treat unlisted pipe and embed samplers as possible stages
The latent and detailer ancestry walks only flagged unlisted nodes with a
linked latent_image, guider, positive or negative input, so easy-use pipe
samplers, Impact pipe detailers and embed-driven video samplers were walked
through and the final sampler's prompt resolved silently. A separate
stage-shaped input set (adding pipes, embeds and custom-sampler parts)
now makes such nodes unsupported-node, named.
* fix(inspect): walk each sampler's latent ancestry once per resolution
Every selector branch run and candidate re-walked the same latent
ancestry, so conditioning-carrying origins such as WanImageToVideo turned
a three-selector graph from ambiguous into work-limit. The walk outcome is
now cached per sampler; branch runs of one candidate pay for it once and
other candidates are charged its visit cost, as the text memo does. Walks
that ran out of work are not cached. Every link input is still walked.
* fix(inspect): treat unlisted model-driven samplers as possible stages
Unlisted samplers that link a model with conditioning or a latent under
other input names (XlabsSampler, generic video samplers) were still walked
through, so the final sampler's prompt resolved silently. An unlisted node
with a linked model plus conditioning, neg_conditioning, samples or latent
is now unsupported-node, named; model patches such as LoraLoader and
ModelSampling nodes are not.
* feat(inspect): compute facts from the resolved sampler
* fix(inspect): merge facts across sampler branches when ambiguous
* fix(inspect): merge facts over unresolved stages and share stage inputs via the table
* feat(inspect): explain why a prompt could not be resolved
* feat(inspect): port legacy parser rules the new resolver missed (PromptManager)
R1: before 3.2.3 a save patch overwrote every PromptManager node's saved
text with the last-run node's prompt. The legacy viewer recognizes the
signature (comfy-metadata.js createContext: two or more PromptManager
workflow nodes with one API text and different widget values). Inspection
now refuses those nodes as malformed instead of resolving to another
node's prompt; the inspector hands the parsed workflow to the resolver for
this check only. Lands before the new pass-throughs (R6) that would
otherwise make these graphs resolve to the wrong text.
R7: this repository's node classes up to 07a5af3 (PromptManagerV2,
Positive, Negative, Detailer, V2Text, PositiveText, NegativeText) resolve
with their own outputs and their exact strip and ", " join.
The default (ingestion) profile ignores both.
* feat(inspect): port legacy parser rules the new resolver missed (A1111)
R3: an accumulating batch saver repeats the A1111 parameters record once
per batch item, identical except "Batch index: N". The bounded PNG reader
now keeps the last copy (Pillow's rule, which #165 relied on) instead of
calling the file unreadable. Any other difference, including a missing or
second batch index, is still a conflict, and decoding the copies draws from
the duplicate role budget.
R4: parameters text that starts with "Negative prompt:" has an empty
positive (#165), instead of showing the delimiter line as the positive.
R5: the settings footer may be followed by sd-dynamic-prompts' one-line
"Template:" and "Negative Template:" records, and may be "Steps: N" alone
when there is no full footer.
R10: commas inside A1111's quoted settings values no longer split them, so
an "ADetailer prompt" with a comma keeps the other facts known.
The facts' A1111 reader mirrors R4, R5 and R10; the default (ingestion)
profile keeps the historical split. Also from the Task 2.7 review: a guard
test fails on any uncoded unresolved reason without a user line (M1), and
the encoder bounds role lines to the client's 512 code units (M4).
* feat(inspect): port legacy parser rules the new resolver missed (installed nodes)
R6 and R9 for nodes whose sources are installed beside ComfyUI, each
entry's input names and output slots read from the node's code (the task
report lists file and line):
- detailer stages UltimateSDUpscale (image), UltimateSDUpscaleNoUpscale
(upscaled_image) and DetailerForEach (image), judged with their upstream
samplers like FaceDetailer;
- pass-throughs ControlNetInpaintingAliMamaApply and LTXVConditioning
(positive 0, negative 1) and FluxKontextMultiReferenceLatentMethod;
- encoder Text to Conditioning, strings TextInput and DisplayText;
- facts model pass-throughs Lora Loader (LoraManager), Power Lora Loader
(rgthree), ModelPatchTorchSettings, CFGNorm, PerturbedAttentionGuidance.
Impact Pack encodes a detailer's non-empty wildcard instead of its positive
input, so FaceDetailer and DetailerForEach now list it as a prompt
override: a literal wildcard is decided at run time and a linked one names
its source, instead of resolving to the positive input. The overwritten
PromptManager refusal (R1) still holds behind the new pass-throughs. The
default profile does not read the table.
* feat(inspect): port legacy parser rules the new resolver missed (upstream nodes)
R6 and R9 for nodes not installed beside ComfyUI here. Their input names
and output slots come from each pack's source at the commit the saved
workflows record in the node's cnr_id and ver properties (the task report
lists repository, commit, file and line):
- samplers ClownsharKSampler_Beta (RES4LYF), KSampler (Efficient) and
ttN KSampler_v2 (latent input "latent");
- pass-throughs SeedVarianceEnhancer and ApplyInstantIDAdvanced (positive
1, negative 2; output 0 is the model);
- selector Switch conditioning [Crystools];
- encoder smZ CLIPTextEncode: its text when with_SDXL is false; when true
the connected CLIP decides at run time which inputs are read, so the
prompt is known only when text, text_g and text_l agree;
- strings ttN text, CR Text, CR Prompt Text and TextBoxMira;
- facts model pass-through CR Apply LoRA Stack.
Inputs that replace what a sampler reads are not followed: RES4LYF options
(may carry a guider, and with sigmas replace the settings), an Efficiency
script and a ttN XY plot leave the prompt unsupported and the facts
unresolved. The overwritten PromptManager refusal (R1) still holds behind
SeedVarianceEnhancer. The default profile does not read the table.
* test(inspect): cover batch copies past the role budget and orphan widgets
* fix(inspect): match the batch index only in the settings footer
Review I-1: duplicate A1111 parameters copies were compared after blanking
"Batch index: N" anywhere in the record, so copies whose prompts differ
only by that phrase were accepted. The field is now blanked only inside
the record's one "Steps: N, ..." footer line, which must hold it exactly
once; any other difference stays a conflict.
Review M-4: the inspector parses the workflow record only when the API
graph can show the overwritten-PromptManager signature (a PromptManager
class, or a node with PromptManager's search_text input).
* fix(inspect): refuse overwritten PromptManager text inside subgraphs
Controller ruling on review M-1: PromptManager nodes inside subgraphs run
as "<instance>:<inner>" API IDs, nested instances chaining their IDs. The
overwritten-text check now walks subgraph instances (bounded in depth and
nodes) and reads a text widget stored by name, so these nodes are refused
like top-level ones, anchored or not. A definition without an instance
runs nothing and is ignored.
Review M-5: the historical adapters skip an explicit null prepend or append,
as the 07a5af3 code's "if prepend_text:" did.
* test(inspect): import unittest one way in the inspection suites
Code scanning flagged the mixed 'import unittest' and 'from unittest import
mock' forms. Use 'import unittest' plus 'import unittest.mock' everywhere.
* fix(history): name the recorded run's current-file prompts apart from its frozen fields
Now that the inspector resolves more saved files, the Recorded run dialog can
hold two regions named "Positive prompt" (and two of each Copy/Send action):
the frozen run field and the inline current file's prompt. Assistive
technology could not tell them apart, and the real-host chronology test read
two <pre> elements.
Prompt blocks take an optional context label. The inline current file inside
a recorded run names its regions "Current file positive prompt" and its
actions "Copy/Send current file positive prompt"; the visible headings stay
as in the concept. The real-host test now also scopes its frozen-field reads
to the "Frozen run prompt texts" section, and a disposable-server workflow
pins the distinct names with a resolved current file.
* fix(inspect): drop preview-only stages from unanchored candidates
A stage whose outputs reach only non-file outputs (PreviewImage and the other
non_file_outputs) cannot have written a saved file, yet it stayed an
unanchored candidate, so a preview-only FaceDetailer made the saved branch
ambiguous (f01b).
The inspection profile now drops such stages before detailer absorption,
using two linear backward passes over the graph (charged like the consumer
map; a failed pass keeps every stage). A stage without consumers, or one that
also reaches any other terminal, is kept, and nothing is dropped when every
stage is preview-only. Raw plus detailed double saves both write files and
stay ambiguous. A file in the host temp folder may itself be a preview, so it
is inspected with PREVIEW_INSPECTION_PROFILE, which keeps those stages.
Workspace facts follow the same choice: an absent or unresolved result that
names a sampler merges facts across that sampler's stages only, so the
dropped detailer's settings no longer blur the saved file's facts.
f01b now resolves (absent negative, guider has none); its vector is updated
and the expected failure removed. The default profile is unchanged:
differential against 39dcc40 identical over 160,256 cases. Over 60,054
inspection cases no previously resolved or absent result changed.
* fix(history): name the current file's prompt pair button apart from the frozen run's
The recorded run dialog nests the current-file inspector, whose footer repeated the "Send prompt pair" name. Name it from the prompt context, scope the real-host test to the frozen region, and add a dialog-wide duplicate-name check.
613 lines
23 KiB
Python
613 lines
23 KiB
Python
"""Read prompt evidence without inference, indexing, or decoding image pixels."""
|
|
|
|
import hashlib
|
|
import io
|
|
import json
|
|
import re
|
|
import struct
|
|
import subprocess # nosec B404
|
|
import threading
|
|
import zlib
|
|
from dataclasses import dataclass, field as dataclass_field
|
|
from pathlib import Path
|
|
from typing import Any
|
|
|
|
from PIL import Image
|
|
|
|
from .inspection_outcomes import UNREADABLE_METADATA_REASON, outcome_message
|
|
from .pair_execution import EXECUTION_KEY
|
|
from .prompt_pair_resolver import (
|
|
_FOOTER,
|
|
EMPTY_POSITIVE_REASON,
|
|
INSPECTION_PROFILE,
|
|
ResolverProfile,
|
|
resolve_prompt_pair,
|
|
)
|
|
from .video_metadata import ffprobe_path, is_video_path
|
|
|
|
# Role records (prompt, parameters, the pair execution record) share one
|
|
# budget, encoded and decoded: roles cannot be trusted from a partial record,
|
|
# so exceeding it fails the whole read. A workflow has its own budget; past it
|
|
# only the workflow is ``limited``. Other text chunks are never retained.
|
|
ROLE_METADATA_BYTES = 2 * 1024 * 1024
|
|
WORKFLOW_METADATA_BYTES = 4 * 1024 * 1024
|
|
# Total bytes read for byte-identical duplicate chunks of one file, per class
|
|
# so workflow repeats can never starve role records: two budgets each, making
|
|
# repeated chunks cost bounded I/O instead of file size.
|
|
DUPLICATE_WORKFLOW_BYTES = 2 * WORKFLOW_METADATA_BYTES
|
|
DUPLICATE_ROLE_BYTES = 2 * ROLE_METADATA_BYTES
|
|
MAX_METADATA_BYTES = ROLE_METADATA_BYTES # video stdout bound, unchanged here
|
|
MAX_KEYWORD_BYTES = 80 # a 1-79 byte PNG keyword plus its NUL separator
|
|
MAX_HEADER_BYTES = 8 * 1024 * 1024
|
|
MAX_PNG_CHUNKS = 10_000
|
|
_ROLE_METADATA_KEYS = frozenset({"prompt", "parameters", EXECUTION_KEY})
|
|
_METADATA_KEYS = _ROLE_METADATA_KEYS | {"workflow"}
|
|
_TEXT_CHUNKS = (b"tEXt", b"zTXt", b"iTXt")
|
|
# An accumulating batch saver writes one A1111 ``parameters`` record per batch
|
|
# item, identical except this field (R3); Pillow keeps the last one.
|
|
# Matched only inside the record's one ``Steps: N, ...`` settings footer line.
|
|
_BATCH_INDEX = re.compile(r"(?:^|(?<=, ))Batch index: \d+(?=, |$)")
|
|
|
|
|
|
def _batch_normalized(text: str) -> str | None:
|
|
"""``text`` with its footer's single batch index blanked, else ``None``."""
|
|
footers = list(_FOOTER.finditer(text))
|
|
if len(footers) != 1:
|
|
return None
|
|
line, count = _BATCH_INDEX.subn("Batch index: #", footers[0].group())
|
|
if count != 1:
|
|
return None
|
|
return text[: footers[0].start()] + line + text[footers[0].end() :]
|
|
|
|
|
|
_SIGNATURE = b"\x89PNG\r\n\x1a\n"
|
|
|
|
|
|
class MetadataReadError(ValueError):
|
|
"""Prompt evidence is unreadable, incomplete, or exceeds inspection limits."""
|
|
|
|
|
|
@dataclass(frozen=True)
|
|
class InspectionRead:
|
|
"""One bounded read, with its transient exact video source kept private."""
|
|
|
|
metadata: dict[str, str]
|
|
record_formats: dict[str, str]
|
|
video_carrier_text: str | None
|
|
format: str | None
|
|
width: int | None
|
|
height: int | None
|
|
# Records present but over their budget: no text, format ``limited``.
|
|
limited_records: frozenset[str] = dataclass_field(default_factory=frozenset)
|
|
|
|
|
|
@dataclass(frozen=True)
|
|
class _VideoInspection:
|
|
metadata: dict[str, str | None]
|
|
record_formats: dict[str, str]
|
|
carrier_text: str | None
|
|
|
|
|
|
def _inflate(data: bytes, limit: int) -> bytes | None:
|
|
"""Inflate at most ``limit + 1`` bytes; ``None`` when the output is over limit."""
|
|
inflater = zlib.decompressobj()
|
|
result = inflater.decompress(data, limit + 1)
|
|
if len(result) > limit:
|
|
return None
|
|
if not inflater.eof or inflater.unused_data:
|
|
raise MetadataReadError("Compressed metadata is invalid or exceeds limits")
|
|
return result
|
|
|
|
|
|
def _keyword(prefix: bytes) -> str:
|
|
keyword, separator, _ = prefix.partition(b"\0")
|
|
if not separator or not 1 <= len(keyword) <= 79:
|
|
raise MetadataReadError("Malformed PNG text metadata")
|
|
return keyword.decode("latin-1")
|
|
|
|
|
|
def _png_text(kind: bytes, data: bytes, limit: int) -> tuple[str, str | None]:
|
|
"""Decode one retained text chunk; ``None`` text when it inflates past limit."""
|
|
_, _, body = data.partition(b"\0")
|
|
key = _keyword(data[:MAX_KEYWORD_BYTES])
|
|
encoding = "latin-1"
|
|
if kind == b"zTXt":
|
|
if not body or body[0] != 0:
|
|
raise MetadataReadError("Unsupported PNG text compression")
|
|
body = _inflate(body[1:], limit)
|
|
elif kind == b"iTXt":
|
|
if len(body) < 2 or body[0] not in (0, 1):
|
|
raise MetadataReadError("Malformed international PNG metadata")
|
|
flag, method = body[:2]
|
|
fields = body[2:].split(b"\0", 2)
|
|
if len(fields) != 3 or (flag and method != 0):
|
|
raise MetadataReadError("Malformed international PNG metadata")
|
|
fields[0].decode("ascii")
|
|
fields[1].decode("utf-8")
|
|
body = _inflate(fields[2], limit) if flag else fields[2]
|
|
encoding = "utf-8"
|
|
if body is None:
|
|
return key, None
|
|
if b"\0" in body:
|
|
raise MetadataReadError("Malformed PNG text metadata")
|
|
return key, body.decode(encoding)
|
|
|
|
|
|
class _PngRecords:
|
|
"""Retained records with per-class budgets and byte-identical duplicates."""
|
|
|
|
def __init__(self) -> None:
|
|
self.found: dict[str, str] = {}
|
|
self.limited: set[str] = set()
|
|
self.digests: dict[str, tuple[int, bytes]] = {}
|
|
self.role_encoded = self.role_decoded = 0
|
|
self.duplicate_workflow = self.duplicate_role = 0
|
|
self.duplicate_decoded = 0
|
|
|
|
def skip(self, key: str, length: int) -> bool:
|
|
"""Decide from the keyword and length alone whether to seek past a chunk."""
|
|
if key not in _METADATA_KEYS or key in self.limited:
|
|
return True # never retained, or a duplicate of a limited workflow
|
|
if key in self.digests:
|
|
if self.digests[key][0] != length and key != "parameters":
|
|
raise MetadataReadError("Duplicate prompt metadata")
|
|
if key == "workflow":
|
|
if self.duplicate_workflow + length > DUPLICATE_WORKFLOW_BYTES:
|
|
self.limited.add(key)
|
|
self.found.pop(key, None)
|
|
return True
|
|
self.duplicate_workflow += length
|
|
else:
|
|
if self.duplicate_role + length > DUPLICATE_ROLE_BYTES:
|
|
raise MetadataReadError("Metadata exceeds inspection limits")
|
|
self.duplicate_role += length
|
|
return False # same length: read and compare bytes
|
|
if key == "workflow":
|
|
if length > WORKFLOW_METADATA_BYTES:
|
|
self.limited.add(key)
|
|
return True
|
|
return False
|
|
self.role_encoded += length
|
|
if self.role_encoded > ROLE_METADATA_BYTES:
|
|
raise MetadataReadError("Metadata exceeds inspection limits")
|
|
return False
|
|
|
|
def add(self, kind: bytes, data: bytes) -> None:
|
|
key = _keyword(data[:MAX_KEYWORD_BYTES])
|
|
digest = hashlib.sha256(kind + data).digest()
|
|
if key in self.digests:
|
|
if self.digests[key][1] != digest:
|
|
if key != "parameters":
|
|
raise MetadataReadError("Duplicate prompt metadata")
|
|
self.batch_copy(kind, data)
|
|
# The kept copy is the last one; later repeats compare with it.
|
|
self.digests[key] = (len(data), digest)
|
|
return
|
|
self.digests[key] = (len(data), digest)
|
|
workflow = key == "workflow"
|
|
limit = (
|
|
WORKFLOW_METADATA_BYTES
|
|
if workflow
|
|
else ROLE_METADATA_BYTES - self.role_decoded
|
|
)
|
|
_, value = _png_text(kind, data, limit)
|
|
size = None if value is None else len(value.encode("utf-8"))
|
|
if workflow:
|
|
if size is None or size > WORKFLOW_METADATA_BYTES:
|
|
self.limited.add(key)
|
|
return
|
|
else:
|
|
self.role_decoded += limit + 1 if size is None else size
|
|
if self.role_decoded > ROLE_METADATA_BYTES:
|
|
raise MetadataReadError("Metadata exceeds inspection limits")
|
|
self.found[key] = value
|
|
|
|
def batch_copy(self, kind: bytes, data: bytes) -> None:
|
|
"""Keep a later ``parameters`` copy that differs only in its batch index.
|
|
|
|
Each copy must carry exactly one ``Batch index: N`` settings field; any
|
|
other difference is still a conflict. Decoding every copy draws from the
|
|
duplicate role budget, so small compressed copies stay bounded.
|
|
"""
|
|
_, value = _png_text(kind, data, ROLE_METADATA_BYTES)
|
|
size = ROLE_METADATA_BYTES + 1 if value is None else len(value.encode())
|
|
self.duplicate_decoded += size
|
|
if value is None or self.duplicate_decoded > DUPLICATE_ROLE_BYTES:
|
|
raise MetadataReadError("Metadata exceeds inspection limits")
|
|
kept = self.found["parameters"]
|
|
normalized = _batch_normalized(kept)
|
|
if normalized is None or normalized != _batch_normalized(value):
|
|
raise MetadataReadError("Duplicate prompt metadata")
|
|
self.role_decoded += max(0, size - len(kept.encode()))
|
|
if self.role_decoded > ROLE_METADATA_BYTES:
|
|
raise MetadataReadError("Metadata exceeds inspection limits")
|
|
self.found["parameters"] = value
|
|
|
|
|
|
def _png_metadata(stream: Any, size: int) -> tuple[dict, frozenset, int, int]:
|
|
"""CRC-check retained text and structural chunks, seeking past everything else.
|
|
|
|
PNG text chunks may follow IDAT. Pillow's ``.text`` loads pixels to find
|
|
them; this bounded reader instead follows the W3C PNG chunk layout. A text
|
|
chunk's keyword is read first (at most ``MAX_KEYWORD_BYTES``); chunks that
|
|
are not retained, or are over their budget, are skipped by length, so their
|
|
bytes are never held in memory or checksummed. IDAT checksums/pixel
|
|
validity are deliberately outside metadata inspection.
|
|
"""
|
|
records = _PngRecords()
|
|
saw_header = saw_pixels = False
|
|
for index in range(MAX_PNG_CHUNKS):
|
|
header = stream.read(8)
|
|
if len(header) != 8:
|
|
raise MetadataReadError("Incomplete PNG metadata")
|
|
length, kind = struct.unpack(">I4s", header)
|
|
if length > 0x7FFFFFFF or stream.tell() + length + 4 > size:
|
|
raise MetadataReadError("Incomplete PNG chunk")
|
|
if index == 0 and (kind != b"IHDR" or length != 13):
|
|
raise MetadataReadError("Missing PNG header")
|
|
if kind in _TEXT_CHUNKS:
|
|
prefix = stream.read(min(length, MAX_KEYWORD_BYTES))
|
|
if records.skip(_keyword(prefix), length):
|
|
stream.seek(length - len(prefix) + 4, 1)
|
|
continue
|
|
data = prefix + stream.read(length - len(prefix))
|
|
elif kind in (b"IHDR", b"IEND"):
|
|
data = stream.read(length)
|
|
else:
|
|
saw_pixels |= kind == b"IDAT"
|
|
stream.seek(length + 4, 1)
|
|
continue
|
|
checksum = stream.read(4)
|
|
if zlib.crc32(kind + data) != int.from_bytes(checksum, "big"):
|
|
raise MetadataReadError("PNG metadata checksum mismatch")
|
|
if kind == b"IHDR":
|
|
if saw_header or not all(struct.unpack(">II", data[:8])):
|
|
raise MetadataReadError("Malformed PNG header")
|
|
saw_header = True
|
|
width, height = struct.unpack(">II", data[:8])
|
|
elif kind == b"IEND":
|
|
if length or not saw_pixels:
|
|
raise MetadataReadError("Incomplete PNG image")
|
|
return records.found, frozenset(records.limited), width, height
|
|
else:
|
|
records.add(kind, data)
|
|
raise MetadataReadError("PNG chunk count exceeds inspection limits")
|
|
|
|
|
|
def _video_metadata(path: Path) -> _VideoInspection:
|
|
probe = ffprobe_path()
|
|
if not probe:
|
|
raise MetadataReadError("Video metadata reader is unavailable")
|
|
command = [
|
|
probe,
|
|
"-v",
|
|
"quiet",
|
|
"-show_entries",
|
|
"format_tags=comment,description",
|
|
"-of",
|
|
"json",
|
|
str(path),
|
|
]
|
|
process = subprocess.Popen( # nosec B603
|
|
command,
|
|
stdout=subprocess.PIPE,
|
|
stderr=subprocess.DEVNULL,
|
|
creationflags=getattr(subprocess, "CREATE_NO_WINDOW", 0),
|
|
)
|
|
|
|
def stop() -> None:
|
|
if process.poll() is None:
|
|
try:
|
|
process.kill()
|
|
except OSError:
|
|
# Timeout and final cleanup can race with the process exiting.
|
|
pass
|
|
|
|
timer = threading.Timer(3, stop)
|
|
timer.daemon = True
|
|
timer.start()
|
|
try:
|
|
raw = process.stdout.read(MAX_METADATA_BYTES + 1)
|
|
if len(raw) > MAX_METADATA_BYTES:
|
|
raise MetadataReadError("Video metadata exceeds inspection limits")
|
|
process.wait()
|
|
if process.returncode:
|
|
raise MetadataReadError("Video metadata could not be read")
|
|
finally:
|
|
timer.cancel()
|
|
stop()
|
|
process.wait()
|
|
process.stdout.close()
|
|
probed = json.loads(raw)
|
|
if not isinstance(probed, dict) or not isinstance(probed.get("format"), dict):
|
|
raise MetadataReadError("Malformed video metadata")
|
|
tags = probed["format"].get("tags", {})
|
|
if not isinstance(tags, dict):
|
|
raise MetadataReadError("Malformed video metadata")
|
|
# The scan normalizer drops null fields and unknown keys. Inspection must
|
|
# retain explicit prompt/execution markers so malformed evidence cannot
|
|
# silently fall back to an unrelated saved recipe or another comment.
|
|
candidates = []
|
|
for key in ("comment", "description"):
|
|
raw = tags.get(key)
|
|
if not isinstance(raw, str):
|
|
continue
|
|
try:
|
|
envelope = json.loads(raw)
|
|
except ValueError:
|
|
continue
|
|
if not isinstance(envelope, dict):
|
|
continue
|
|
candidates.append((raw, envelope))
|
|
selected = next(
|
|
(item for item in candidates if _ROLE_METADATA_KEYS.intersection(item[1])),
|
|
None,
|
|
)
|
|
if selected is None:
|
|
selected = next((item for item in candidates if "workflow" in item[1]), None)
|
|
if selected is not None:
|
|
raw, envelope = selected
|
|
metadata, formats = {}, {}
|
|
for field in _METADATA_KEYS.intersection(envelope):
|
|
value = envelope[field]
|
|
if field == "workflow" and not isinstance(value, (str, dict)):
|
|
formats[field] = "invalid"
|
|
continue
|
|
metadata[field] = (
|
|
value if value is None or isinstance(value, str) else json.dumps(value)
|
|
)
|
|
formats[field] = (
|
|
"original-text" if isinstance(value, str) else "reserialized-json"
|
|
)
|
|
return _VideoInspection(metadata, formats, raw)
|
|
return _VideoInspection({}, {}, None)
|
|
|
|
|
|
def read_inspection_file(path: Path) -> InspectionRead:
|
|
"""Read bounded metadata and header facts without decoding media pixels."""
|
|
try:
|
|
formats, carrier, limited = {}, None, frozenset()
|
|
format_name = width = height = None
|
|
if is_video_path(path):
|
|
video = _video_metadata(path)
|
|
metadata, formats, carrier = (
|
|
video.metadata,
|
|
video.record_formats,
|
|
video.carrier_text,
|
|
)
|
|
else:
|
|
with path.open("rb") as stream:
|
|
signature = stream.read(8)
|
|
if signature == _SIGNATURE:
|
|
metadata, limited, width, height = _png_metadata(
|
|
stream, path.stat().st_size
|
|
)
|
|
format_name = "PNG"
|
|
else:
|
|
stream.seek(0)
|
|
header = stream.read(MAX_HEADER_BYTES)
|
|
with Image.open(io.BytesIO(header)) as image:
|
|
metadata = dict(image.info)
|
|
format_name, (width, height) = image.format, image.size
|
|
result, role_size, limited = {}, 0, set(limited)
|
|
for key in _METADATA_KEYS:
|
|
if key not in metadata:
|
|
continue
|
|
value = metadata[key]
|
|
if key == "workflow" and not isinstance(value, str):
|
|
formats[key] = "invalid"
|
|
continue
|
|
if not isinstance(value, str):
|
|
raise MetadataReadError("Malformed prompt metadata")
|
|
size = len(value.encode("utf-8", "surrogatepass"))
|
|
if key == "workflow":
|
|
if size > WORKFLOW_METADATA_BYTES:
|
|
limited.add(key)
|
|
continue
|
|
else:
|
|
role_size += size
|
|
if role_size > ROLE_METADATA_BYTES:
|
|
raise MetadataReadError("Metadata exceeds inspection limits")
|
|
result[key] = value
|
|
formats.setdefault(key, "original-text")
|
|
if _ROLE_METADATA_KEYS.intersection(limited):
|
|
# A limited role record would vanish and let fallback evidence win.
|
|
raise MetadataReadError("Metadata exceeds inspection limits")
|
|
for key in limited:
|
|
formats[key] = "limited"
|
|
return InspectionRead(
|
|
result,
|
|
formats,
|
|
carrier,
|
|
format_name,
|
|
width,
|
|
height,
|
|
frozenset(limited),
|
|
)
|
|
except MetadataReadError:
|
|
raise
|
|
except (
|
|
OSError,
|
|
ValueError,
|
|
EOFError,
|
|
SyntaxError,
|
|
RecursionError,
|
|
struct.error,
|
|
zlib.error,
|
|
):
|
|
raise MetadataReadError("Media metadata could not be read") from None
|
|
|
|
|
|
def read_inspection_metadata(path: Path) -> dict[str, str]:
|
|
"""Compatible metadata-only projection of the single inspection read."""
|
|
return read_inspection_file(path).metadata
|
|
|
|
|
|
def _side(status: str, text: str | None = None, source: str | None = None) -> dict:
|
|
return {"status": status, "text": text, "source": source}
|
|
|
|
|
|
_EMPTY_POSITIVE = outcome_message(
|
|
{"state": "unresolved", "reason": EMPTY_POSITIVE_REASON}
|
|
)
|
|
|
|
|
|
def _explained(prompts: dict, resolution: dict) -> dict:
|
|
"""Each unresolved role carries the outcome's line; other roles are kept."""
|
|
message = outcome_message(resolution)
|
|
if message is None:
|
|
return prompts
|
|
return {
|
|
role: (
|
|
{**side, "message": message}
|
|
if side["status"] == "unresolved" and "message" not in side
|
|
else side
|
|
)
|
|
for role, side in prompts.items()
|
|
}
|
|
|
|
|
|
def unreadable_evidence() -> dict:
|
|
"""Both roles when the file's metadata could not be read at all."""
|
|
resolution = {
|
|
"state": "unresolved",
|
|
"sampler_id": None,
|
|
"reason": UNREADABLE_METADATA_REASON,
|
|
"reason_code": None,
|
|
"node": None,
|
|
}
|
|
prompts = {role: _side("unresolved") for role in ("positive", "negative")}
|
|
return {"prompts": _explained(prompts, resolution), "resolution": resolution}
|
|
|
|
|
|
def _may_hold_prompt_manager(metadata: dict) -> bool:
|
|
"""Whether the API graph can show the R1 signature, so the workflow is needed.
|
|
|
|
PromptManager classes, or a ``CLIPTextEncode`` carrying PromptManager's own
|
|
``search_text`` input (an older save rewrote the class).
|
|
"""
|
|
graph = metadata.get("prompt")
|
|
if not isinstance(graph, dict):
|
|
return False
|
|
for node in graph.values():
|
|
if not isinstance(node, dict):
|
|
continue
|
|
name = node.get("class_type")
|
|
inputs = node.get("inputs")
|
|
if isinstance(name, str) and "promptmanager" in name.lower():
|
|
return True
|
|
if isinstance(inputs, dict) and "search_text" in inputs:
|
|
return True
|
|
return False
|
|
|
|
|
|
def _parsed_workflow(value: Any) -> dict | None:
|
|
"""The UI workflow as an object, or ``None`` (absent, invalid or not JSON)."""
|
|
if isinstance(value, str):
|
|
try:
|
|
value = json.loads(value)
|
|
except (ValueError, RecursionError):
|
|
return None
|
|
return value if isinstance(value, dict) else None
|
|
|
|
|
|
def inspect_prompt_evidence(
|
|
metadata: dict,
|
|
saved: dict | None = None,
|
|
*,
|
|
profile: ResolverProfile = INSPECTION_PROFILE,
|
|
) -> dict:
|
|
"""Keep uncertainty visible; a saved association never replaces file evidence.
|
|
|
|
Inspection routes resolve with ``INSPECTION_PROFILE``: bounded work, not
|
|
graph size. Ingestion (scan, live monitor, admin import) calls the resolver
|
|
directly with its default profile. ``resolution`` carries the closed
|
|
``reason_code`` and, for a blocking node, ``node: {id, class_type}``.
|
|
"""
|
|
workflow = metadata.get("workflow")
|
|
metadata = {
|
|
key: value for key, value in metadata.items() if key in _ROLE_METADATA_KEYS
|
|
}
|
|
resolution = {
|
|
"state": "unresolved",
|
|
"sampler_id": None,
|
|
"reason": "Prompt evidence is malformed",
|
|
"reason_code": "malformed",
|
|
"node": None,
|
|
}
|
|
if not metadata:
|
|
result = {role: _side("absent") for role in ("positive", "negative")}
|
|
if saved:
|
|
context = saved.get("pair_context")
|
|
paired = isinstance(saved.get("pair"), dict) or isinstance(context, dict)
|
|
for role in ("positive", "negative") if paired else ("positive",):
|
|
result[role] = {
|
|
**_side("unresolved", source="saved-association"),
|
|
"message": (
|
|
"A saved library association exists, but it cannot be "
|
|
"verified against this file."
|
|
),
|
|
}
|
|
return {
|
|
"prompts": result,
|
|
"resolution": {
|
|
"state": "absent",
|
|
"sampler_id": None,
|
|
"reason": "No parsed prompt metadata",
|
|
"reason_code": None,
|
|
"node": None,
|
|
},
|
|
}
|
|
canonical = dict(metadata)
|
|
malformed = {
|
|
"prompts": _explained(
|
|
{role: _side("unresolved") for role in ("positive", "negative")},
|
|
resolution,
|
|
),
|
|
"resolution": resolution,
|
|
}
|
|
if "prompt" not in canonical and "parameters" not in canonical:
|
|
return malformed
|
|
if isinstance(canonical.get("prompt"), str):
|
|
try:
|
|
canonical["prompt"] = json.loads(canonical["prompt"])
|
|
except (ValueError, RecursionError):
|
|
return malformed
|
|
workflow = (
|
|
_parsed_workflow(workflow) if _may_hold_prompt_manager(canonical) else None
|
|
)
|
|
if workflow is not None and "prompt" in canonical:
|
|
# Only to recognize overwritten PromptManager text (R1); never a role.
|
|
canonical["workflow"] = workflow
|
|
resolved = resolve_prompt_pair(canonical, profile=profile)
|
|
state = resolved["state"]
|
|
result = {}
|
|
for role in ("positive", "negative"):
|
|
side = resolved[role]
|
|
if (
|
|
state != "ambiguous"
|
|
and isinstance(side, dict)
|
|
and (role == "negative" or side["text"].strip())
|
|
):
|
|
result[role] = _side(
|
|
"resolved", side["text"], side["provenance"]["text_source"]
|
|
)
|
|
elif state == "resolved" and isinstance(side, dict):
|
|
# A resolved pair withholds only an empty positive.
|
|
result[role] = {**_side("unresolved"), "message": _EMPTY_POSITIVE}
|
|
else:
|
|
result[role] = _side(state if state != "resolved" else "unresolved")
|
|
resolution = {
|
|
"state": state,
|
|
"sampler_id": resolved.get("sampler_id"),
|
|
"reason": resolved.get("reason", ""),
|
|
"reason_code": resolved.get("reason_code"),
|
|
"node": resolved.get("node"),
|
|
}
|
|
return {"prompts": _explained(result, resolution), "resolution": resolution}
|
|
|
|
|
|
def inspect_prompt_roles(metadata: dict, saved: dict | None = None) -> dict:
|
|
"""Keep the established role-only helper contract."""
|
|
return inspect_prompt_evidence(metadata, saved)["prompts"]
|