Skip to content

Pipeline

The detection pipeline transforms rule definitions into detector configurations and executes them.

Pipeline configuration

mcp_zen_of_languages.analyzers.pipeline.PipelineConfig

Bases: BaseModel

Typed container for the detector configs that drive a language pipeline.

A PipelineConfig holds an ordered list of DetectorConfig instances ready for execution by DetectionPipeline. Configs are either projected from zen principles via from_rules or loaded from zen-config.yaml and validated through the registry's discriminated-union :pyclass:TypeAdapter.

ATTRIBUTE DESCRIPTION
language

ISO-style language identifier (e.g. "python", "typescript").

TYPE: str

detectors

Ordered detector configs; validated on assignment via _validate_detectors.

TYPE: list[DetectorConfig]

Functions

from_rules classmethod

from_rules(language)

Build a complete pipeline by projecting a language's zen principles.

Loads the LanguageZenPrinciples for language, then delegates to configs_from_rules to project each principle's metrics onto the matching detector configs.

PARAMETER DESCRIPTION
language

Language key recognised by get_language_zen (e.g. "python").

TYPE: str

RETURNS DESCRIPTION
PipelineConfig

A fully populated PipelineConfig whose detector list reflects

TYPE: PipelineConfig

PipelineConfig

all zen principles defined for the language.

RAISES DESCRIPTION
ValueError

If no zen rules exist for language.

Examples:

>>> cfg = PipelineConfig.from_rules("python")
>>> cfg.language
'python'
Source code in src/mcp_zen_of_languages/analyzers/pipeline.py
@classmethod
def from_rules(cls, language: str) -> PipelineConfig:
    """Build a complete pipeline by projecting a language's zen principles.

    Loads the [`LanguageZenPrinciples`][mcp_zen_of_languages.rules.base_models.LanguageZenPrinciples]
    for *language*, then delegates to
    ``configs_from_rules``
    to project each principle's metrics onto the matching detector configs.

    Args:
        language (str): Language key recognised by [`get_language_zen`][mcp_zen_of_languages.rules.get_language_zen]
            (e.g. ``"python"``).

    Returns:
        PipelineConfig: A fully populated ``PipelineConfig`` whose detector list reflects
        all zen principles defined for the language.

    Raises:
        ValueError: If no zen rules exist for *language*.

    Examples:
        >>> cfg = PipelineConfig.from_rules("python")
        >>> cfg.language
        'python'
    """
    lang_zen = get_language_zen(language)
    if not lang_zen:
        msg = f"No zen rules for language: {language}"
        raise ValueError(msg)
    from mcp_zen_of_languages.analyzers.registry import REGISTRY

    detectors = REGISTRY.configs_from_rules(lang_zen)
    return cls(language=language, detectors=detectors)

Pipeline utilities

mcp_zen_of_languages.analyzers.pipeline.project_rules_to_configs

project_rules_to_configs(lang_zen)

Convert zen principle metric thresholds into typed detector configs.

For every ZenPrinciple in lang_zen, the function resolves which detectors are registered for that rule and maps the principle's metrics dict onto each detector's config fields. Keys that don't match any registered config field raise immediately so typos in rule definitions are caught at startup.

PARAMETER DESCRIPTION
lang_zen

The complete set of zen principles for a single language, including metric thresholds and violation specs.

TYPE: LanguageZenPrinciples

RETURNS DESCRIPTION
list[DetectorConfig]

list[DetectorConfig]: Ordered detector configs with thresholds populated from the rules.

See Also

DetectorRegistry.configs_from_rules — the registry method this function delegates to.

Source code in src/mcp_zen_of_languages/analyzers/pipeline.py
def project_rules_to_configs(lang_zen: LanguageZenPrinciples) -> list[DetectorConfig]:
    """Convert zen principle metric thresholds into typed detector configs.

    For every [`ZenPrinciple`][mcp_zen_of_languages.rules.base_models.ZenPrinciple]
    in *lang_zen*, the function resolves which detectors are registered for
    that rule and maps the principle's ``metrics`` dict onto each detector's
    config fields.  Keys that don't match any registered config field raise
    immediately so typos in rule definitions are caught at startup.

    Args:
        lang_zen (LanguageZenPrinciples): The complete set of zen principles for a single language,
            including metric thresholds and violation specs.

    Returns:
        list[DetectorConfig]: Ordered detector configs with thresholds populated from the rules.

    See Also:
        ``DetectorRegistry.configs_from_rules``
        — the registry method this function delegates to.
    """
    from mcp_zen_of_languages.analyzers.registry import REGISTRY

    return REGISTRY.configs_from_rules(lang_zen)

mcp_zen_of_languages.analyzers.pipeline.merge_pipeline_overrides

merge_pipeline_overrides(base, overrides)

Layer user overrides from zen-config.yaml onto rule-derived defaults.

Override entries are matched to base entries by DetectorConfig.type. When a match is found, only the fields explicitly set in the override are applied (via model_dump(exclude_unset=True)), preserving every rule-derived default that the user didn't touch. Overrides whose type doesn't appear in the base are appended as new detector entries.

PARAMETER DESCRIPTION
base

Pipeline produced by PipelineConfig.from_rules with thresholds derived from canonical zen principles.

TYPE: PipelineConfig

overrides

Pipeline section from zen-config.yaml, or None to skip merging entirely.

TYPE: PipelineConfig | None

RETURNS DESCRIPTION
PipelineConfig

A new PipelineConfig containing the merged detector list.

TYPE: PipelineConfig

RAISES DESCRIPTION
ValueError

If overrides.language doesn't match base.language.

Source code in src/mcp_zen_of_languages/analyzers/pipeline.py
def merge_pipeline_overrides(
    base: PipelineConfig,
    overrides: PipelineConfig | None,
) -> PipelineConfig:
    """Layer user overrides from ``zen-config.yaml`` onto rule-derived defaults.

    Override entries are matched to base entries by ``DetectorConfig.type``.
    When a match is found, only the fields explicitly set in the override are
    applied (via ``model_dump(exclude_unset=True)``), preserving every
    rule-derived default that the user didn't touch.  Overrides whose type
    doesn't appear in the base are appended as new detector entries.

    Args:
        base (PipelineConfig): Pipeline produced by ``PipelineConfig.from_rules`` with
            thresholds derived from canonical zen principles.
        overrides (PipelineConfig | None): Pipeline section from ``zen-config.yaml``, or ``None``
            to skip merging entirely.

    Returns:
        PipelineConfig: A new ``PipelineConfig`` containing the merged detector list.

    Raises:
        ValueError: If *overrides.language* doesn't match *base.language*.
    """
    if overrides is None:
        return base
    if overrides.language != base.language:
        msg = "Override pipeline language mismatch"
        raise ValueError(msg)
    from mcp_zen_of_languages.analyzers.registry import REGISTRY

    merged = REGISTRY.merge_configs(base.detectors, overrides.detectors)
    return PipelineConfig(language=base.language, detectors=merged)

Mapping models

mcp_zen_of_languages.analyzers.mapping_models.RuleBinding

Bases: BaseModel

Explicit binding between one rule id and its dogma/violation bounds.

Functions

model_post_init

model_post_init(__context)

Default empty violation selectors to the wildcard selector.

Source code in src/mcp_zen_of_languages/analyzers/mapping_models.py
def model_post_init(self, __context: object, /) -> None:
    """Default empty violation selectors to the wildcard selector."""
    if not self.violation_selectors:
        self.violation_selectors = ["*"]
    if self.verified_dogma_ids is None:
        self.verified_dogma_ids = (
            list(self.dogma_ids) if len(self.dogma_ids) == 1 else []
        )
    if self.verified_testing_ids is None:
        self.verified_testing_ids = (
            list(self.testing_ids) if len(self.testing_ids) == 1 else []
        )
    if self.verified_projection_ids is None:
        self.verified_projection_ids = (
            list(self.projection_ids) if len(self.projection_ids) == 1 else []
        )

mcp_zen_of_languages.analyzers.mapping_models.RuleDetectorBinding

Bases: BaseBinding

Declares which detector class enforces one or more zen rules.

Each binding is authored in a language's mapping.py and consumed by registry bootstrap during startup.

ATTRIBUTE DESCRIPTION
detector_id

Unique key that doubles as the type discriminator on the detector's config model.

TYPE: str

detector_class

Concrete ViolationDetector subclass to instantiate in the pipeline.

TYPE: type[ViolationDetector]

config_model

Pydantic model used to validate threshold values projected from zen principle metrics.

TYPE: type[DetectorConfig | AnalyzerConfig]

rules

Explicit per-rule bounds for this detector. Each bound rule carries its own violation selectors and dogma ids.

TYPE: list[RuleBinding]

default_order

Pipeline execution order; lower values run first.

TYPE: int

enabled_by_default

If False, the detector is excluded from the discriminated-union config adapter unless explicitly requested.

TYPE: bool

Attributes

rule_ids property

rule_ids

Return the ordered rule ids covered by this detector.

rule_map property

rule_map

Return violation-selector coverage grouped by rule id.

rule_dogma_map property

rule_dogma_map

Return explicit dogma ids grouped by rule id.

rule_verified_dogma_map property

rule_verified_dogma_map

Return explicitly authored verified dogma ids grouped by rule id.

rule_testing_map property

rule_testing_map

Return explicit testing family ids grouped by rule id.

rule_verified_testing_map property

rule_verified_testing_map

Return explicitly authored verified testing ids grouped by rule id.

rule_projection_map property

rule_projection_map

Return explicit projection family ids grouped by rule id.

rule_verified_projection_map property

rule_verified_projection_map

Return explicitly authored verified projection ids grouped by rule id.

Functions

build_bundle

build_bundle(language)

Build the rule-perspective bundle for a rule detector binding.

Source code in src/mcp_zen_of_languages/analyzers/mapping_models.py
def build_bundle(self, language: str) -> BindingPerspectiveBundle:
    """Build the rule-perspective bundle for a rule detector binding."""
    from mcp_zen_of_languages.analyzers.registry import DetectorMetadata

    dogma_rule_map: dict[str, list[str]] = {}
    dogma_verified_rule_map: dict[str, list[str]] = {}
    testing_rule_map: dict[str, list[str]] = {}
    testing_verified_rule_map: dict[str, list[str]] = {}
    projection_rule_map: dict[str, list[str]] = {}
    projection_verified_rule_map: dict[str, list[str]] = {}
    for rule_id in self.rule_ids:
        for dogma_id in self.rule_dogma_map.get(rule_id, []):
            dogma_rule_map.setdefault(dogma_id, []).append(rule_id)
        for dogma_id in self.rule_verified_dogma_map.get(rule_id, []):
            dogma_verified_rule_map.setdefault(dogma_id, []).append(rule_id)
        for testing_id in self.rule_testing_map.get(rule_id, []):
            testing_rule_map.setdefault(testing_id, []).append(rule_id)
        for testing_id in self.rule_verified_testing_map.get(rule_id, []):
            testing_verified_rule_map.setdefault(testing_id, []).append(rule_id)
        for projection_id in self.rule_projection_map.get(rule_id, []):
            projection_rule_map.setdefault(projection_id, []).append(rule_id)
        for projection_id in self.rule_verified_projection_map.get(rule_id, []):
            projection_verified_rule_map.setdefault(projection_id, []).append(
                rule_id
            )

    return BindingPerspectiveBundle(
        rule_model=DetectorMetadata(
            detector_id=self.detector_id,
            detector_class=self.detector_class,
            config_model=self.config_model,
            language=language,
            rule_ids=list(self.rule_ids),
            rule_map=dict(self.rule_map),
            rule_dogma_map=dict(self.rule_dogma_map),
            rule_verified_dogma_map=dict(self.rule_verified_dogma_map),
            default_order=self.default_order,
            enabled_by_default=self.enabled_by_default,
        ),
        dogma_model=DogmaPerspectiveModel(
            detector_id=self.detector_id,
            language=language,
            dogma_ids=list(
                dict.fromkeys(
                    [*dogma_rule_map.keys(), *dogma_verified_rule_map.keys()]
                ),
            ),
            dogma_rule_map=dogma_rule_map,
            dogma_verified_rule_map=dogma_verified_rule_map,
        ),
        testing_model=TestingPerspectiveModel(
            detector_id=self.detector_id,
            language=language,
            testing_ids=list(
                dict.fromkeys(
                    [*testing_rule_map.keys(), *testing_verified_rule_map.keys()],
                ),
            ),
            testing_rule_map=testing_rule_map,
            testing_verified_rule_map=testing_verified_rule_map,
        ),
        projection_model=ProjectionPerspectiveModel(
            detector_id=self.detector_id,
            language=language,
            projection_ids=list(
                dict.fromkeys(
                    [
                        *projection_rule_map.keys(),
                        *projection_verified_rule_map.keys(),
                    ],
                ),
            ),
            projection_rule_map=projection_rule_map,
            projection_verified_rule_map=projection_verified_rule_map,
        ),
    )

mcp_zen_of_languages.analyzers.mapping_models.NonRuleDetectorBinding

Bases: BaseBinding

Binding for generic detectors that do not map to explicit rules.

Functions

build_bundle

build_bundle(language)

Build the rule-perspective bundle for a non-rule detector binding.

Source code in src/mcp_zen_of_languages/analyzers/mapping_models.py
def build_bundle(self, language: str) -> BindingPerspectiveBundle:
    """Build the rule-perspective bundle for a non-rule detector binding."""
    from mcp_zen_of_languages.analyzers.registry import DetectorMetadata

    return BindingPerspectiveBundle(
        rule_model=DetectorMetadata(
            detector_id=self.detector_id,
            detector_class=self.detector_class,
            config_model=self.config_model,
            language=language,
            rule_ids=[],
            rule_map={},
            rule_dogma_map={},
            rule_verified_dogma_map={},
            default_order=self.default_order,
            enabled_by_default=self.enabled_by_default,
        ),
    )

mcp_zen_of_languages.analyzers.mapping_models.LanguageDetectorMap

Bases: BaseModel

All current detector bindings for one language, exported as DETECTOR_MAP.

Each language's mapping.py module constructs a single instance of this model and assigns it to the module-level DETECTOR_MAP constant, which DetectorRegistry.bootstrap_from_mappings reads during startup.

ATTRIBUTE DESCRIPTION
language

Language identifier matching language.

TYPE: str

bindings

Ordered list of detector bindings for this language.

TYPE: list[BaseBinding]

mcp_zen_of_languages.analyzers.mapping_models.FullDetectorMap

Bases: BaseModel

Aggregate view of detector bindings across every supported language.

Primarily useful for tooling and introspection; the registry itself iterates individual LanguageDetectorMap instances during bootstrap.

ATTRIBUTE DESCRIPTION
languages

Mapping from language identifier to its LanguageDetectorMap.

TYPE: dict[str, LanguageDetectorMap]