Files
ComfyAssets-ComfyUI_PromptM…/py/api/inspection_service.py
T
Vito a0fc5171e3 fix(inspector): pass text through unknown nodes instead of refusing them (#381) (#383)
* fix(inspector): pass text through unknown nodes instead of refusing them (#381)

The inspector refused any node class missing from the semantics table, so a
PromptManagerPair behind an rgthree Any Switch showed "Prompt uses a node the
inspector does not support". The inspection resolver (Python and browser) now
reads through unknown nodes: a text input is the text, positive/negative route
by output slot, a conditioning input is followed alone, otherwise every linked
input except core type inputs (pass_through.skip_inputs). PromptManager nodes
are authoritative; several different texts become one candidate each
("Several prompts reach the sampler"). A seeded text node with no conditioning
input is generated at run time, and a recorded PromptManager execution is not
checked against text that passed through an unknown node.

INSPECTION_RESOLVER_VERSION goes to 2; ingestion keeps the default profile.
A legacy parity gate records every committed graph whose inspection positive
matches the legacy parser's; no graph may leave it.

* fix(inspector): keep recorded pair text, string-only seed rule, role inputs and unique candidates (#381)

Review of the pass-through: a walk through an unknown node never overrides a
PromptManagerPair's execution record (any outcome but work-limit is
unverifiable while the record is checked); the run-time seed rule reads only
string outputs; a node with positive/negative inputs follows only the role of
the requested slot, nothing when it is unlinked; one text offered twice is one
candidate.

* fix(inspector): anchor on the saver that wrote the file and resolve stages on shared links (#381)

An inspected file's name now anchors the inspection on the one saver whose
filename_prefix (literal or traced) makes the file's stem <prefix>_<digits>[_];
the samplers in that saver's image ancestry are the candidates. No match,
several matches or an unreadable walk anchor nothing, so the file stays
"several possible prompts". In a latent walk, an earlier sampler reading the
same conditioning links as the later one is the same prompt, not an
unattributed stage. Python and the browser resolver behave the same; the
legacy parity gate states each graph that differs from legacy on purpose.

* fix(inspector): skip only plain first stages on shared links; an unfinished prefix scan anchors nothing (#381)

Review of the saver anchor: a same-links earlier stage is skipped only when it
is not a detailer, sets no prompt override and continues no other sampler, so
an earlier prompt or override behind it is never hidden. A work limit while
tracing a saver prefix ends the match scan without an anchor instead of
counting that saver as no match.

* test(inspector): cover every plain-first-stage outcome on both resolvers (#381)

The frontend coverage gate found local-resolver-stages.mjs branches at 92.44%
(< 93). Each way an earlier stage stops being a plain first stage (no latent,
literal, missing or cyclic latent, a sampler-shaped node, a detailer) is now a
resolver test and an anchor-first-stage-* parity vector.
2026-10-27 16:56:37 +00:00

153 lines
4.6 KiB
Python

"""One bounded current-file inspection worker shared by physical identities."""
import os
import stat
from pathlib import Path
from typing import Callable
try:
from ...utils.prompt_inspector import (
MetadataReadError,
inspect_prompt_evidence,
read_inspection_file,
unreadable_evidence,
)
from ...utils.prompt_pair_resolver import (
INSPECTION_PROFILE,
PREVIEW_INSPECTION_PROFILE,
)
from ...utils.video_metadata import is_video_path
except ImportError:
from utils.prompt_inspector import (
MetadataReadError,
inspect_prompt_evidence,
read_inspection_file,
unreadable_evidence,
)
from utils.prompt_pair_resolver import (
INSPECTION_PROFILE,
PREVIEW_INSPECTION_PROFILE,
)
from utils.video_metadata import is_video_path
# Only these file types are served or inspected within an authorized directory.
IMAGE_EXTENSIONS = frozenset(
{
".png",
".jpg",
".jpeg",
".webp",
".gif",
".bmp",
".tiff",
".tif",
".mp4",
".webm",
".mov",
}
)
VIDEO_EXTENSIONS = frozenset({".mp4", ".webm", ".avi", ".mov", ".mkv", ".m4v", ".wmv"})
SERVABLE_EXTENSIONS = IMAGE_EXTENSIONS | VIDEO_EXTENSIONS
class CurrentFileChanged(Exception):
"""File facts are invalid or changed across the bounded metadata observation."""
def _current_file_facts(path: Path) -> os.stat_result:
if path.suffix.lower() not in SERVABLE_EXTENSIONS:
raise CurrentFileChanged
try:
observed = path.stat()
except (FileNotFoundError, NotADirectoryError, PermissionError):
raise CurrentFileChanged from None
if (
not stat.S_ISREG(observed.st_mode)
or not observed.st_mode & 0o444
or not os.access(path, os.R_OK)
):
raise CurrentFileChanged
return observed
def _in_host_temp(path: Path) -> bool:
"""Whether a file lies in ComfyUI's temp folder, where previews are written.
An unknown temp folder reads as no: the file is then inspected as saved
output.
"""
try:
import folder_paths
temp = folder_paths.get_temp_directory()
except (ImportError, AttributeError, OSError):
return False
if type(temp) is not str or not temp:
return False
try:
folder = os.path.normcase(os.path.realpath(temp))
target = os.path.normcase(os.path.realpath(path))
return os.path.commonpath([folder, target]) == folder
except (OSError, ValueError):
return False
def inspect_current_file(
path: Path,
*,
source_label: str,
saved: dict | None,
library: dict,
image_tags: Callable[[int, int], dict] | None = None,
) -> dict:
"""Current-file evidence read between two equal stats of *path*.
*image_tags*, when given, is called once with that stable
``(st_size, st_mtime_ns)`` and its facet is returned as ``image_tags``
(spec amendment S1: tags of a byte record verified at exactly that stat).
"""
before = _current_file_facts(path)
try:
inspection = read_inspection_file(path)
except MetadataReadError:
inspection = None
after = _current_file_facts(path)
fields = ("st_dev", "st_ino", "st_mode", "st_size", "st_mtime_ns", "st_ctime_ns")
if any(getattr(before, key) != getattr(after, key) for key in fields):
raise CurrentFileChanged
facets = {}
if image_tags is not None:
facets["image_tags"] = image_tags(before.st_size, before.st_mtime_ns)
if inspection is None:
evidence = unreadable_evidence()
else:
role_saved = (
None if saved and saved.get("association_status") == "ambiguous" else saved
)
# A preview may be the output of a stage that reaches only previews.
profile = (
PREVIEW_INSPECTION_PROFILE if _in_host_temp(path) else INSPECTION_PROFILE
)
# The file name anchors on the saver that wrote it (#381).
evidence = inspect_prompt_evidence(
inspection.metadata, role_saved, profile=profile, file_name=path.name
)
return {
"file": {
"observation": "current-file",
"source_label": source_label,
"media_type": "video" if is_video_path(path) else "image",
"extension": path.suffix.lower(),
"size": before.st_size,
"modified_time": before.st_mtime,
"format": None,
"width": None,
"height": None,
},
**evidence,
"inspection": inspection,
"library": library,
**facets,
}