Skip to content
7 changes: 5 additions & 2 deletions comfy_execution/asset_enrichment.py
Original file line number Diff line number Diff line change
Expand Up @@ -126,17 +126,20 @@ def register_cached_outputs(ui_wrapper: dict | None, job_id: str, asset_manager:
return enriched


def emit_cached_output(server: "ExecutionServer", node_id: str, display_node_id: str, cached: "CacheEntry", prompt_id: str, ui_outputs: dict, asset_manager: "AssetManager") -> None:
def emit_cached_output(server: "ExecutionServer", node_id: str, meta: dict, cached: "CacheEntry", prompt_id: str, ui_outputs: dict, asset_manager: "AssetManager") -> None:
if node_id in ui_outputs:
return
enriched = register_cached_outputs(cached.ui, prompt_id, asset_manager)
if enriched is not None:
# The cache entry may have been written by another node with the same
# inputs, so record this node's meta rather than the writer's.
enriched["meta"] = meta
ui_outputs[node_id] = enriched
if server.client_id is None:
return
output = enriched.get("output") if enriched is not None else None
server.send_sync(
"executed",
{"node": node_id, "display_node": display_node_id, "output": output, "prompt_id": prompt_id},
{"node": node_id, "display_node": meta["display_node"], "output": output, "prompt_id": prompt_id},
server.client_id,
)
1 change: 1 addition & 0 deletions comfy_execution/jobs.py
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,7 @@ def normalize_history_item(prompt_id: str, history_item: dict, include_outputs:
if include_outputs:
job['outputs'] = normalize_outputs(outputs)
job['execution_status'] = status_info
job['execution_meta'] = history_item.get('meta', {})
job['workflow'] = {
'prompt': prompt,
'extra_data': extra_data,
Expand Down
22 changes: 12 additions & 10 deletions execution.py
Original file line number Diff line number Diff line change
Expand Up @@ -436,17 +436,25 @@ def _is_intermediate_output(dynprompt, node_id):
return getattr(class_def, 'HAS_INTERMEDIATE_OUTPUT', False)


def _node_meta(dynprompt, unique_id) -> dict:
return {
"node_id": unique_id,
"display_node": dynprompt.get_display_node_id(unique_id),
"parent_node": dynprompt.get_parent_node_id(unique_id),
"real_node_id": dynprompt.get_real_node_id(unique_id),
}


async def execute(server: "ExecutionServer", dynprompt, caches, current_item, extra_data, executed, prompt_id, execution_list, pending_subgraph_results, pending_async_nodes, ui_outputs, asset_manager: AssetManager):
unique_id = current_item
real_node_id = dynprompt.get_real_node_id(unique_id)
display_node_id = dynprompt.get_display_node_id(unique_id)
parent_node_id = dynprompt.get_parent_node_id(unique_id)
inputs = dynprompt.get_node(unique_id)['inputs']
class_type = dynprompt.get_node(unique_id)['class_type']
class_def = nodes.NODE_CLASS_MAPPINGS[class_type]
cached = await caches.outputs.get(unique_id)
if cached is not None:
emit_cached_output(server, unique_id, display_node_id, cached, prompt_id, ui_outputs, asset_manager)
emit_cached_output(server, unique_id, _node_meta(dynprompt, unique_id), cached, prompt_id, ui_outputs, asset_manager)
get_progress_state().finish_progress(unique_id)
execution_list.cache_update(unique_id, cached)
return (ExecutionResult.SUCCESS, None, None)
Expand Down Expand Up @@ -567,12 +575,7 @@ async def await_completion():
if len(output_ui) > 0:
asset_previews = take_asset_previews(output_ui)
output_ui = enrich_output_with_media_metadata(output_ui)
meta = {
"node_id": unique_id,
"display_node": display_node_id,
"parent_node": parent_node_id,
"real_node_id": real_node_id,
}
meta = _node_meta(dynprompt, unique_id)
enriched_output_ui = register_executed_outputs(output_ui, prompt_id, asset_manager, asset_previews)
ui_outputs[unique_id] = {"meta": meta, "output": enriched_output_ui}
cache_ui_value = {"meta": meta, "output": output_ui}
Expand Down Expand Up @@ -823,8 +826,7 @@ async def execute_async(self, prompt, prompt_id, extra_data={}, execute_outputs=
continue
cached = await self.caches.outputs.get(node_id)
if cached is not None:
display_node_id = dynamic_prompt.get_display_node_id(node_id)
emit_cached_output(self.server, node_id, display_node_id, cached, prompt_id, ui_node_outputs, self.asset_manager)
emit_cached_output(self.server, node_id, _node_meta(dynamic_prompt, node_id), cached, prompt_id, ui_node_outputs, self.asset_manager)
self.add_message("execution_success", { "prompt_id": prompt_id }, broadcast=False)

ui_outputs = {}
Expand Down
29 changes: 24 additions & 5 deletions tests-unit/execution_test/test_enrich_output.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,10 @@ def _output(filename: str, *, subfolder: str = "", type_: str = "output") -> dic
return {"images": [{"filename": filename, "subfolder": subfolder, "type": type_}]}


def _meta(node_id: str) -> dict:
return {"node_id": node_id, "display_node": node_id, "parent_node": None, "real_node_id": node_id}


def _wrapper(filename: str, node_id: str = "1") -> dict:
return {
"meta": {
Expand Down Expand Up @@ -220,7 +224,7 @@ def test_cached_registration_happens_without_client(output_path_environment) ->
emit_cached_output(
server,
"node-1",
"node-1",
_meta("node-1"),
_CacheEntry(ui=_wrapper("noclient.png"), outputs=[]),
"job-x",
ui_outputs,
Expand Down Expand Up @@ -254,7 +258,7 @@ def test_cached_ui_object_unmodified_after_emission(output_path_environment) ->
snapshot = copy.deepcopy(cached.ui)

register_executed_outputs(_output("immut.png"), "seed-job", manager)
emit_cached_output(server, "1", "1", cached, "prompt-1", {}, manager)
emit_cached_output(server, "1", _meta("1"), cached, "prompt-1", {}, manager)

assert cached.ui == snapshot

Expand All @@ -266,8 +270,8 @@ def test_double_emission_yields_single_delivery(output_path_environment) -> None
cached = _CacheEntry(ui=_wrapper("dbl.png"), outputs=[])

register_executed_outputs(_output("dbl.png"), "seed-job", manager)
emit_cached_output(server, "1", "1", cached, "prompt-1", ui_outputs, manager)
emit_cached_output(server, "1", "1", cached, "prompt-1", ui_outputs, manager)
emit_cached_output(server, "1", _meta("1"), cached, "prompt-1", ui_outputs, manager)
emit_cached_output(server, "1", _meta("1"), cached, "prompt-1", ui_outputs, manager)

deliveries = manager.deliveries_by_path[os.path.join(_BASE, "dbl.png")]
assert len([delivery for delivery in deliveries if delivery.asset.job_id == "prompt-1"]) == 1
Expand Down Expand Up @@ -372,7 +376,7 @@ def test_emit_cached_sends_enriched_output_to_client(output_path_environment) ->
emit_cached_output(
server,
"1",
"1",
_meta("1"),
_CacheEntry(ui=_wrapper("send.png"), outputs=[]),
"prompt-1",
ui_outputs,
Expand All @@ -384,3 +388,18 @@ def test_emit_cached_sends_enriched_output_to_client(output_path_environment) ->
assert event == "executed"
assert client_id == "client-1"
assert payload["output"]["images"][0]["id"] is not None


def test_cached_output_records_the_current_nodes_meta(output_path_environment) -> None:
"""A cache entry shared by two nodes must not carry the first node's meta into the second's history."""
manager = InMemoryAssets()
server = _Server(client_id="client-1")
ui_outputs: dict = {}
cached = _CacheEntry(ui=_wrapper("shared.png", node_id="4"), outputs=[])
current_meta = _meta("7")

register_executed_outputs(_output("shared.png"), "seed-job", manager)
emit_cached_output(server, "7", current_meta, cached, "prompt-1", ui_outputs, manager)

assert ui_outputs["7"]["meta"] == current_meta
assert cached.ui["meta"]["node_id"] == "4"
32 changes: 32 additions & 0 deletions tests/execution/test_execution.py
Original file line number Diff line number Diff line change
Expand Up @@ -1211,6 +1211,38 @@ def test_cached_outputs_in_job_without_client_id(self, client: ComfyClient, buil
assert job['status'] == 'completed'
assert output.id in job['outputs'], "Cached outputs must appear in job outputs without a client_id"

def test_cached_output_meta_names_its_own_node(self, client: ComfyClient, builder: GraphBuilder):
"""Two identical previews share a cache entry; each must keep its own meta in the job detail."""
g = builder
image = g.node("StubImage", content="BLACK", height=32, width=32, batch_size=1)
first = g.node("PreviewImage", images=image.out(0))
second = g.node("PreviewImage", images=image.out(0))

result = client.run(g)
job = client.get_job(result.get_prompt_id())

meta = job['execution_meta']
for node in (first, second):
assert meta[node.id]['node_id'] == node.id
assert meta[node.id]['display_node'] == node.id

Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved
def test_expanded_output_meta_names_its_display_node(self, client: ComfyClient, builder: GraphBuilder):
"""An output from an expanded node is shown on the node that expanded it."""
g = builder
expander = g.node("TestExecutedNodeIdsExpander", value="expanded-child")

result = client.run(g)
job = client.get_job(result.get_prompt_id())

expanded = {k: v for k, v in job['execution_meta'].items() if k != expander.id}
assert len(expanded) == 1
node_id, meta = next(iter(expanded.items()))
assert meta['node_id'] == node_id
assert meta['display_node'] == expander.id
assert meta['parent_node'] == expander.id
assert meta['real_node_id'] == expander.id
assert node_id in job['outputs']

def _create_history_item(self, client, builder):
g = GraphBuilder(prefix="offset_test")
input_node = g.node(
Expand Down
31 changes: 31 additions & 0 deletions tests/execution/test_jobs.py
Original file line number Diff line number Diff line change
Expand Up @@ -683,6 +683,37 @@ def test_include_outputs(self):
'extra_data': {'create_time': 1234567890, 'client_id': 'abc'},
}

def test_include_outputs_returns_execution_meta(self):
"""Detail view should expose each node's display node as execution_meta."""
meta = {
'15.0.0.3': {'node_id': '15.0.0.3', 'display_node': '15', 'parent_node': '15', 'real_node_id': '15'},
'9': {'node_id': '9', 'display_node': '9', 'parent_node': None, 'real_node_id': '9'},
}
history_item = {
'prompt': (5, 'prompt-meta', {}, {'create_time': 1234567890}, ['9']),
'status': {'status_str': 'success', 'completed': True, 'messages': []},
'outputs': {'9': {'images': [{'filename': 'a.png'}]}},
'meta': meta,
}

detail = normalize_history_item('prompt-meta', history_item, include_outputs=True)
summary = normalize_history_item('prompt-meta', history_item)

assert detail['execution_meta'] == meta
assert 'execution_meta' not in summary

def test_include_outputs_without_meta(self):
"""History written before meta existed should still give an object."""
history_item = {
'prompt': (5, 'prompt-old', {}, {'create_time': 1234567890}, []),
'status': {'status_str': 'success', 'completed': True, 'messages': []},
'outputs': {},
}

job = normalize_history_item('prompt-old', history_item, include_outputs=True)

assert job['execution_meta'] == {}

def test_include_outputs_normalizes_3d_strings(self):
"""Detail view should transform string 3D filenames into file output dicts."""
history_item = {
Expand Down
Loading