fix(images): card facts read the inspector's projected graphs
The inspector hands its browser parser only graphs the inspection projection parsed (strict JSON, depth 64, node ceiling); a deeper record is "limited" there, so the legacy fill never runs and no seed shows. The card parsed the raw records instead and showed a seed the inspector did not, e.g. for a 900-deep class_type beside a normal sampler. project_graph() is now the one projection step, used by the inspection response and by the card. The card runs the shared parser on the projected graphs, the same values the browser receives: big integers arrive as their exact digits, so the separate exact-integer re-parse and the parse_js_json flag it needed are gone.
This commit is contained in:
+34
-63
@@ -1,24 +1,24 @@
|
||||
"""Workspace card facts from the metadata records a header open already loaded.
|
||||
|
||||
The card shows what the inspector shows, so it reads the same shared parser
|
||||
(``extract_generation_params``, the Python twin of the browser parser the
|
||||
inspector fills from since #251) and nothing else: no separate seed heuristic,
|
||||
no graph resolution and no extra file reads. One parse feeds both the seed and
|
||||
the positive that confirms a first-line card title.
|
||||
The card shows what the inspector shows, so it reads exactly what the
|
||||
inspector's browser parser reads: each ``prompt``/``workflow`` record through
|
||||
the inspection projection (``project_graph``: strict JSON, the depth and node
|
||||
bounds, big integers and decimals as text), and only graphs it parsed, through
|
||||
the shared parser (``extract_generation_params``, the Python twin of the
|
||||
browser parser the inspector fills from since #251). No separate seed
|
||||
heuristic, no graph resolution and no extra file reads. One parse feeds both
|
||||
the seed and the positive that confirms a first-line card title.
|
||||
"""
|
||||
|
||||
import math
|
||||
import re
|
||||
from collections.abc import Mapping
|
||||
|
||||
from .generation_params import (
|
||||
PLACEHOLDERS,
|
||||
GenerationParamsError,
|
||||
extract_from_records,
|
||||
extract_generation_params,
|
||||
number_string,
|
||||
parse_js_json,
|
||||
)
|
||||
from .inspection_response import project_graph
|
||||
|
||||
__all__ = ["MAX_RECORD_BYTES", "card_seed", "header_params", "parsed_positive"]
|
||||
|
||||
@@ -26,7 +26,6 @@ __all__ = ["MAX_RECORD_BYTES", "card_seed", "header_params", "parsed_positive"]
|
||||
MAX_RECORD_BYTES = 2 * 1024 * 1024
|
||||
_RECORD_KEYS = ("prompt", "workflow")
|
||||
_INTEGER = re.compile(r"-?[0-9]+\Z")
|
||||
_MAX_SAFE_INTEGER = 2**53 - 1
|
||||
|
||||
|
||||
def _utf8_size_over(text: str, limit: int) -> bool:
|
||||
@@ -43,79 +42,51 @@ def _utf8_size_over(text: str, limit: int) -> bool:
|
||||
|
||||
|
||||
def header_params(info) -> dict | None:
|
||||
"""The shared parser's result for the ``prompt``/``workflow`` text in ``info``.
|
||||
"""The shared parser's result for the inspector's view of ``info``'s records.
|
||||
|
||||
``info`` is a Pillow ``Image.info`` (or the inspection read's metadata).
|
||||
None when neither record is present, a record is not text or is over
|
||||
``MAX_RECORD_BYTES``, or the browser parser would throw on it.
|
||||
A record the projection does not parse is left out, as the inspector
|
||||
leaves it out. None when no record parses, a record is not text or is
|
||||
over ``MAX_RECORD_BYTES``, or the parser cannot use the graphs.
|
||||
"""
|
||||
if not isinstance(info, Mapping):
|
||||
return None
|
||||
records = {key: info.get(key) for key in _RECORD_KEYS}
|
||||
if all(text is None for text in records.values()):
|
||||
return None
|
||||
for text in records.values():
|
||||
graphs = {}
|
||||
for key in _RECORD_KEYS:
|
||||
text = info.get(key)
|
||||
if text is None:
|
||||
continue
|
||||
if not isinstance(text, str):
|
||||
if not isinstance(text, str) or _utf8_size_over(text, MAX_RECORD_BYTES):
|
||||
return None
|
||||
if _utf8_size_over(text, MAX_RECORD_BYTES):
|
||||
return None
|
||||
try:
|
||||
params = extract_from_records(records["prompt"], records["workflow"])
|
||||
except (GenerationParamsError, RecursionError):
|
||||
# RecursionError: the Python parser recurses on deeply nested arrays
|
||||
# where JavaScript does not throw; a crafted file gives no card facts.
|
||||
graph = project_graph(text, key)
|
||||
if graph["status"] == "parsed":
|
||||
graphs[key] = graph["value"]
|
||||
if not graphs:
|
||||
return None
|
||||
return _with_exact_seed(params, records)
|
||||
|
||||
|
||||
def _with_exact_seed(params: dict, records: dict) -> dict:
|
||||
"""``params`` with a rounded seed replaced by the integer the file records.
|
||||
|
||||
``JSON.parse`` rounds integers beyond 2**53, but the inspector reads the
|
||||
record's own digits (its projection carries big numbers as text). ComfyUI's
|
||||
randomized seeds are mostly that large, so the same parser is run once more
|
||||
on an exact parse of the same records, and its seed is kept only when it is
|
||||
the one that rounded to the parsed value. Anything else leaves the seed
|
||||
as parsed.
|
||||
"""
|
||||
seed = params.get("seed")
|
||||
if not isinstance(seed, float) or not math.isfinite(seed):
|
||||
return params
|
||||
if abs(seed) <= _MAX_SAFE_INTEGER or not seed.is_integer():
|
||||
return params
|
||||
try:
|
||||
exact = extract_generation_params(
|
||||
{
|
||||
key: parse_js_json(text, exact_integers=True)
|
||||
for key, text in records.items()
|
||||
if text is not None
|
||||
}
|
||||
)["seed"]
|
||||
return extract_generation_params(graphs)
|
||||
except (GenerationParamsError, RecursionError):
|
||||
return params
|
||||
if isinstance(exact, bool) or not isinstance(exact, int) or float(exact) != seed:
|
||||
return params
|
||||
return {**params, "seed": exact}
|
||||
# Defence in depth: a parser failure gives no card facts, never a 500.
|
||||
return None
|
||||
|
||||
|
||||
def card_seed(params: dict | None) -> int | None:
|
||||
"""The parsed seed as the integer the browser prints, else None.
|
||||
"""The parsed seed as an integer, else None.
|
||||
|
||||
A placeholder, text, boolean or non-integral number gives None. A double
|
||||
that ``header_params`` could not make exact gives the integer whose decimal
|
||||
form equals the browser's ``String(seed)``.
|
||||
The projection carries integers beyond 2**53 as their exact digits, so a
|
||||
digit string is the recorded seed (the inspector shows it too). A
|
||||
placeholder, other text, a boolean or a decimal gives None.
|
||||
"""
|
||||
if not isinstance(params, Mapping):
|
||||
return None
|
||||
value = params.get("seed")
|
||||
if isinstance(value, bool) or not isinstance(value, (int, float)):
|
||||
if isinstance(value, bool):
|
||||
return None
|
||||
if isinstance(value, float) and not math.isfinite(value):
|
||||
return None
|
||||
text = number_string(value)
|
||||
return int(text) if _INTEGER.match(text) else None
|
||||
if isinstance(value, int):
|
||||
return value
|
||||
if isinstance(value, str) and _INTEGER.match(value):
|
||||
return int(value)
|
||||
return None
|
||||
|
||||
|
||||
def parsed_positive(params: dict | None) -> str | None:
|
||||
|
||||
@@ -27,7 +27,6 @@ __all__ = [
|
||||
"GenerationParamsError",
|
||||
"extract_generation_params",
|
||||
"extract_from_records",
|
||||
"number_string",
|
||||
"parse_js_json",
|
||||
]
|
||||
|
||||
@@ -203,9 +202,6 @@ def _number_string(value):
|
||||
return sign + body
|
||||
|
||||
|
||||
number_string = _number_string
|
||||
|
||||
|
||||
def _js_string(value):
|
||||
"""``String(value)`` for any JSON-derived value."""
|
||||
if value is UNDEFINED:
|
||||
@@ -945,17 +941,14 @@ def _double_integer(text):
|
||||
return value if -_MAX_SAFE_INTEGER <= value <= _MAX_SAFE_INTEGER else float(text)
|
||||
|
||||
|
||||
def parse_js_json(text, *, exact_integers=False):
|
||||
"""``JSON.parse(text)``, or None where it throws (or the text is ``null``).
|
||||
|
||||
``exact_integers`` keeps integers beyond 2**53 exact instead of rounding
|
||||
them as JavaScript does; every other rule is unchanged.
|
||||
"""
|
||||
def parse_js_json(text):
|
||||
"""``JSON.parse(text)``, or None where it throws (or the text is ``null``)."""
|
||||
if not isinstance(text, str):
|
||||
return None
|
||||
integers = int if exact_integers else _double_integer
|
||||
try:
|
||||
return json.loads(text, parse_constant=_reject_constant, parse_int=integers)
|
||||
return json.loads(
|
||||
text, parse_constant=_reject_constant, parse_int=_double_integer
|
||||
)
|
||||
except (ValueError, RecursionError):
|
||||
return None
|
||||
|
||||
|
||||
@@ -161,6 +161,21 @@ def _graph(value: object, *, workflow: bool = False) -> dict:
|
||||
return {"status": "parsed", "value": value}
|
||||
|
||||
|
||||
def project_graph(text: str, kind: Literal["prompt", "workflow"]) -> dict:
|
||||
"""One record's graph as the inspector projects it: ``{status, value}``.
|
||||
|
||||
``status`` is ``parsed`` (``value`` is the projected graph the browser
|
||||
parser receives), ``limited`` (past the depth or node bound) or
|
||||
``malformed``. Shared with the workspace card so both read one bound.
|
||||
"""
|
||||
try:
|
||||
return _graph(_strict_projection(text, kind=kind), workflow=kind == "workflow")
|
||||
except _ProjectionLimit:
|
||||
return {"status": "limited", "value": None}
|
||||
except (ValueError, TypeError, KeyError, OverflowError):
|
||||
return {"status": "malformed", "value": None}
|
||||
|
||||
|
||||
def _metadata(inspection: "InspectionRead | None") -> dict:
|
||||
formats = (
|
||||
{}
|
||||
@@ -213,16 +228,15 @@ def _metadata(inspection: "InspectionRead | None") -> dict:
|
||||
graph["status"] = failed or "limited"
|
||||
elif formats.get(key) == "invalid":
|
||||
graph["status"] = "malformed"
|
||||
elif carrier is None and formats.get(key) == "reserialized-json":
|
||||
graph["status"] = "limited"
|
||||
elif carrier is None:
|
||||
graph = project_graph(raw.get(key), key)
|
||||
else:
|
||||
try:
|
||||
if carrier is not None:
|
||||
value = carrier.get(key)
|
||||
if isinstance(value, str):
|
||||
value = _strict_projection(value, kind=key)
|
||||
elif formats.get(key) == "reserialized-json":
|
||||
raise _ProjectionLimit()
|
||||
else:
|
||||
value = _strict_projection(raw[key], kind=key)
|
||||
value = carrier.get(key)
|
||||
if isinstance(value, str):
|
||||
value = _strict_projection(value, kind=key)
|
||||
graph = _graph(value, workflow=key == "workflow")
|
||||
except _ProjectionLimit:
|
||||
graph["status"] = "limited"
|
||||
|
||||
Reference in New Issue
Block a user