jpipe-runner 3.4.0__tar.gz → 3.4.1__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (34) hide show
  1. {jpipe_runner-3.4.0 → jpipe_runner-3.4.1}/PKG-INFO +1 -1
  2. {jpipe_runner-3.4.0 → jpipe_runner-3.4.1}/pyproject.toml +1 -1
  3. {jpipe_runner-3.4.0 → jpipe_runner-3.4.1}/src/jpipe_runner/framework/decorators/jpipe_decorator.py +36 -5
  4. {jpipe_runner-3.4.0 → jpipe_runner-3.4.1}/src/jpipe_runner/framework/engine.py +15 -92
  5. jpipe_runner-3.4.1/src/jpipe_runner/framework/graphviz_exporter.py +106 -0
  6. jpipe_runner-3.4.1/src/jpipe_runner/framework/validators/__init__.py +21 -0
  7. jpipe_runner-3.4.1/src/jpipe_runner/framework/validators/base.py +41 -0
  8. jpipe_runner-3.4.1/src/jpipe_runner/framework/validators/duplicate_producer.py +47 -0
  9. jpipe_runner-3.4.1/src/jpipe_runner/framework/validators/evidence_dependency.py +186 -0
  10. jpipe_runner-3.4.1/src/jpipe_runner/framework/validators/missing_variable.py +54 -0
  11. jpipe_runner-3.4.1/src/jpipe_runner/framework/validators/order.py +77 -0
  12. jpipe_runner-3.4.1/src/jpipe_runner/framework/validators/produced_not_consumed.py +46 -0
  13. jpipe_runner-3.4.1/src/jpipe_runner/framework/validators/schema.py +92 -0
  14. jpipe_runner-3.4.1/src/jpipe_runner/framework/validators/self_dependency.py +47 -0
  15. jpipe_runner-3.4.1/src/jpipe_runner/framework/validators/unbound_element.py +52 -0
  16. {jpipe_runner-3.4.0 → jpipe_runner-3.4.1}/src/jpipe_runner/runner.py +7 -2
  17. {jpipe_runner-3.4.0 → jpipe_runner-3.4.1}/src/jpipe_runner/runtime.py +6 -1
  18. jpipe_runner-3.4.0/src/jpipe_runner/framework/validators.py +0 -591
  19. {jpipe_runner-3.4.0 → jpipe_runner-3.4.1}/LICENSE +0 -0
  20. {jpipe_runner-3.4.0 → jpipe_runner-3.4.1}/README.md +0 -0
  21. {jpipe_runner-3.4.0 → jpipe_runner-3.4.1}/src/jpipe_runner/__init__.py +0 -0
  22. {jpipe_runner-3.4.0 → jpipe_runner-3.4.1}/src/jpipe_runner/__main__.py +0 -0
  23. {jpipe_runner-3.4.0 → jpipe_runner-3.4.1}/src/jpipe_runner/enums.py +0 -0
  24. {jpipe_runner-3.4.0 → jpipe_runner-3.4.1}/src/jpipe_runner/exceptions.py +0 -0
  25. {jpipe_runner-3.4.0 → jpipe_runner-3.4.1}/src/jpipe_runner/framework/__init__.py +0 -0
  26. {jpipe_runner-3.4.0 → jpipe_runner-3.4.1}/src/jpipe_runner/framework/context.py +0 -0
  27. {jpipe_runner-3.4.0 → jpipe_runner-3.4.1}/src/jpipe_runner/framework/decorators/__init__.py +0 -0
  28. {jpipe_runner-3.4.0 → jpipe_runner-3.4.1}/src/jpipe_runner/framework/decorators/contribution_decorator.py +0 -0
  29. {jpipe_runner-3.4.0 → jpipe_runner-3.4.1}/src/jpipe_runner/framework/decorators/link_decorator.py +0 -0
  30. {jpipe_runner-3.4.0 → jpipe_runner-3.4.1}/src/jpipe_runner/framework/decorators/skip_decorator.py +0 -0
  31. {jpipe_runner-3.4.0 → jpipe_runner-3.4.1}/src/jpipe_runner/framework/logger.py +0 -0
  32. {jpipe_runner-3.4.0 → jpipe_runner-3.4.1}/src/jpipe_runner/schema/__init__.py +0 -0
  33. {jpipe_runner-3.4.0 → jpipe_runner-3.4.1}/src/jpipe_runner/schema/justification.schema.json +0 -0
  34. {jpipe_runner-3.4.0 → jpipe_runner-3.4.1}/src/jpipe_runner/utils.py +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: jpipe-runner
3
- Version: 3.4.0
3
+ Version: 3.4.1
4
4
  Summary: A Justification Runner designed for jPipe.
5
5
  License: MIT
6
6
  License-File: LICENSE
@@ -1,6 +1,6 @@
1
1
  [tool.poetry]
2
2
  name = "jpipe-runner"
3
- version = "3.4.0"
3
+ version = "3.4.1"
4
4
  description = "A Justification Runner designed for jPipe."
5
5
  authors = [
6
6
  "Jason Lyu <xjasonlyu@gmail.com>",
@@ -21,8 +21,11 @@ def jpipe(consume: Optional[List[str]] = None, produce: Optional[List[str]] = No
21
21
  produce = produce or []
22
22
 
23
23
  def decorator(func: Callable) -> Callable:
24
+ _check_produce_param(func, produce)
24
25
  consume_checker = _init_checker(ConsumedVariableChecker, func, consume)
25
26
  produce_checker = _init_checker(ProducedVariableChecker, func, produce)
27
+ params = list(inspect.signature(func).parameters)
28
+ _has_produce_slot = bool(params) and params[-1] == "produce"
26
29
 
27
30
  @wraps(func)
28
31
  def wrapper(*args: Any, **kwargs: Any) -> Any:
@@ -30,6 +33,8 @@ def jpipe(consume: Optional[List[str]] = None, produce: Optional[List[str]] = No
30
33
  kwargs = consume_checker.inject_arguments(kwargs)
31
34
  if produce_checker:
32
35
  kwargs["produce"] = produce_checker.produce
36
+ elif _has_produce_slot:
37
+ kwargs["produce"] = _noop_produce
33
38
 
34
39
  result = func(*args, **kwargs)
35
40
 
@@ -43,6 +48,26 @@ def jpipe(consume: Optional[List[str]] = None, produce: Optional[List[str]] = No
43
48
  return decorator
44
49
 
45
50
 
51
+ def _check_produce_param(func: Callable, produce: List[str]) -> None:
52
+ if not produce:
53
+ return
54
+ params = list(inspect.signature(func).parameters)
55
+ if not params or params[-1] != "produce":
56
+ raise ValueError(
57
+ f"[jpipe] Function '{func.__name__}' declares produce={produce!r} "
58
+ f"but its last parameter is not 'produce'.\n"
59
+ f" • Actual signature: {func.__name__}({', '.join(params)})\n"
60
+ f" • Fix: add 'produce' as the final parameter, "
61
+ f"typed as Callable[[str, Any], None]."
62
+ )
63
+
64
+
65
+ def _noop_produce(name: str, value: Any) -> None:
66
+ # Intentional no-op: allows functions to keep a produce param for consistency
67
+ # when produce=[] — calls are accepted and silently discarded.
68
+ pass
69
+
70
+
46
71
  def _init_checker(
47
72
  CheckerClass: type, func: Callable, params: List[str]
48
73
  ) -> "ConsumedVariableChecker | ProducedVariableChecker | None":
@@ -93,6 +118,15 @@ class ConsumedVariableChecker:
93
118
  ctx._set(self.func_name, param, None, RuntimeContext.CONSUME)
94
119
  GLOBAL_LOGGER.debug(f"[{self.func_name}] Variable '{param}' registered as CONSUME")
95
120
 
121
+ unused = set(self.declared_params) - self.used_params
122
+ if unused:
123
+ raise ValueError(
124
+ f"[jpipe] Function '{self.func_name}' declares consumed variable(s) "
125
+ f"{sorted(unused)!r} that are never referenced in the function body.\n"
126
+ f" • Fix: either reference each variable in the function body, "
127
+ f"or remove it from consume=[]."
128
+ )
129
+
96
130
  def inject_arguments(self, kwargs: dict) -> dict:
97
131
  """
98
132
  Injects variable values from the context into the function’s keyword arguments.
@@ -102,10 +136,6 @@ class ConsumedVariableChecker:
102
136
  :raises ValueError: If a declared variable is missing from the context.
103
137
  """
104
138
  for param in self.declared_params:
105
- if param not in self.used_params:
106
- GLOBAL_LOGGER.warning(
107
- f"Consumed variable '{param}' is declared but not used in function '{self.func_name}'."
108
- )
109
139
  value = ctx.get(param)
110
140
  GLOBAL_LOGGER.debug(
111
141
  f"[{self.func_name}] Injecting consumed variable '{param}' = {value}"
@@ -136,7 +166,8 @@ class ConsumedVariableChecker:
136
166
  self.used_vars: set[str] = set()
137
167
 
138
168
  def visit_Name(self, node: ast.Name) -> None:
139
- self.used_vars.add(node.id)
169
+ if isinstance(node.ctx, ast.Load):
170
+ self.used_vars.add(node.id)
140
171
 
141
172
  visitor = VarVisitor()
142
173
  visitor.visit(tree)
@@ -1,6 +1,4 @@
1
1
  import json
2
- import logging
3
- from pathlib import Path
4
2
  from typing import Any, Iterable, Iterator, Optional, Tuple
5
3
 
6
4
  import networkx as nx
@@ -11,6 +9,7 @@ from ..exceptions import FunctionException
11
9
  from ..runtime import PythonRuntime
12
10
  from ..utils import normalize_structure, parse_value, sanitize_string
13
11
  from .context import RuntimeContext, ctx
12
+ from .graphviz_exporter import GraphvizExporter
14
13
  from .logger import GLOBAL_LOGGER
15
14
  from .validators import (
16
15
  DuplicateProducerValidator,
@@ -20,6 +19,7 @@ from .validators import (
20
19
  OrderValidator,
21
20
  ProducedButNotConsumedValidator,
22
21
  SelfDependencyValidator,
22
+ UnboundElementValidator,
23
23
  )
24
24
 
25
25
 
@@ -58,6 +58,7 @@ class PipelineEngine:
58
58
  """
59
59
  GLOBAL_LOGGER.info("Initializing PipelineEngine...")
60
60
  self.justification_name = "Unknown Justification"
61
+ self._link_registry: dict[str, str] = {}
61
62
  self.load_config(config_path, variables)
62
63
  if justification_path:
63
64
  self.graph = self.parse_justification(justification_path)
@@ -270,6 +271,7 @@ class PipelineEngine:
270
271
  ProducedButNotConsumedValidator(self, ctx),
271
272
  DuplicateProducerValidator(self, ctx),
272
273
  EvidenceDependencyValidator(self, ctx, self.graph),
274
+ UnboundElementValidator(self, ctx, self._link_registry),
273
275
  ]
274
276
 
275
277
  all_passed = True
@@ -389,6 +391,7 @@ class PipelineEngine:
389
391
  :param registry: Mapping of link_id → attr_name produced by runtime.build_link_registry().
390
392
  :type registry: dict[str, str]
391
393
  """
394
+ self._link_registry = registry
392
395
  for link_id, attr_name in registry.items():
393
396
  node_id = self._resolve_node_id(link_id)
394
397
  if node_id is not None:
@@ -489,12 +492,17 @@ class PipelineEngine:
489
492
  status = StatusType.SKIP
490
493
 
491
494
  # --- Attempt function execution (or dry-run) ---
492
- elif node_type in {"evidence", "strategy"}:
495
+ elif node_type in {"evidence", "strategy"} or (
496
+ node_type == "sub-conclusion" and (
497
+ node in self._link_registry
498
+ or f"{self.justification_name}:{node}" in self._link_registry
499
+ )
500
+ ):
493
501
  status, exception = self._execute_justification_fn(
494
502
  label, fn_name, runtime, dry_run, node
495
503
  )
496
504
 
497
- # --- Default handling for conclusion nodes ---
505
+ # --- Default handling for conclusion and unbound sub-conclusion nodes ---
498
506
  else:
499
507
  status = StatusType.PASS
500
508
 
@@ -636,96 +644,11 @@ class PipelineEngine:
636
644
  self, status_dict: dict[str, str], output_path: str, filename: str, format: str
637
645
  ) -> None:
638
646
  """
639
- Export the justification graph to any image format (png, svg, pdf etc),
640
- styling nodes by VariableType and edges by status.
647
+ Export the justification graph to an image file.
641
648
 
642
649
  :param status_dict: Mapping node id -> status ("PASS", "FAIL", "SKIP")
643
650
  :param output_path: Directory path to save the exported graph image.
644
651
  :param filename: Output filename (without extension).
645
- :param format: Image format string (e.g. "png", "svg", "pdf").
652
+ :param format: Image format string (e.g. "png", "svg", "dot").
646
653
  """
647
- import graphviz as gv
648
-
649
- resolved_path = self._resolve_output_path(output_path, filename)
650
-
651
- G = self.graph.copy()
652
- dot = gv.Digraph()
653
- dot.attr(rankdir="BT")
654
-
655
- self._style_nodes(dot, G, status_dict)
656
- self._style_edges(dot, G, status_dict)
657
-
658
- dot.render(
659
- str(resolved_path),
660
- format=format,
661
- engine="dot",
662
- cleanup=True,
663
- outfile=str(resolved_path.with_suffix(f".{format}")),
664
- )
665
-
666
- @staticmethod
667
- def _resolve_output_path(output_path: str, filename: str) -> Path:
668
- """
669
- Resolve and prepare the output file path, creating the directory if needed.
670
-
671
- :param output_path: Directory path for the output file.
672
- :param filename: Output filename (without extension).
673
- :return: Full Path object pointing to the output file location.
674
- """
675
- path = Path(output_path)
676
- if not path.exists():
677
- path.mkdir(parents=True, exist_ok=True)
678
- return path / filename
679
-
680
- @staticmethod
681
- def _style_nodes(dot: Any, G: nx.DiGraph, status_dict: dict[str, str]) -> None:
682
- """
683
- Apply visual styles to graph nodes based on their type and execution status.
684
-
685
- :param dot: graphviz.Digraph instance to add nodes to.
686
- :param G: NetworkX DiGraph with node attribute data.
687
- :param status_dict: Mapping of node id -> status string ("PASS", "FAIL", "SKIP").
688
- """
689
- node_attr_map = {
690
- "conclusion": {"shape": "rect", "style": "filled,rounded", "fillcolor": "lightgrey"},
691
- "sub-conclusion": {"shape": "rect", "color": "#0072B2"},
692
- "strategy": {"shape": "hexagon", "style": "filled", "fillcolor": "#F0C27F"},
693
- "evidence": {"shape": "note", "style": "filled", "fillcolor": "#9ECAE1"},
694
- "support": {"shape": "rect", "style": "dotted"},
695
- }
696
- for node_id, attrs in G.nodes(data=True):
697
- var_type = attrs.get("type", "").lower()
698
- style = dict(node_attr_map.get(
699
- var_type, dict(fillcolor="white", shape="ellipse", style="filled")
700
- ))
701
-
702
- status = status_dict.get(node_id, "UNKNOWN")
703
- logging.info("Setting node color for %s with status %s", node_id, status)
704
- if status == StatusType.FAIL.name:
705
- style.update(style="filled", fillcolor="red", fontcolor="white", fontname="Helvetica-Bold")
706
- elif status == StatusType.SKIP.name:
707
- style.update(style="filled", fillcolor="#cccccc", opacity="1", fontcolor="white", fontname="Helvetica-Bold")
708
-
709
- label = attrs.get("label", node_id)
710
- dot.node(node_id, label=label, **style)
711
-
712
- @staticmethod
713
- def _style_edges(dot: Any, G: nx.DiGraph, status_dict: dict[str, str]) -> None:
714
- """
715
- Apply visual styles to graph edges based on the execution status of their source node.
716
-
717
- :param dot: graphviz.Digraph instance to add edges to.
718
- :param G: NetworkX DiGraph with edge data.
719
- :param status_dict: Mapping of node id -> status string ("PASS", "FAIL", "SKIP").
720
- """
721
- for source, target in G.edges():
722
- status = status_dict.get(source, "UNKNOWN")
723
- logging.info("Setting edge color for %s -> %s with status %s", source, target, status)
724
- if status == StatusType.PASS.name:
725
- dot.edge(source, target, color="black")
726
- elif status == StatusType.FAIL.name:
727
- dot.edge(source, target, color="red")
728
- elif status == StatusType.SKIP.name:
729
- dot.edge(source, target, color="#cccccc", opacity="1")
730
- else:
731
- dot.edge(source, target, color="gray")
654
+ GraphvizExporter(self.graph).export(status_dict, output_path, filename, format)
@@ -0,0 +1,106 @@
1
+ """
2
+ jpipe_runner.framework.graphviz_exporter
3
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
4
+
5
+ Graphviz rendering for justification pipeline graphs.
6
+ """
7
+
8
+ from pathlib import Path
9
+ from typing import Any
10
+
11
+ import networkx as nx
12
+
13
+ from ..enums import StatusType
14
+ from .logger import GLOBAL_LOGGER
15
+
16
+
17
+ class GraphvizExporter:
18
+ """
19
+ Renders a justification pipeline graph to an image file via Graphviz.
20
+
21
+ Handles DOT-safe node ID sanitization: element IDs containing `:`
22
+ (e.g. ``final:hook``) are replaced with ``_`` in the DOT graph while
23
+ the human-readable label is preserved for display.
24
+ """
25
+
26
+ _NODE_STYLES: dict[str, dict[str, str]] = {
27
+ "conclusion": {"shape": "rect", "style": "filled,rounded", "fillcolor": "lightgrey"},
28
+ "sub-conclusion": {"shape": "rect", "color": "#0072B2"},
29
+ "strategy": {"shape": "hexagon", "style": "filled", "fillcolor": "#F0C27F"},
30
+ "evidence": {"shape": "note", "style": "filled", "fillcolor": "#9ECAE1"},
31
+ "support": {"shape": "rect", "style": "dotted"},
32
+ }
33
+
34
+ def __init__(self, graph: nx.DiGraph) -> None:
35
+ self.graph = graph
36
+
37
+ def export(
38
+ self, status_dict: dict[str, str], output_path: str, filename: str, fmt: str
39
+ ) -> None:
40
+ """
41
+ Render the pipeline graph to an image file.
42
+
43
+ :param status_dict: Mapping of node id → status string ("PASS", "FAIL", "SKIP").
44
+ :param output_path: Directory path to save the exported image.
45
+ :param filename: Output filename (without extension).
46
+ :param fmt: Image format string (e.g. "png", "svg", "dot").
47
+ """
48
+ import graphviz as gv
49
+
50
+ resolved_path = self._resolve_output_path(output_path, filename)
51
+ dot = gv.Digraph()
52
+ dot.attr(rankdir="BT")
53
+ self._style_nodes(dot, status_dict)
54
+ self._style_edges(dot, status_dict)
55
+ dot.render(
56
+ str(resolved_path),
57
+ format=fmt,
58
+ engine="dot",
59
+ cleanup=True,
60
+ outfile=str(resolved_path.with_suffix(f".{fmt}")),
61
+ )
62
+
63
+ @staticmethod
64
+ def _dot_id(node_id: str) -> str:
65
+ """Return a DOT-safe identifier by replacing `:` with `_`."""
66
+ return node_id.replace(":", "_")
67
+
68
+ def _style_nodes(self, dot: Any, status_dict: dict[str, str]) -> None:
69
+ """Add nodes to *dot* styled by element type and execution status."""
70
+ for node_id, attrs in self.graph.nodes(data=True):
71
+ var_type = attrs.get("type", "").lower()
72
+ style = dict(self._NODE_STYLES.get(
73
+ var_type, {"fillcolor": "white", "shape": "ellipse", "style": "filled"}
74
+ ))
75
+
76
+ status = status_dict.get(node_id, "UNKNOWN")
77
+ GLOBAL_LOGGER.info("Setting node color for %s with status %s", node_id, status)
78
+ if status == StatusType.FAIL.name:
79
+ style.update(style="filled", fillcolor="red", fontcolor="white", fontname="Helvetica-Bold")
80
+ elif status == StatusType.SKIP.name:
81
+ style.update(style="filled", fillcolor="#cccccc", opacity="1", fontcolor="white", fontname="Helvetica-Bold")
82
+
83
+ label = attrs.get("label", node_id)
84
+ dot.node(self._dot_id(node_id), label=label, **style)
85
+
86
+ def _style_edges(self, dot: Any, status_dict: dict[str, str]) -> None:
87
+ """Add edges to *dot* colored by the source node's execution status."""
88
+ for source, target in self.graph.edges():
89
+ status = status_dict.get(source, "UNKNOWN")
90
+ GLOBAL_LOGGER.info("Setting edge color for %s -> %s with status %s", source, target, status)
91
+ if status == StatusType.PASS.name:
92
+ dot.edge(self._dot_id(source), self._dot_id(target), color="black")
93
+ elif status == StatusType.FAIL.name:
94
+ dot.edge(self._dot_id(source), self._dot_id(target), color="red")
95
+ elif status == StatusType.SKIP.name:
96
+ dot.edge(self._dot_id(source), self._dot_id(target), color="#cccccc", opacity="1")
97
+ else:
98
+ dot.edge(self._dot_id(source), self._dot_id(target), color="gray")
99
+
100
+ @staticmethod
101
+ def _resolve_output_path(output_path: str, filename: str) -> Path:
102
+ """Resolve and prepare the output directory, returning the full file path."""
103
+ path = Path(output_path)
104
+ if not path.exists():
105
+ path.mkdir(parents=True, exist_ok=True)
106
+ return path / filename
@@ -0,0 +1,21 @@
1
+ from .base import BaseValidator
2
+ from .duplicate_producer import DuplicateProducerValidator
3
+ from .evidence_dependency import EvidenceDependencyValidator
4
+ from .missing_variable import MissingVariableValidator
5
+ from .order import OrderValidator
6
+ from .produced_not_consumed import ProducedButNotConsumedValidator
7
+ from .schema import JustificationSchemaValidator
8
+ from .self_dependency import SelfDependencyValidator
9
+ from .unbound_element import UnboundElementValidator
10
+
11
+ __all__ = [
12
+ "BaseValidator",
13
+ "DuplicateProducerValidator",
14
+ "EvidenceDependencyValidator",
15
+ "JustificationSchemaValidator",
16
+ "MissingVariableValidator",
17
+ "OrderValidator",
18
+ "ProducedButNotConsumedValidator",
19
+ "SelfDependencyValidator",
20
+ "UnboundElementValidator",
21
+ ]
@@ -0,0 +1,41 @@
1
+ from jpipe_runner.framework.context import RuntimeContext
2
+
3
+ from ..logger import GLOBAL_LOGGER # noqa: F401 — re-exported for submodule use
4
+
5
+
6
+ class BaseValidator:
7
+ """
8
+ Abstract base class for all pipeline validation checks.
9
+
10
+ Subclasses must implement the `validate()` method and append any errors
11
+ encountered during validation to `self.errors`.
12
+
13
+ :param pipeline: The pipeline engine to validate.
14
+ :type pipeline: PipelineEngine
15
+ """
16
+
17
+ def __init__(self, pipeline: "PipelineEngine", ctx: "RuntimeContext") -> None:
18
+ self.pipeline = pipeline
19
+ self.ctx = ctx
20
+ self.errors: list[str] = []
21
+ self.warnings: list[str] = []
22
+
23
+ def validate(self) -> tuple[list[str], list[str]]:
24
+ """
25
+ Abstract method for performing validation.
26
+
27
+ Subclasses must override this method to implement specific validation logic. This method
28
+ should populate `self.errors` with detailed error messages and return them.
29
+
30
+ :raises NotImplementedError: If called on the abstract base class.
31
+ :return: A list of error messages (if any).
32
+ :rtype: list[str]
33
+ """
34
+ raise NotImplementedError("Subclasses must implement the `validate()` method.")
35
+
36
+ def _find_element_context(self, fn_name: str) -> tuple[str, str, str] | None:
37
+ """Return (node_id, label, element_type) for the graph node bound to fn_name, or None."""
38
+ for node_id, data in self.pipeline.graph.nodes(data=True):
39
+ if data.get("function_name") == fn_name:
40
+ return node_id, data.get("label", node_id), data.get("type", "unknown")
41
+ return None
@@ -0,0 +1,47 @@
1
+ from jpipe_runner.framework.context import RuntimeContext
2
+
3
+ from ..logger import GLOBAL_LOGGER
4
+ from .base import BaseValidator
5
+
6
+
7
+ class DuplicateProducerValidator(BaseValidator):
8
+ """
9
+ Validator that checks that no variable is produced by more than one function.
10
+
11
+ A variable in a pipeline must be produced by only a single function to ensure
12
+ clear data provenance and avoid ambiguity in execution dependencies.
13
+ """
14
+
15
+ def validate(self) -> tuple[list[str], list[str]]:
16
+ """
17
+ Validate that each produced variable is only produced by a single function.
18
+
19
+ :return: A list of error messages for duplicate producers.
20
+ :rtype: list[str]
21
+ """
22
+ GLOBAL_LOGGER.info("Running DuplicateProducerValidator...")
23
+ variable_to_producers: dict[str, list[str]] = {}
24
+
25
+ for func_key, var_maps in self.ctx._vars.items():
26
+ produced_vars = var_maps.get(RuntimeContext.PRODUCE, {})
27
+ GLOBAL_LOGGER.debug(f"Function '{func_key}' produces: {list(produced_vars)}")
28
+ for var in produced_vars:
29
+ variable_to_producers.setdefault(var, []).append(func_key)
30
+
31
+ for var, producers in variable_to_producers.items():
32
+ if len(producers) > 1:
33
+ error_message = (
34
+ "[DuplicateProducerValidator]\n"
35
+ "Pipeline validation error: duplicate producers detected.\n"
36
+ f" • Variable '{var}' is produced by multiple functions: {producers}\n"
37
+ " • Each variable must have exactly one producer to maintain a valid pipeline structure.\n"
38
+ " • To fix:\n"
39
+ f" - Choose a single function to produce '{var}' and remove it from the others.\n"
40
+ " - If multiple outputs are required, consider renaming or splitting the variables.\n"
41
+ )
42
+ self.errors.append(error_message)
43
+
44
+ GLOBAL_LOGGER.info(
45
+ f"DuplicateProducerValidator completed with {len(self.errors)} error(s)."
46
+ )
47
+ return self.errors, self.warnings
@@ -0,0 +1,186 @@
1
+ import networkx as nx
2
+
3
+ from jpipe_runner.framework.context import RuntimeContext
4
+
5
+ from ..logger import GLOBAL_LOGGER
6
+ from .base import BaseValidator
7
+
8
+
9
+ class EvidenceDependencyValidator(BaseValidator):
10
+ """
11
+ Validates the dependency relationship between leaf nodes and strategy nodes.
12
+
13
+ Specifically, it ensures that:
14
+ 1. Every evidence node produces at least one output variable.
15
+ 2. Every strategy node directly connected above a leaf node (evidence or bound
16
+ sub-conclusion) consumes all variables produced by that leaf.
17
+ """
18
+
19
+ def __init__(
20
+ self, pipeline: "PipelineEngine", ctx: "RuntimeContext", graph: nx.DiGraph
21
+ ) -> None:
22
+ """
23
+ Initialize the EvidenceDependencyValidator.
24
+
25
+ :param pipeline: The pipeline engine being validated.
26
+ :type pipeline: PipelineEngine
27
+ :param ctx: The runtime context containing mappings of variables produced and consumed.
28
+ :type ctx: RuntimeContext
29
+ :param graph: The justification graph representing function dependencies.
30
+ :type graph: nx.DiGraph
31
+ """
32
+ super().__init__(pipeline, ctx)
33
+ self.graph = graph
34
+
35
+ def validate(self) -> tuple[list[str], list[str]]:
36
+ """
37
+ Perform the validation of leaf-to-strategy variable dependencies.
38
+
39
+ :return: A tuple of (errors, warnings).
40
+ :rtype: tuple[list[str], list[str]]
41
+ """
42
+ GLOBAL_LOGGER.info("Running EvidenceDependencyValidator...")
43
+ errors: list[str] = []
44
+ warnings: list[str] = []
45
+
46
+ leaf_strategy_edges = self._get_leaf_to_strategy_edges()
47
+
48
+ for node_id, data in self.graph.nodes(data=True):
49
+ node_type = data.get("type")
50
+ fn_name = data.get("function_name")
51
+ label = data.get("label", node_id)
52
+
53
+ if node_type == "evidence":
54
+ produced_vars = self._get_produced_variables(fn_name)
55
+ if not produced_vars:
56
+ errors.append(self._create_no_variables_error(node_id, label))
57
+ continue
58
+ connected = self._get_connected_strategies(fn_name, leaf_strategy_edges)
59
+ errors.extend(
60
+ self._validate_strategy_consumption(node_id, label, produced_vars, connected)
61
+ )
62
+
63
+ elif node_type == "sub-conclusion" and self.ctx._vars.get(fn_name):
64
+ produced_vars = self._get_produced_variables(fn_name)
65
+ if produced_vars:
66
+ connected = self._get_connected_strategies(fn_name, leaf_strategy_edges)
67
+ errors.extend(
68
+ self._validate_strategy_consumption(node_id, label, produced_vars, connected)
69
+ )
70
+
71
+ GLOBAL_LOGGER.info(
72
+ f"EvidenceDependencyValidator completed with {len(errors)} error(s) and {len(warnings)} warning(s)."
73
+ )
74
+ return errors, warnings
75
+
76
+ def _get_leaf_to_strategy_edges(self) -> list[tuple[str, str]]:
77
+ """
78
+ Return (leaf_fn_name, strategy_fn_name) for every direct edge where the predecessor
79
+ is evidence or a bound sub-conclusion and the successor is a strategy.
80
+ """
81
+ LEAF_TYPES = {"evidence", "sub-conclusion"}
82
+ edges = []
83
+ for u, v in self.graph.edges():
84
+ u_type = self.graph.nodes[u].get("type")
85
+ v_type = self.graph.nodes[v].get("type")
86
+ if u_type not in LEAF_TYPES or v_type != "strategy":
87
+ continue
88
+ u_fn = self.graph.nodes[u].get("function_name")
89
+ # Sub-conclusions without a bound function (no ctx entry) are skipped
90
+ if u_type == "sub-conclusion" and not self.ctx._vars.get(u_fn):
91
+ continue
92
+ edges.append((u_fn, self.graph.nodes[v].get("function_name")))
93
+ return edges
94
+
95
+ def _get_produced_variables(self, function_name: str) -> list[str]:
96
+ """
97
+ Retrieve variables produced by the given function.
98
+
99
+ :param function_name: Name of the function.
100
+ :type function_name: str
101
+ :return: List of variable names the function produces.
102
+ :rtype: list[str]
103
+ """
104
+ return list(self.ctx._vars.get(function_name, {}).get(RuntimeContext.PRODUCE, {}).keys())
105
+
106
+ def _get_consumed_variables(self, function_name: str) -> list[str]:
107
+ """
108
+ Retrieve variables consumed by the given function.
109
+
110
+ :param function_name: Name of the function.
111
+ :type function_name: str
112
+ :return: List of variable names the function consumes.
113
+ :rtype: list[str]
114
+ """
115
+ return list(self.ctx._vars.get(function_name, {}).get(RuntimeContext.CONSUME, {}).keys())
116
+
117
+ @staticmethod
118
+ def _get_connected_strategies(
119
+ leaf_fn: str, leaf_strategy_edges: list[tuple[str, str]]
120
+ ) -> list[str]:
121
+ """
122
+ Get all strategy function names directly connected to a given leaf node.
123
+
124
+ :param leaf_fn: The leaf function name.
125
+ :param leaf_strategy_edges: All leaf-to-strategy edges.
126
+ :return: List of strategy function names.
127
+ :rtype: list[str]
128
+ """
129
+ return [strategy for lf, strategy in leaf_strategy_edges if lf == leaf_fn]
130
+
131
+ def _validate_strategy_consumption(
132
+ self, node_id: str, label: str, produced_vars: list[str], connected_strategies: list[str]
133
+ ) -> list[str]:
134
+ """
135
+ Check that all connected strategies consume every variable produced by the leaf node.
136
+
137
+ :return: List of formatted error messages (if any).
138
+ :rtype: list[str]
139
+ """
140
+ errors = []
141
+ non_consuming_strategies = []
142
+
143
+ for strategy in connected_strategies:
144
+ consumed_vars = self._get_consumed_variables(strategy)
145
+ if not all(var in consumed_vars for var in produced_vars):
146
+ non_consuming_strategies.append(strategy)
147
+
148
+ if non_consuming_strategies:
149
+ errors.append(
150
+ self._create_consumption_error(node_id, label, produced_vars, non_consuming_strategies)
151
+ )
152
+
153
+ return errors
154
+
155
+ @staticmethod
156
+ def _create_no_variables_error(node_id: str, label: str) -> str:
157
+ """
158
+ Generate an error message for an evidence node that produces no variables.
159
+ """
160
+ return (
161
+ "[EvidenceDependencyValidator]\n"
162
+ "Pipeline validation error: evidence node does not produce any variables.\n"
163
+ f" • Element: {node_id} (\"{label}\") [evidence]\n"
164
+ f" • Problem: This evidence node produces no output variables.\n"
165
+ f" • Impact: Connected strategies will receive no inputs from this evidence.\n"
166
+ f" • Fix: Ensure the function bound to {node_id} calls produce(...) at least once.\n"
167
+ )
168
+
169
+ @staticmethod
170
+ def _create_consumption_error(
171
+ node_id: str, label: str, produced_vars: list[str], strategies: list[str]
172
+ ) -> str:
173
+ """
174
+ Generate an error message when strategy nodes do not consume all variables
175
+ produced by a leaf node.
176
+ """
177
+ strategy_list = "', '".join(strategies)
178
+ return (
179
+ "[EvidenceDependencyValidator]\n"
180
+ "Pipeline validation error: leaf node variables not consumed by connected strategies.\n"
181
+ f" • Element: {node_id} (\"{label}\")\n"
182
+ f" • Produced variables: {produced_vars}\n"
183
+ f" • Affected strategies: ['{strategy_list}']\n"
184
+ f" • Problem: These strategies do not consume all variables produced by {node_id}.\n"
185
+ f" • Fix: Verify that the strategies listed above declare consume=[...] for all outputs of {node_id}.\n"
186
+ )