Build AI methods with your coding agent, then run them anywhere: as an MCP for chatbots, as a webapp for people, or via API for your software.
feature/Unparsable-condition-expression-500s · L-260927-00be25 (#1277)
Louis Choquelbe796a0d8c2b
diff --git a/CHANGELOG.md b/CHANGELOG.mdindex a43a6599c..15fbf3382 100644--- a/CHANGELOG.md+++ b/CHANGELOG.md@@ -27,7 +27,8 @@- **Every validation error reaches the reader**: an error the blueprint categorizer does not know, such as a misspelled field, is its own `blueprint_validation` item without an `error_type`, carrying its `source`, `pipe_code` and `field_path`, where it used to vanish whenever another item existed and surface only once that one was fixed; and `format_pydantic_validation_error` renders every error it is given, listing the kinds without a heading of their own under "Other validation errors" whatever else is present.- **A validation verdict names no path on the host**: an item located inside a package the method depends on by address, and a duplicate declaration inside that package, now name the bundle by the package's address and its path inside the package (`github.com/acme/harbour-methods/tides/notices/tide_notices.mthds`) instead of the directory the host installed it in, on every surface; and when submitted content is validated or run beside a host's own library directories, an item located in one of their files carries no `source` or `field_path` naming it and a message that named such a file, or the module one of their Python files was imported as, reads `<host library file>`, so STRICT disclosure no longer hands a hosted caller a path on the server. A bundle validated from a file keeps the full paths of its own library directories, and so does content validated against directories that are the caller's: `validate_bundle` and `validate_bundles_in_process` take `library_dirs_are_callers`, which `PipelexMTHDSProtocol.validate` now passes as its `execute` does.-- **A run that fails on the caller's own method reads its reason under STRICT disclosure**: a `PipeParallel` whose branch results do not fit its output, a pipe started without a required input, a `PipeCondition` whose expression renders nothing, whose outcome is `fail` or whose chosen pipe misses inputs, and a step naming in an inline setting a model the deck neither defines nor names now report the `input` domain, so an HTTP surface answers 422 instead of 500, a message STRICT disclosure keeps instead of `An internal error occurred.`, and a next step: for the model, a `change_model` action naming the model as the method wrote it and the type of model its pipe needs, which the model panel of `pipelex run` follows with its local-deck advice. A `PipeCondition`'s dry-run refusals, among them an expression rendering nothing and every outcome mapped to `fail`, now give a `dry_run` item carrying their reason instead of the title `Pipe run`, and name the missing pipes or the line an unparsable expression fails at instead of printing the outcome map or the expression; a `fail` outcome's message names the pipe and no longer the value its expression rendered. A model that the deck itself names but does not serve stays a redacted `config` error. The new `PipelexError.as_caller_fault()` is how a raise site classifies one error of a class whose other instances are not the caller's fault, as [the error model](under-the-hood/error-model.md#classified-where-it-is-raised) describes.+- **A run that fails on the caller's own method reads its reason under STRICT disclosure**: a `PipeParallel` whose branch results do not fit its output, a pipe started without a required input, a `PipeCondition` whose expression renders nothing, whose outcome is `fail` or whose chosen pipe misses inputs, and a step naming in an inline setting a model the deck neither defines nor names now report the `input` domain, so an HTTP surface answers 422 instead of 500, a message STRICT disclosure keeps instead of `An internal error occurred.`, and a next step: for the model, a `change_model` action naming the model as the method wrote it and the type of model its pipe needs, which the model panel of `pipelex run` follows with its local-deck advice. A `PipeCondition`'s dry-run refusals, among them an expression rendering nothing and every outcome mapped to `fail`, now give a `dry_run` item carrying their reason instead of the title `Pipe run`, and name the missing pipes instead of printing the outcome map; a `fail` outcome's message names the pipe and no longer the value its expression rendered, and neither does the absence reason a `continue` outcome records, which the message of a step force-unwrapping the condition's output quotes under STRICT disclosure. A model that the deck itself names but does not serve stays a redacted `config` error. The new `PipelexError.as_caller_fault()` is how a raise site classifies one error of a class whose other instances are not the caller's fault, as [the error model](under-the-hood/error-model.md#classified-where-it-is-raised) describes.+- **A `PipeCondition` whose expression does not parse is an item of the verdict**: validation refuses it as a `blueprint_validation` item on the condition, naming its `expression` or `expression_template` and the line of it that fails, and refuses the same way an expression that parses but that the run could not compile, such as one naming a filter or a test that does not exist outside any conditional (Jinja2 leaves a name inside an `{% if %}` or an inline `if` to be resolved when the expression renders); a run of the bundle is refused with that verdict, which a hosted run answers with HTTP 422, where the parser's error used to escape the load with no verdict and a hosted run answered 500 `An internal error occurred.`. The item quotes neither the expression nor the parser's diagnosis, since the condition may be a host library's.## [v0.66.1] - 2026-09-26diff --git a/docs/building-methods/pipes/pipe-controllers/PipeCondition.md b/docs/building-methods/pipes/pipe-controllers/PipeCondition.mdindex 164110092..14115bca4 100644--- a/docs/building-methods/pipes/pipe-controllers/PipeCondition.md+++ b/docs/building-methods/pipes/pipe-controllers/PipeCondition.md@@ -17,6 +17,8 @@ The `PipeCondition` controller adds branching logic to your pipelines. It evaluaWhen the expression renders nothing, no outcome can be chosen and the pipe fails with `Conditional expression returned no result`, naming the pipe. That refusal and the others a condition makes are faults in your method, each reported with its reason and a next step. In a run, an expression that renders nothing, a `fail` outcome and a chosen pipe whose inputs are missing fail the run, which a hosted run answers with HTTP 422. In the dry run of `pipelex validate`, which runs every branch instead of choosing one, an expression that renders nothing over the mock inputs and a condition whose every outcome is `fail` give a `dry_run` item.+An expression that does not parse, whether written as `expression` or as `expression_template`, is refused when the bundle loads, before anything runs, and so is one the run could not compile, such as one naming a filter or a test that does not exist outside any conditional. Jinja2 resolves a name inside an `{% if %}` block or an inline `if` only when the expression renders, so a wrong name there fails the run rather than the load. `pipelex validate` reports either as an item on the condition naming the field and the line of it that fails, and a run of the bundle is refused with the same item, which a hosted run answers with HTTP 422. The item quotes none of the expression, which may be a host library's.+## Configuration`PipeCondition` is configured in your pipeline's `.mthds` file.@@ -147,7 +149,7 @@ Process this unknown document type...Beside pipe names, an outcome (or the `default_outcome`) can be one of two special outcomes:- **`fail`**: the run fails loudly with an error naming the pipe. Use it to make "this should never happen" branches explicit. The failure is a refusal written into your method, so a hosted run answers it with HTTP 422 and keeps its message, with a next step, under STRICT disclosure. The message leaves out the value the expression rendered, which the run's execution graph keeps: that value can be text of a condition a host's library declared.-- **`continue`**: the condition declares that it produced **no output**. The runtime records an absence for the declared output (with the evaluated expression as the reason) and the run continues as a success — the rest of the working memory is unchanged.+- **`continue`**: the condition declares that it produced **no output**. The runtime records an absence for the declared output, whose reason names the condition and its `continue` outcome, and the run continues as a success — the rest of the working memory is unchanged. The reason leaves out the value the expression rendered, which the run's execution graph keeps, for the same reason a `fail` outcome's message does: a pipe that later requires the output quotes the reason to the caller.Because `continue` resolves the declared output as absent, a `continue`-reachable condition (any outcome mapped to `continue`, or `default_outcome = "continue"`) **must declare its output optional** — `output = "Constraint?"`. Validation enforces this statically (`optional_output_required`), so the no-output path is always visible to the type system. The same visibility rule applies at the condition's boundary: if a mapped outcome pipe declares an optional output, the condition must declare `?` too (`optional_not_handled`).diff --git a/pipelex/pipe_controllers/condition/pipe_condition.py b/pipelex/pipe_controllers/condition/pipe_condition.pyindex 9ef4826f7..6e1a5f175 100644--- a/pipelex/pipe_controllers/condition/pipe_condition.py+++ b/pipelex/pipe_controllers/condition/pipe_condition.py@@ -1,6 +1,5 @@from typing import TYPE_CHECKING, Any, Literal-from jinja2 import TemplateSyntaxErrorfrom typing_extensions import overridefrom pipelex import log@@ -14,6 +13,7 @@ from pipelex.core.pipes.inputs.input_stuff_specs import InputStuffSpecsfrom pipelex.core.pipes.inputs.input_stuff_specs_factory import InputStuffSpecsFactoryfrom pipelex.core.pipes.pipe_output import PipeOutputfrom pipelex.interpreter_hub import get_optional_pipe, get_pipe_router, get_required_pipe+from pipelex.pipe_controllers.condition.pipe_condition_blueprint import describe_expression_parse_failurefrom pipelex.pipe_controllers.condition.special_outcome import SpecialOutcomefrom pipelex.pipe_controllers.pipe_controller import PipeControllerfrom pipelex.pipe_machinery.template_guard_lint import lint_optional_input_guards@@ -316,10 +316,13 @@ class PipeCondition(PipeController):if SpecialOutcome.is_continue(outcome):log.dev(f"PipeCondition '{self.code}' continued with outcome: {outcome}. Evaluated expression: {evaluated_expression}")self._register_execution_data(job_metadata=job_metadata, execution_data=execution_data_dict)+ # The reason names only the pipe and the outcome: a pipe that force-unwraps this output quotes it to the caller+ # under STRICT disclosure, and the value the expression rendered can be literal text of a condition a host+ # library declared. The execution data above keeps that value.self._record_declared_absent_output(working_memory=working_memory,output_name=output_name,- reason=f"PipeCondition '{self.code}' resolved to 'continue' for evaluated expression '{evaluated_expression}'",+ reason=f"PipeCondition '{self.code}' resolved to its 'continue' outcome",)return PipeOutput(working_memory=working_memory, pipeline_run_id=job_metadata.run_metadata.pipeline_run_id)@@ -406,7 +409,7 @@ class PipeCondition(PipeController):# must not reach the caller. The line locates the fault. It is raised from the parser's own error,# past the `Jinja2DetectVariablesError` that quotes the expression: a run failure reports the innermost# Pipelex error on its chain (`find_root_fault`), which must be this refusal.- msg = f"Dry run failed for pipe '{self.code}' (PipeCondition): its expression {_describe_expression_parse_failure(error=exc)}."+ msg = f"Dry run failed for pipe '{self.code}' (PipeCondition): its expression {describe_expression_parse_failure(error=exc)}."raise PipeRunError(message=msg, run_mode=pipe_run_params.run_mode, pipe_code=self.code).as_caller_fault(user_action=UserAction(kind=UserActionKind.CHANGE_INPUT,@@ -489,18 +492,3 @@ class PipeCondition(PipeController):producing_pipe=self.code,)working_memory.record_new_main_absence(record)---def _describe_expression_parse_failure(*, error: Jinja2DetectVariablesError) -> str:- """Say where a condition's expression fails to parse, in words that owe nothing to the expression.-- Every layer between Jinja2 and the condition puts the expression in its message, and Jinja2's own- diagnosis quotes the token it stopped at, so no text of the error can be passed on. The parser's line,- counted within the expression, is what locates the fault.- """- cause: BaseException | None = error.__cause__- while cause is not None:- if isinstance(cause, TemplateSyntaxError):- return f"does not parse at line {cause.lineno} of that expression"- cause = cause.__cause__- return "does not parse"diff --git a/pipelex/pipe_controllers/condition/pipe_condition_blueprint.py b/pipelex/pipe_controllers/condition/pipe_condition_blueprint.pyindex fea27ba72..34716206c 100644--- a/pipelex/pipe_controllers/condition/pipe_condition_blueprint.py+++ b/pipelex/pipe_controllers/condition/pipe_condition_blueprint.py@@ -1,10 +1,15 @@from typing import Annotated, Literal, Self+from jinja2 import TemplateSyntaxErrorfrom pydantic import Field, WithJsonSchema, field_validator, model_validatorfrom typing_extensions import overridefrom pipelex.pipe_controllers.condition.special_outcome import SpecialOutcomefrom pipelex.pipe_machinery.pipe_blueprint import PipeBlueprint+from pipelex.tools.jinja2.exceptions import Jinja2DetectVariablesError+from pipelex.tools.jinja2.jinja2_environment import make_jinja2_env_without_loader+from pipelex.tools.jinja2.jinja2_required_variables import detect_jinja2_required_variables+from pipelex.tools.jinja2.template_category import TemplateCategoryfrom pipelex.tools.typing.validation_utils import has_exactly_one_among_attributes_from_listOutcomeMap = dict[str, str]@@ -40,6 +45,19 @@ class PipeConditionBlueprint(PipeBlueprint):pipe_codes.add(self.default_outcome)return pipe_codes - set(SpecialOutcome.value_list())+ @property+ def runtime_expression(self) -> str | None:+ """The Jinja2 template the built ``PipeCondition`` renders to choose its outcome, or ``None`` when neither field gives one.++ An ``expression_template`` is the template as written; an ``expression`` is wrapped in ``{{ … }}``. Neither+ adds a line, so a line of the template is the same line of the field the author wrote.+ """+ if self.expression_template:+ return self.expression_template+ if self.expression:+ return "{{ " + self.expression + " }}"+ return None+@field_validator("outcomes", mode="after")@classmethoddef validate_outcome_map(cls, outcomes: OutcomeMap) -> OutcomeMap:@@ -57,8 +75,54 @@ class PipeConditionBlueprint(PipeBlueprint):@overridedef validate_inputs(self):- pass+ # The expression is parsed where the bundle loads, so every surface that loads it refuses an expression that+ # does not parse as an item of its verdict, located on the pipe, as it refuses a prompt or a template that+ # does not parse. The parse is the one the built pipe makes when it reads the variables its expression needs.+ # It is then compiled as the run compiles it before rendering, in the same environment, which refuses what+ # parses but cannot be built, such as a filter or a test that does not exist: the run would refuse it on+ # every input, so the refusal is no stricter for being made at load. Jinja2 resolves a filter or a test inside+ # a conditional only when it renders, so a wrong name there compiles and is left to the run, as it must be:+ # refusing it here would refuse an expression whose run never reaches it.+ # The message quotes neither the expression nor the parser's diagnosis, which names the token it stopped at:+ # the verdict is kept verbatim under STRICT disclosure, and the condition may be a host library's.+ if not has_exactly_one_among_attributes_from_list(self, attributes_list=["expression_template", "expression"]):+ # Both fields or neither: the validator of the pair refuses that and says why, where a refusal of the+ # parse of one of them, which runs first, would hide it.+ return+ runtime_expression = self.runtime_expression+ if runtime_expression is None:+ return+ field_name = "expression_template" if self.expression_template else "expression"+ wanted = "a Jinja2 template" if self.expression_template else "a Jinja2 expression"+ try:+ detect_jinja2_required_variables(template_category=TemplateCategory.EXPRESSION, template_source=runtime_expression)+ except Jinja2DetectVariablesError as exc:+ msg = f"The '{field_name}' of this PipeCondition {describe_expression_parse_failure(error=exc)}. Fix it so that it parses as {wanted}."+ raise ValueError(msg) from exc.__cause__+ try:+ make_jinja2_env_without_loader(template_category=TemplateCategory.EXPRESSION).compile(runtime_expression)+ except TemplateSyntaxError as exc:+ msg = (+ f"The '{field_name}' of this PipeCondition does not compile at line {exc.lineno} of that expression. "+ f"Check that every filter and test it names exists, and fix it so that it compiles as {wanted}."+ )+ raise ValueError(msg) from exc@overridedef validate_output(self):pass+++def describe_expression_parse_failure(*, error: Jinja2DetectVariablesError) -> str:+ """Say where a condition's expression fails to parse, in words that owe nothing to the expression.++ Every layer between Jinja2 and the condition puts the expression in its message, and Jinja2's own+ diagnosis quotes the token it stopped at, so no text of the error can be passed on. The parser's line,+ counted within the expression, is what locates the fault.+ """+ cause: BaseException | None = error.__cause__+ while cause is not None:+ if isinstance(cause, TemplateSyntaxError):+ return f"does not parse at line {cause.lineno} of that expression"+ cause = cause.__cause__+ return "does not parse"diff --git a/pipelex/pipe_controllers/condition/pipe_condition_factory.py b/pipelex/pipe_controllers/condition/pipe_condition_factory.pyindex 9124963ef..018628200 100644--- a/pipelex/pipe_controllers/condition/pipe_condition_factory.py+++ b/pipelex/pipe_controllers/condition/pipe_condition_factory.py@@ -25,13 +25,8 @@ class PipeConditionFactory(PipeFactoryProtocol[PipeConditionBlueprint, PipeCondioutput: StuffSpec,blueprint: PipeConditionBlueprint,) -> PipeCondition:- # Compute expression from expression_template or expression in blueprint- expression: str | None = None- if blueprint.expression_template:- expression = blueprint.expression_template- elif blueprint.expression:- expression = "{{ " + blueprint.expression + " }}"- else:+ expression = blueprint.runtime_expression+ if expression is None:msg = "PipeCondition must have either expression_template or expression"raise PipeConditionFactoryError(msg)diff --git a/tests/integration/pipelex/pipeline/test_condition_expression_parse_refusal.py b/tests/integration/pipelex/pipeline/test_condition_expression_parse_refusal.pynew file mode 100644index 000000000..ae7aea19f--- /dev/null+++ b/tests/integration/pipelex/pipeline/test_condition_expression_parse_refusal.py@@ -0,0 +1,133 @@+"""Pin: a `PipeCondition` whose expression does not parse is refused as an item of the bundle's verdict.++The expression is the method author's own, so the refusal is the caller's to fix: validation answers an invalid+verdict with one item on the condition, saying its expression does not parse and at which line, and a run of the+bundle answers with the same verdict, which a hosted run serves as HTTP 422. Before, the parser's own error+escaped the load, so validation gave no verdict and a hosted run answered 500 "An internal error occurred.".++The item quotes neither the expression nor the parser's diagnosis, which names the token it stopped at: the+verdict is kept verbatim under STRICT disclosure, and a condition declared in a host's library directories must+not put its text there.+"""++import json+from pathlib import Path+from typing import Any++import pytest++from pipelex.base_exceptions import DisclosureMode, ValidationErrorCategory, ValidationErrorItem+from pipelex.pipeline.exceptions import ValidateBundleError+from pipelex.pipeline.runner import PipelexMTHDSProtocol+from pipelex.pipeline.validate_bundle import validate_bundle+from tests.integration.pipelex.pipeline.test_data import ConditionExpressionParseTestData, ConditionMethodFaultsTestData+++def _routing_bundle(*, expression: str) -> str:+ return (+ ConditionMethodFaultsTestData.ROUTING_MTHDS.replace(+ ConditionMethodFaultsTestData.INPUTS_SLOT, ConditionMethodFaultsTestData.PARCEL_AND_LANE_INPUTS+ )+ .replace(ConditionMethodFaultsTestData.EXPRESSION_SLOT, expression)+ .replace(ConditionMethodFaultsTestData.OUTCOMES_SLOT, ConditionMethodFaultsTestData.LANE_OUTCOMES)+ .replace(ConditionMethodFaultsTestData.DEFAULT_OUTCOME_SLOT, "send_standard")+ )+++# A dangling operator, and a tag where an expression is expected: neither parses at the one line of the expression.+_DANGLING_OPERATOR_BUNDLE = _routing_bundle(expression="lane ==")+_TAG_INSIDE_EXPRESSION_BUNDLE = _routing_bundle(expression="{% if %}")+# A filter that does not exist: the expression parses, but the run could not compile it.+_UNKNOWN_FILTER_BUNDLE = _routing_bundle(expression="lane.text | lenght_of_lane")++_EXPRESSION_REASON = (+ "Validation error at 'pipe.route_parcel': The 'expression' of this PipeCondition does not parse at line 1 of that expression. "+ "Fix it so that it parses as a Jinja2 expression."+)+_TEMPLATE_REASON = (+ "Validation error at 'pipe.route_parcel': The 'expression_template' of this PipeCondition does not parse at line 2 of that expression. "+ "Fix it so that it parses as a Jinja2 template."+)+_COMPILE_REASON = (+ "Validation error at 'pipe.route_parcel': The 'expression' of this PipeCondition does not compile at line 1 of that expression. "+ "Check that every filter and test it names exists, and fix it so that it compiles as a Jinja2 expression."+)++_CASES = pytest.mark.parametrize(+ ("bundle", "reason", "expression_text"),+ [+ (_DANGLING_OPERATOR_BUNDLE, _EXPRESSION_REASON, "lane =="),+ (_TAG_INSIDE_EXPRESSION_BUNDLE, _EXPRESSION_REASON, "{% if %}"),+ (ConditionExpressionParseTestData.UNKNOWN_TAG_TEMPLATE_MTHDS, _TEMPLATE_REASON, "frobnicate_the_parcel"),+ (_UNKNOWN_FILTER_BUNDLE, _COMPILE_REASON, "lenght_of_lane"),+ ],+ ids=["dangling_operator", "tag_inside_expression", "unknown_tag_on_line_2", "unknown_filter"],+)+++def _assert_is_the_refusal(*, item: ValidationErrorItem, reason: str, expression_text: str) -> None:+ assert item.category == ValidationErrorCategory.BLUEPRINT_VALIDATION+ assert item.pipe_code == "route_parcel"+ assert item.domain_code == "condition_faults_routing"+ assert item.field_path == "pipe.route_parcel"+ assert item.message == reason+ assert expression_text not in item.message+++def _strict_item_messages(payload: dict[str, Any]) -> list[str]:+ return [item["message"] for item in payload["validation_errors"]]++[email protected](loop_scope="class")+class TestConditionExpressionParseRefusal:+ @_CASES+ async def test_validation_refuses_it_as_an_item_on_the_condition(self, bundle: str, reason: str, expression_text: str) -> None:+ """The verdict carries one item on the condition, the same locally and in the STRICT payload."""+ with pytest.raises(ValidateBundleError) as exc_info:+ await validate_bundle(mthds_contents=[bundle])+ verdict = exc_info.value++ (item,) = verdict.to_error_report().validation_errors or []+ _assert_is_the_refusal(item=item, reason=reason, expression_text=expression_text)++ strict_payload = verdict.to_error_report().to_dict(disclosure_mode=DisclosureMode.STRICT)+ assert _strict_item_messages(strict_payload) == [reason]+ assert expression_text not in json.dumps(strict_payload)++ @_CASES+ async def test_a_run_answers_422_with_the_item(self, bundle: str, reason: str, expression_text: str) -> None:+ """A run is refused with the verdict validation gives, before any pipe runs, and a hosted run answers 422 with its item."""+ with pytest.raises(ValidateBundleError) as exc_info:+ await PipelexMTHDSProtocol().execute(+ pipe_code="route_parcel", mthds_contents=[bundle], inputs={"parcel": "a box of books", "lane": "express"}+ )+ verdict = exc_info.value++ (item,) = verdict.to_error_report().validation_errors or []+ _assert_is_the_refusal(item=item, reason=reason, expression_text=expression_text)++ document = verdict.to_error_report().to_problem_document(disclosure_mode=DisclosureMode.STRICT)+ assert document["status"] == 422+ assert document["error_domain"] == "input"+ assert document["error_type"] == "ValidateBundleError"+ assert document["detail"] == reason+ assert _strict_item_messages(document) == [reason]+ assert document["user_action"]["kind"] == "change_input"+ assert expression_text not in json.dumps(document)++ async def test_a_host_library_condition_puts_none_of_its_text_in_the_strict_verdict(self, tmp_path: Path) -> None:+ """Beside a host's library directory, the refusal of its condition names neither its expression nor its file."""+ host_library_dir = tmp_path / "host" / "library"+ host_library_dir.mkdir(parents=True)+ (host_library_dir / "routing.mthds").write_text(ConditionExpressionParseTestData.UNKNOWN_TAG_TEMPLATE_MTHDS, encoding="utf-8")++ with pytest.raises(ValidateBundleError) as exc_info:+ await validate_bundle(mthds_contents=[ConditionExpressionParseTestData.VALID_CALLER_MTHDS], library_dirs=[host_library_dir])++ strict_payload = exc_info.value.to_error_report().to_dict(disclosure_mode=DisclosureMode.STRICT)+ assert _strict_item_messages(strict_payload) == [_TEMPLATE_REASON]+ serialized_payload = json.dumps(strict_payload)+ assert "frobnicate_the_parcel" not in serialized_payload+ assert "lane ==" not in serialized_payload+ assert str(tmp_path) not in serialized_payload+ assert str(tmp_path.resolve()) not in serialized_payloaddiff --git a/tests/integration/pipelex/pipeline/test_condition_method_faults.py b/tests/integration/pipelex/pipeline/test_condition_method_faults.pyindex 5f2ab91c1..f03e9031d 100644--- a/tests/integration/pipelex/pipeline/test_condition_method_faults.py+++ b/tests/integration/pipelex/pipeline/test_condition_method_faults.py@@ -8,6 +8,7 @@ hosted run answered with `An internal error occurred.`.from __future__ import annotations+import jsonfrom typing import Anyimport pytest@@ -122,3 +123,25 @@ class TestConditionMethodFaults:"selects another outcome, or map that outcome to a pipe or to 'continue'."),}++ async def test_a_continue_outcome_leaves_its_rendered_value_out_of_the_absence_reason(self) -> None:+ """A step force-unwrapping a result the condition resolved to 'continue' fails naming the condition, never the value it rendered."""+ with pytest.raises(PipelineExecutionError) as exc_info:+ await PipelexMTHDSProtocol().execute(+ pipe_code="route_and_stamp",+ mthds_contents=[ConditionMethodFaultsTestData.CONTINUE_THEN_FORCE_MTHDS],+ inputs={"parcel": "a box of books", "lane": "hold"},+ )+ report = exc_info.value.to_error_report()++ assert report.error_type == "OptionalValueAbsentError"+ assert (+ "Absence origin: pipe 'route_parcel' produced no value — \"PipeCondition 'route_parcel' resolved to its 'continue' outcome\""+ in report.message+ )+ assert ConditionMethodFaultsTestData.HELD_LANE_LITERAL not in report.message++ # The message is caller-facing, so STRICT disclosure keeps it, and nothing in the STRICT answer quotes the expression.+ document = report.to_problem_document(disclosure_mode=DisclosureMode.STRICT)+ assert document["detail"] == report.message+ assert ConditionMethodFaultsTestData.HELD_LANE_LITERAL not in json.dumps(document)diff --git a/tests/integration/pipelex/pipeline/test_data.py b/tests/integration/pipelex/pipeline/test_data.pyindex 591476cf1..f796bfd02 100644--- a/tests/integration/pipelex/pipeline/test_data.py+++ b/tests/integration/pipelex/pipeline/test_data.py@@ -299,3 +299,97 @@ template = "Standard: $parcel"LANE_OUTCOMES: ClassVar[str] = '{ express = "send_express", standard = "send_standard", reject = "fail" }'# Every outcome, and the default, refuse the run.ALL_FAIL_OUTCOMES: ClassVar[str] = '{ reject = "fail" }'++ # The literal a lane the condition holds renders to, text of the expression the absence reason must not quote.+ HELD_LANE_LITERAL: ClassVar[str] = "hold_at_the_depot_quietly"+ # A sequence whose condition resolves a held lane to 'continue', and whose next step force-unwraps its absent result.+ CONTINUE_THEN_FORCE_MTHDS: ClassVar[str] = """+domain = "condition_faults_routing"+description = "Stamp a parcel once it is routed down a lane"+main_pipe = "route_and_stamp"++[concept]+Parcel = "A parcel waiting at the sorting bench"++[pipe.route_and_stamp]+type = "PipeSequence"+description = "Route the parcel, then stamp it"+inputs = { parcel = "Parcel", lane = "Text" }+output = "Parcel"+steps = [{ pipe = "route_parcel", result = "routed" }, { pipe = "stamp_parcel", result = "stamped" }]++[pipe.route_parcel]+type = "PipeCondition"+description = "Choose the lane a parcel goes down, or hold it"+inputs = { parcel = "Parcel", lane = "Text" }+output = "Parcel?"+expression_template = "{% if lane.text == 'hold' %}hold_at_the_depot_quietly{% else %}standard{% endif %}"+outcomes = { standard = "send_standard" }+default_outcome = "continue"++[pipe.send_standard]+type = "PipeCompose"+description = "Note the parcel onto the standard lane"+inputs = { parcel = "Parcel" }+output = "Parcel"+template = "Standard: $parcel"++[pipe.stamp_parcel]+type = "PipeCompose"+description = "Stamp the routed parcel, which must be present"+inputs = { routed = "Parcel!" }+output = "Parcel"+template = "Stamped: $routed"+"""+++class ConditionExpressionParseTestData:+ """Bundles whose `PipeCondition` declares an expression that does not parse."""++ # A template whose second line opens a tag Jinja2 does not know.+ UNKNOWN_TAG_TEMPLATE_MTHDS: ClassVar[str] = '''+domain = "condition_faults_routing"+description = "Send a parcel down the lane the request names"+main_pipe = "route_parcel"++[concept]+Parcel = "A parcel waiting at the sorting bench"++[pipe.route_parcel]+type = "PipeCondition"+description = "Choose the lane a parcel goes down"+inputs = { parcel = "Parcel", lane = "Text" }+output = "Parcel"+expression_template = """{% if lane == 'express' %}express{% else %}standard+{% frobnicate_the_parcel %}{% endif %}"""+outcomes = { express = "send_express", standard = "send_standard" }+default_outcome = "send_standard"++[pipe.send_express]+type = "PipeCompose"+description = "Note the parcel onto the express lane"+inputs = { parcel = "Parcel" }+output = "Parcel"+template = "Express: $parcel"++[pipe.send_standard]+type = "PipeCompose"+description = "Note the parcel onto the standard lane"+inputs = { parcel = "Parcel" }+output = "Parcel"+template = "Standard: $parcel"+'''++ # A caller's bundle that is valid on its own, validated beside a host library holding an unparsable condition.+ VALID_CALLER_MTHDS: ClassVar[str] = """+domain = "harbour_notices"+description = "Post notices on the harbour board"+main_pipe = "write_board_notice"++[pipe.write_board_notice]+type = "PipeLLM"+description = "Write the notice for the harbour board"+inputs = { tide_times = "Text" }+output = "Text"+prompt = "Write a short notice for the harbour board from these tide times: $tide_times"+"""diff --git a/tests/integration/pipelex/pipes/controller/pipe_condition/test_pipe_condition_continue_delivery.py b/tests/integration/pipelex/pipes/controller/pipe_condition/test_pipe_condition_continue_delivery.pyindex ca3640266..c532e4d46 100644--- a/tests/integration/pipelex/pipes/controller/pipe_condition/test_pipe_condition_continue_delivery.py+++ b/tests/integration/pipelex/pipes/controller/pipe_condition/test_pipe_condition_continue_delivery.py@@ -79,9 +79,11 @@ class TestPipeConditionContinueDelivery:assert isinstance(resolved_main, AbsenceRecord)assert resolved_main.kind == AbsenceKind.DECLARED_ABSENTassert resolved_main.producing_pipe == "continue_gate"- # The record names the declared output slot and carries the evaluated expression.+ # The record names the declared output slot, and its reason names the pipe and its outcome but not the+ # value the expression rendered ("skip"), which a caller force-unwrapping the output would read.assert resolved_main.variable_name == "gate_result"- assert "skip" in resolved_main.reason+ assert resolved_main.reason == "PipeCondition 'continue_gate' resolved to its 'continue' outcome"+ assert "skip" not in resolved_main.reasonassert result_memory.get_optional_absence("gate_result") == resolved_main# Memory otherwise unchanged: the input is still there, untainted.assert result_memory.get_optional_stuff("input_text") is not Nonediff --git a/tests/unit/pipelex/pipe_controllers/condition/test_pipe_condition_blueprint.py b/tests/unit/pipelex/pipe_controllers/condition/test_pipe_condition_blueprint.pyindex 50f68e93c..856997ac0 100644--- a/tests/unit/pipelex/pipe_controllers/condition/test_pipe_condition_blueprint.py+++ b/tests/unit/pipelex/pipe_controllers/condition/test_pipe_condition_blueprint.py@@ -69,3 +69,120 @@ class TestPipeConditionBlueprint:default_outcome="process_default",)assert "PipeConditionBlueprint must have at least one mapping in outcomes" in str(exc_info.value)++ @pytest.mark.parametrize(+ ("expression", "expression_template", "field_name", "line", "wanted", "expression_text"),+ [+ ("lane ==", None, "expression", 1, "a Jinja2 expression", "lane =="),+ ("{% if %}", None, "expression", 1, "a Jinja2 expression", "{% if %}"),+ (+ None,+ "{% if lane == 'express' %}express\n{% frobnicate_the_parcel %}{% endif %}",+ "expression_template",+ 2,+ "a Jinja2 template",+ "frobnicate_the_parcel",+ ),+ ],+ ids=["dangling_operator", "tag_inside_expression", "unknown_tag_on_line_2"],+ )+ def test_an_expression_that_does_not_parse_is_refused_by_its_line_only(+ self, expression: str | None, expression_template: str | None, field_name: str, line: int, wanted: str, expression_text: str+ ):+ """The refusal names the field and the line it fails at, never a token of the expression or of the parser's diagnosis."""+ with pytest.raises(ValidationError) as exc_info:+ PipeConditionBlueprint(+ description="Choose the lane a parcel goes down",+ inputs={"lane": "Text"},+ output="Text",+ expression=expression,+ expression_template=expression_template,+ outcomes={"express": "send_express"},+ default_outcome="send_standard",+ )++ (error,) = exc_info.value.errors()+ assert error["msg"] == (+ f"Value error, The '{field_name}' of this PipeCondition does not parse at line {line} of that expression. "+ f"Fix it so that it parses as {wanted}."+ )+ assert expression_text not in error["msg"]++ @pytest.mark.parametrize(+ ("expression", "expression_template", "field_name", "line", "wanted", "expression_text"),+ [+ ("lane | lenght_of_lane", None, "expression", 1, "a Jinja2 expression", "lenght_of_lane"),+ ("lane is frobbed_lane", None, "expression", 1, "a Jinja2 expression", "frobbed_lane"),+ (None, "{{ lane }}\n{{ lane | frobnicate_the_parcel }}", "expression_template", 2, "a Jinja2 template", "frobnicate_the_parcel"),+ ],+ ids=["unknown_filter", "unknown_test", "unknown_filter_on_line_2"],+ )+ def test_an_expression_that_parses_but_does_not_compile_is_refused_by_its_line_only(+ self, expression: str | None, expression_template: str | None, field_name: str, line: int, wanted: str, expression_text: str+ ):+ """An expression the run could not compile, such as one naming a filter that does not exist, is refused at load by its line."""+ with pytest.raises(ValidationError) as exc_info:+ PipeConditionBlueprint(+ description="Choose the lane a parcel goes down",+ inputs={"lane": "Text"},+ output="Text",+ expression=expression,+ expression_template=expression_template,+ outcomes={"express": "send_express"},+ default_outcome="send_standard",+ )++ (error,) = exc_info.value.errors()+ assert error["msg"] == (+ f"Value error, The '{field_name}' of this PipeCondition does not compile at line {line} of that expression. "+ f"Check that every filter and test it names exists, and fix it so that it compiles as {wanted}."+ )+ assert expression_text not in error["msg"]++ def test_an_expression_using_a_filter_jinja2_has_is_accepted(self):+ blueprint = PipeConditionBlueprint(+ description="Choose the lane a parcel goes down",+ inputs={"lane": "Text"},+ output="Text",+ expression="lane | lower",+ outcomes={"express": "send_express"},+ default_outcome="send_standard",+ )+ assert blueprint.runtime_expression == "{{ lane | lower }}"++ def test_both_fields_are_refused_as_such_even_when_the_template_does_not_parse(self):+ """Declaring both fields is the fault reported, not the parse of the template the author may not have meant to keep."""+ with pytest.raises(ValidationError) as exc_info:+ PipeConditionBlueprint(+ description="Choose the lane a parcel goes down",+ inputs={"lane": "Text"},+ output="Text",+ expression="lane",+ expression_template="{% if %}",+ outcomes={"express": "send_express"},+ default_outcome="send_standard",+ )++ (error,) = exc_info.value.errors()+ assert error["msg"] == "Value error, PipeCondition should have exactly one of 'expression_template' or 'expression'"++ def test_the_runtime_expression_is_the_template_or_the_wrapped_expression(self):+ from_expression = PipeConditionBlueprint(+ description="Choose the lane a parcel goes down",+ inputs={"lane": "Text"},+ output="Text",+ expression="lane",+ outcomes={"express": "send_express"},+ default_outcome="send_standard",+ )+ from_template = PipeConditionBlueprint(+ description="Choose the lane a parcel goes down",+ inputs={"lane": "Text"},+ output="Text",+ expression_template="{% if lane %}{{ lane }}{% endif %}",+ outcomes={"express": "send_express"},+ default_outcome="send_standard",+ )++ assert from_expression.runtime_expression == "{{ lane }}"+ assert from_template.runtime_expression == "{% if lane %}{{ lane }}{% endif %}"