Image Pipeline Playground
feat(backend): enhance logging capabilities in script execution
Mohammad Rajaeia1727914f0db
diff --git a/backend/app/engine/executor.py b/backend/app/engine/executor.pyindex 3cf8db3..13f0c1e 100644--- a/backend/app/engine/executor.py+++ b/backend/app/engine/executor.py@@ -15,7 +15,7 @@ import numpy as npfrom app.engine.cache import CacheManager, is_image_valuefrom app.engine.registry import NodeRegistry, registry-from app.engine.run_context import current_sample_index+from app.engine.run_context import current_log_emit, current_node_id, current_sample_indexfrom app.engine.save_bundle import (FolderSaveTracker,SaveBundle,@@ -432,9 +432,27 @@ class DagExecutor:cache_hit = Trueif not cache_hit:- produced = node_impl.execute(- inputs, params, seed=iteration_seed- )+ def _script_log_emit(message: str) -> None:+ emit(+ ExecutionEvent(+ type=ExecutionEventType.LOG,+ node_id=node_id,+ message=message,+ sample_index=flat_index,+ iteration=iteration,+ batch_index=batch_index,+ )+ )++ node_token = current_node_id.set(node_id)+ log_token = current_log_emit.set(_script_log_emit)+ try:+ produced = node_impl.execute(+ inputs, params, seed=iteration_seed+ )+ finally:+ current_log_emit.reset(log_token)+ current_node_id.reset(node_token)outputs[node_id] = producedif use_cache:self.cache.put_outputs(diff --git a/backend/app/engine/run_context.py b/backend/app/engine/run_context.pyindex 25b8c5d..9e5d93f 100644--- a/backend/app/engine/run_context.py+++ b/backend/app/engine/run_context.py@@ -2,6 +2,7 @@from __future__ import annotations+from collections.abc import Callablefrom contextvars import ContextVarcurrent_sample_index: ContextVar[int] = ContextVar("current_sample_index", default=0)@@ -9,6 +10,12 @@ current_source_stems: ContextVar[list[str]] = ContextVar("current_source_stems",default=[],)+current_node_id: ContextVar[str | None] = ContextVar("current_node_id", default=None)+# Optional sink for script log() lines (set by DagExecutor during node execute).+current_log_emit: ContextVar[Callable[[str], None] | None] = ContextVar(+ "current_log_emit",+ default=None,+)def source_stem_for_sample(sample_index: int) -> str:diff --git a/backend/app/nodes/custom.py b/backend/app/nodes/custom.pyindex 3d1a682..7f9b9cb 100644--- a/backend/app/nodes/custom.py+++ b/backend/app/nodes/custom.py@@ -15,6 +15,7 @@ DEFAULT_CUSTOM_CODE = """\def process(image, seed=0):# image: BGR uint8 numpy array (H, W, C)# return: ndarray (same dtype preferred)+ # Helpers: log(*args) — writes to Script log in the inspectorreturn image"""diff --git a/backend/app/nodes/script_runtime.py b/backend/app/nodes/script_runtime.pyindex c37f8ce..a2942a7 100644--- a/backend/app/nodes/script_runtime.py+++ b/backend/app/nodes/script_runtime.py@@ -7,6 +7,38 @@ from typing import Anyimport cv2import numpy as np+from app.engine.run_context import current_log_emit++_MAX_LOG_ARG_CHARS = 500+++def format_log_value(value: Any) -> str:+ """Pretty-print a value for script log() (ndarrays show shape/dtype)."""+ if isinstance(value, np.ndarray):+ return f"ndarray(shape={value.shape}, dtype={value.dtype})"+ if isinstance(value, str):+ text = value+ else:+ try:+ text = repr(value)+ except Exception: # noqa: BLE001+ text = str(value)+ if len(text) > _MAX_LOG_ARG_CHARS:+ return f"{text[: _MAX_LOG_ARG_CHARS - 1]}…"+ return text+++def format_log_message(*args: Any) -> str:+ return " ".join(format_log_value(arg) for arg in args)+++def script_log(*args: Any) -> None:+ """Global helper injected into user scripts: log(*args) → Script log panel."""+ message = format_log_message(*args)+ emit = current_log_emit.get()+ if emit is not None:+ emit(message)+def run_process_code(code: str,@@ -23,6 +55,7 @@ def run_process_code("cv2": cv2,"np": np,"numpy": np,+ "log": script_log,}try:compiled = compile(code, source_name, "exec")diff --git a/backend/tests/test_script_log_executor.py b/backend/tests/test_script_log_executor.pynew file mode 100644index 0000000..0062b37--- /dev/null+++ b/backend/tests/test_script_log_executor.py@@ -0,0 +1,48 @@+"""Executor emits LOG events from script log() helpers."""++from __future__ import annotations++from app.engine.executor import DagExecutor+from app.models.graph import Edge, ExecuteRequest, ExecutionEventType, Graph, NodeInstance+from app.nodes import register_builtin_nodes+++def test_executor_emits_script_log_events(tmp_path) -> None:+ register_builtin_nodes()+ events = []+ request = ExecuteRequest(+ graph=Graph(+ nodes=[+ NodeInstance(id="src", type="blank_image", params={"width": 8, "height": 8}),+ NodeInstance(+ id="custom",+ type="custom_python",+ params={+ "code": (+ "def process(image, seed=0):\n"+ " log('hello', image)\n"+ " return image\n"+ ),+ },+ ),+ ],+ edges=[+ Edge(+ id="e1",+ source="src",+ source_port="image",+ target="custom",+ target_port="image",+ ),+ ],+ ),+ allow_custom_code=True,+ cache=False,+ )+ DagExecutor(tmp_path / "cache").execute(request, on_event=events.append)+ log_events = [e for e in events if e.type == ExecutionEventType.LOG]+ assert len(log_events) == 1+ assert log_events[0].node_id == "custom"+ assert log_events[0].message is not None+ assert "hello" in log_events[0].message+ assert "ndarray(shape=" in log_events[0].messagediff --git a/backend/tests/test_script_runtime_log.py b/backend/tests/test_script_runtime_log.pynew file mode 100644index 0000000..f9f01a3--- /dev/null+++ b/backend/tests/test_script_runtime_log.py@@ -0,0 +1,69 @@+"""Tests for script_runtime log() helper."""++from __future__ import annotations++import numpy as np+import pytest++from app.engine.run_context import current_log_emit+from app.nodes.script_runtime import format_log_message, format_log_value, run_process_code+++def test_format_log_value_ndarray() -> None:+ arr = np.zeros((4, 5, 3), dtype=np.uint8)+ assert format_log_value(arr) == "ndarray(shape=(4, 5, 3), dtype=uint8)"+++def test_format_log_message_truncates_long_strings() -> None:+ message = format_log_message("x", "a" * 600)+ assert message.startswith("x ")+ assert message.endswith("…")+ assert len(message) < 520+++def test_log_noop_without_emit() -> None:+ image = np.zeros((2, 2, 3), dtype=np.uint8)+ code = """+def process(image, seed=0):+ log("hello", image)+ return image+"""+ result = run_process_code(code, image, seed=0)+ np.testing.assert_array_equal(result, image)+++def test_log_calls_emit_with_formatted_message() -> None:+ image = np.ones((3, 3, 3), dtype=np.uint8)+ messages: list[str] = []+ token = current_log_emit.set(messages.append)+ try:+ code = """+def process(image, seed=0):+ log("start", image, 42)+ return image+"""+ run_process_code(code, image, seed=0)+ finally:+ current_log_emit.reset(token)++ assert messages == [+ "start ndarray(shape=(3, 3, 3), dtype=uint8) 42",+ ]+++def test_log_during_process_failure_still_emitted() -> None:+ image = np.zeros((2, 2, 3), dtype=np.uint8)+ messages: list[str] = []+ token = current_log_emit.set(messages.append)+ try:+ code = """+def process(image, seed=0):+ log("before boom")+ raise RuntimeError("boom")+"""+ with pytest.raises(ValueError, match="boom"):+ run_process_code(code, image, seed=0)+ finally:+ current_log_emit.reset(token)++ assert messages == ["before boom"]