Files
ComfyAssets-ComfyUI_PromptM…/utils/execution_context_graph.py
T
Vito d9f168c317 feat(nodes): per-node Save to library switch (#90) (#427)
* feat(nodes): per-node Save to library switch (#90)

Every PromptManager node (Classic, Text, Focused, Split, Pair) gets an
optional save_to_library BOOLEAN, default on and declared last. Off, the
node still returns its normal outputs but PromptManager ignores the run:

- the node saves, counts, tracks and records nothing;
- the queue hook reads the switch from the queued graph (literal, or a
  linked PrimitiveBoolean), so a cached node is not counted either; a
  switch wired from an unreadable node is left to the node itself;
- an opted-out node is no run prompt, so no tracker entry, run key,
  embedded-metadata match or history attribution links its images, and
  a run with any opted-out positive node links none of its files;
- the scan reports such files as not_saved (never stored or queued for
  Needs review), and a file whose only opted-out node is its negative is
  stored positive-only, without the pair;
- execution history records no prompt text for an opted-out role.

PromptSearchList only reads the library and gets no switch.

The switch takes the widget index older workflows used for the editor's
own DOM slot (''), so a configure step turns any non-boolean value on:
workflows saved before the switch load with it on and no earlier widget
value moves. The Focused, Split and Pair editors draw it as a "Save to
library" checkbox in their toolbar; Classic and Text show the native
toggle.

* fix(nodes): Save to library review fixes (#90)

- A switch wired from a node the graph cannot read and off at run time
  marks the node's run key, so an earlier run's tracker entry never
  links this run's images.
- The scan reports such files as not_saved_wired ("only the run itself
  can link it") instead of not_saved.
- Workflow-only metadata ignores muted and bypassed PromptManager nodes
  and reads nodes inside subgraph definitions.
- The Focused, Split and Pair footer says when the switch comes from a
  link.

* fix(usage): count cached re-runs of runtime-linked prompt text (#347)

A PromptManager-family node whose text comes from a runtime link
(PreviewAny, an LLM, a list node) counted only the job that executed it;
later jobs served from ComfyUI's output cache counted nothing.

Each execution now records a receipt (digests of its STRING outputs and
hashes of the text it stores) per run key and queue item. On
execution_cached the receipt whose digests equal the cache entry's text is
replayed once for the job, with the node's role and what the queue hook
already saw recomputed from the job's own graph. The executing node and the
replay count through one shared own_counts. Unmatched cache entries count
nothing, as before; a node that executes after its replay in the same job
skips its own count. No schema change, no backfill.

* fix(usage): never replay a node twice in one job; dedupe receipts once complete (#347)

A repeated execution_cached id or message for the same job now counts the
node once. Receipts replace equal older ones only when their job finishes
(or the next job starts), so a list still recording [A] of [A, B] no longer
drops an older [A] receipt. Node-level tests check the receipt each
execution records.

* test(coverage): baseline for the Save to library module (#90)

* test(usage): disabled node then cached jobs counts 0 (#90, #347)

A PromptManager node with Save to library off records no cached-run
receipt and is no run prompt in the replayed job's graph, so ComfyUI
serving it from its cache on later jobs counts nothing. The prompt is
saved first by an enabled run, so a replayed count would show. Covers
PromptManager, Focused, Split, Text and the Pair.

* fix(nodes): hide the editor's native widgets through one helper (#90)

The Save to library switch copied the text fallbacks' zero-height computeSize
arrow, and neither copy was ever called, so focused-node.js function coverage
fell to 87.5 (floor 90). One hideNativeWidget helper now hides both, and the
spec checks that the hidden switch and a text fallback take no node height.
2026-10-28 18:09:16 +00:00

437 lines
16 KiB
Python

"""Bounded projection of declared sampler roles, independent of the host."""
import re
from dataclasses import dataclass
from .execution_contexts import PromptSnapshot, TextEvidence, text_size
from .prompt_graph import save_setting
from .prompt_pair_resolver import _DEFAULT_ADAPTERS
# A role whose PromptManager source has save_to_library off, or wired from a
# node the graph cannot read: no text is recorded for it (#90)
NOT_SAVED = "not_saved"
@dataclass(frozen=True)
class SourceRef:
node_id: str
node_type: str
output_slot: int
@dataclass(frozen=True)
class GraphRole:
sources: tuple[SourceRef, ...]
inferred: PromptSnapshot | None
reason: str
@dataclass(frozen=True)
class DeclaredRecipe:
anchor_id: str
positive: GraphRole
negative: GraphRole
@dataclass(frozen=True)
class GraphProjection:
recipes: tuple[DeclaredRecipe, ...]
sources: tuple[SourceRef, ...]
reason: str
source_snapshots: tuple[tuple[SourceRef, PromptSnapshot], ...] = ()
_SAMPLERS = {"KSampler", "KSamplerAdvanced", "SamplerCustom"}
_PM = {
"PromptManager",
"PromptManagerFocused",
"PromptManagerSplit",
"PromptManagerText",
"PromptManagerPair",
}
class _Unknown(ValueError):
pass
class _Limit(_Unknown):
pass
def _identifier(value: object) -> str:
if type(value) is int:
if abs(value) > 10**127:
raise _Unknown("invalid_node_identifier")
value = str(value)
try:
text_size(value, 512)
if len(value) > 128:
raise ValueError("oversized_node_identifier")
except ValueError:
raise _Unknown("invalid_node_identifier") from None
if not value:
raise _Unknown("invalid_node_identifier")
return value
def _link(value: object) -> tuple[str, int]:
if (
type(value) not in (tuple, list)
or len(value) != 2
or type(value[1]) is not int
or value[1] < 0
):
raise _Unknown("expected_typed_graph_link")
return _identifier(value[0]), value[1]
class _Projector:
def __init__(self, graph: dict) -> None:
self.graph = graph
self.visits = 0
self.retained = 0
self.transformed = False
self.selected_seen = set()
def node(self, node_id: str, depth: int) -> tuple[str, dict]:
self.visits += 1
if self.visits > 500 or depth > 64:
raise _Unknown("graph_resolution_limit_reached")
node = self.graph.get(node_id)
if type(node) is not dict or type(node.get("inputs")) is not dict:
raise _Unknown("missing_or_malformed_node")
if len(node["inputs"]) > 500:
raise _Unknown("node_input_limit_reached")
kind = node.get("class_type")
try:
text_size(kind, 128)
except ValueError:
raise _Unknown("invalid_node_type") from None
return kind, node["inputs"]
def text(
self, value: object, kind: str, depth: int = 1, active: frozenset = frozenset()
) -> tuple[PromptSnapshot, tuple]:
if type(value) is str and kind == "string":
text_size(value)
return self.snapshot(value, value, "", ""), ()
node_id, slot = _link(value)
key = (node_id, slot)
if key in active:
raise _Unknown("cyclic_prompt_graph")
name, inputs = self.node(node_id, depth)
active = active | {key}
if name == "Reroute" and slot == 0:
keys = [key for key in ("value", "input", "in") if key in inputs]
if len(keys) != 1:
raise _Unknown("ambiguous_reroute")
return self.text(inputs[keys[0]], kind, depth + 1, active)
if (
name in ("StringConcatenate", "Text Concatenate")
and slot == 0
and kind == "string"
):
keys = (
["string_a", "string_b"]
if name == "StringConcatenate"
else sorted(
key
for key in inputs
if type(key) is str and re.fullmatch(r"text_[a-z]", key)
)
)
delimiter = inputs.get(
"delimiter", "" if name == "StringConcatenate" else " "
)
text_size(delimiter)
clean_value = inputs.get("clean_whitespace", "true")
if name == "Text Concatenate" and type(clean_value) not in (str, bool):
raise _Unknown("invalid_whitespace_mode")
clean = name == "Text Concatenate" and (
clean_value is True
or type(clean_value) is str
and clean_value.lower() == "true"
)
if not keys:
raise _Unknown("missing_concatenation_inputs")
parts, sources, size = [], [], 0
for input_name in keys:
child, refs = self.text(
inputs.get(input_name, ""), "string", depth + 1, active
)
part = child.effective.value.strip() if clean else child.effective.value
if part or name == "StringConcatenate":
size += text_size(part) + (text_size(delimiter) if parts else 0)
if size > 65536:
raise _Unknown("text_field_limit_reached")
parts.append(part)
sources.extend(refs)
if len(sources) > 64:
raise _Unknown("source_reference_limit_reached")
text = delimiter.join(parts)
return self.snapshot(
text, text, "", "", composed=True, raw_composed=True
), tuple(sources)
adapter = _DEFAULT_ADAPTERS.get((name, slot))
if adapter is None or adapter.kind != kind:
raise _Unknown("unsupported_node_semantics_or_output_slot")
child, sources = self.text(
inputs.get(adapter.input_name), "string", depth + 1, active
)
if name in _PM:
sources = (SourceRef(node_id, name, slot),)
if adapter.compose:
before, before_sources = self.text(
inputs.get("prepend_text", ""), "string", depth + 1, active
)
after, after_sources = self.text(
inputs.get("append_text", ""), "string", depth + 1, active
)
raw = child.effective.value
prepend, append = before.effective.value, after.effective.value
parts = [part for part in (prepend.strip(), raw, append.strip()) if part]
if sum(text_size(part) for part in parts) + max(0, len(parts) - 1) > 65536:
raise _Unknown("text_field_limit_reached")
effective = " ".join(parts)
text_size(effective)
snapshot = self.retain(
PromptSnapshot(
child.effective,
TextEvidence(effective, "inferred", "graph_semantics", None),
before.effective,
after.effective,
)
)
if name not in _PM:
sources += before_sources + after_sources
elif name in _PM:
snapshot = self.retain(
PromptSnapshot(
child.effective,
child.effective,
TextEvidence("", "inferred", "graph_literal", None),
TextEvidence("", "inferred", "graph_literal", None),
)
)
else:
snapshot = child
if len(sources) > 64:
raise _Unknown("source_reference_limit_reached")
return snapshot, sources
def source_snapshot(self, source: SourceRef) -> PromptSnapshot:
name, inputs = self.node(source.node_id, 1)
adapter = _DEFAULT_ADAPTERS[(name, source.output_slot)]
unknown = TextEvidence(None, "unknown", "unknown", "missing")
def literal(value: object) -> TextEvidence:
try:
snapshot, sources = self.text(value, "string", 2)
# Stateful PM outputs cannot prove their injected result from inputs.
return unknown if sources else snapshot.effective
except _Limit:
raise
except ValueError:
return unknown
raw = literal(inputs.get(adapter.input_name))
before = literal(inputs.get("prepend_text", "") if adapter.compose else "")
after = literal(inputs.get("append_text", "") if adapter.compose else "")
return self.retain(PromptSnapshot(raw, unknown, before, after))
def discover_sources(
self, value: object, kind: str, depth: int = 1, active: frozenset = frozenset()
) -> tuple[SourceRef, ...]:
if type(value) is str and kind == "string":
return ()
node_id, slot = _link(value)
key = (node_id, slot)
if key in active:
raise _Unknown("cyclic_prompt_graph")
name, inputs = self.node(node_id, depth)
active = active | {key}
adapter = _DEFAULT_ADAPTERS.get((name, slot))
if adapter is not None and adapter.kind == kind:
if name in _PM:
return (SourceRef(node_id, name, slot),)
return self.discover_sources(
inputs.get(adapter.input_name), "string", depth + 1, active
)
if name == "Reroute" and slot == 0:
keys = [key for key in ("value", "input", "in") if key in inputs]
if len(keys) != 1:
raise _Unknown("ambiguous_reroute")
return self.discover_sources(inputs[keys[0]], kind, depth + 1, active)
if (
name in ("StringConcatenate", "Text Concatenate")
and slot == 0
and kind == "string"
):
keys = (
["string_a", "string_b"]
if name == "StringConcatenate"
else sorted(
key
for key in inputs
if type(key) is str and re.fullmatch(r"text_[a-z]", key)
)
)
sources = []
for key in keys:
sources.extend(
self.discover_sources(
inputs.get(key, ""), "string", depth + 1, active
)
)
if len(sources) > 64:
raise _Unknown("source_limit_reached")
self.transformed = self.transformed or bool(sources)
return tuple(sources)
return ()
def retain(self, snapshot: PromptSnapshot) -> PromptSnapshot:
size = sum(
(text_size(field.value) if field.value is not None else 0) + 64
for field in (
snapshot.raw,
snapshot.effective,
snapshot.prepend,
snapshot.append,
)
)
if self.retained + size > 1048576:
raise _Limit("projection_payload_limit_reached")
self.retained += size
return snapshot
def snapshot(
self,
raw: str,
effective: str,
prepend: str,
append: str,
composed: bool = False,
raw_composed: bool = False,
) -> PromptSnapshot:
return self.retain(
PromptSnapshot(
*(
TextEvidence(
value,
"inferred",
(
"graph_semantics"
if (composed and index == 1 or raw_composed and index == 0)
else "graph_literal"
),
None,
)
for index, value in enumerate((raw, effective, prepend, append))
)
)
)
def role(self, value: object) -> GraphRole:
sources = ()
try:
self.transformed = False
# Only the capture-source-to-consumer path determines transforms.
sources = self.discover_sources(value, "conditioning")
if any(
save_setting(self.graph, source.node_id) is not True
for source in sources
):
return GraphRole((), None, NOT_SAVED)
snapshot, _ = self.text(value, "conditioning")
return GraphRole(
sources, snapshot, "source-transformed" if self.transformed else ""
)
except _Limit:
raise
except ValueError as error:
reason = str(error) if isinstance(error, _Unknown) else "invalid_text_value"
return GraphRole(
sources,
None,
"source-transformed" if self.transformed and sources else reason,
)
def anchors(self, node_id: str, depth: int = 1) -> tuple[str, ...]:
if node_id in self.selected_seen:
return ()
self.selected_seen.add(node_id)
name, inputs = self.node(node_id, depth)
anchors = [node_id] if name in _SAMPLERS else []
for value in inputs.values():
if (
type(value) not in (tuple, list)
or len(value) != 2
or type(value[0]) not in (str, int)
or type(value[1]) is not int
or value[1] < 0
):
continue
parent, _ = _link(value)
anchors.extend(self.anchors(parent, depth + 1))
if len(anchors) > 16:
raise _Unknown("recipe_context_limit_reached")
return tuple(anchors)
def project_contexts(graph: object, outputs: object) -> GraphProjection:
"""Keep declared roles and graph inference, never evidence of consumption.
Sources are PM capture points. An inferred snapshot may compose text; its
presence never promotes a source observation to recorded effective text.
"""
try:
if type(graph) is not dict or len(graph) > 500:
raise _Unknown("invalid_or_oversized_graph")
normalized = {_identifier(key): value for key, value in graph.items()}
if len(normalized) != len(graph):
raise _Unknown("duplicate_graph_identifiers")
resolver = _Projector(normalized)
if outputs is None:
anchors = [
key
for key, value in normalized.items()
if type(value) is dict
and type(value.get("class_type")) is str
and value["class_type"] in _SAMPLERS
]
elif type(outputs) in (list, tuple) and len(outputs) <= 500:
anchors = []
for output in outputs:
anchors.extend(resolver.anchors(_identifier(output)))
anchors = list(dict.fromkeys(anchors))
else:
raise _Unknown("invalid_output_selection")
if len(anchors) > 16:
raise _Unknown("recipe_context_limit_reached")
recipes, sources = [], []
for anchor in anchors:
_, inputs = resolver.node(anchor, 1)
positive = resolver.role(inputs.get("positive"))
negative = resolver.role(inputs.get("negative"))
recipes.append(DeclaredRecipe(anchor, positive, negative))
sources.extend(positive.sources + negative.sources)
sources = tuple(dict.fromkeys(sources))
if len(sources) > 64:
raise _Unknown("source_reference_limit_reached")
reason = "" if recipes else "no_selected_sampler_anchors"
if any(
role.reason
for recipe in recipes
for role in (recipe.positive, recipe.negative)
):
reason = "some_roles_require_inferred_or_unresolved_text"
return GraphProjection(
tuple(recipes),
sources,
reason,
tuple((source, resolver.source_snapshot(source)) for source in sources),
)
except ValueError as error:
reason = str(error) if isinstance(error, _Unknown) else "invalid_text_value"
return GraphProjection((), (), reason)