Skip to content

[IMPROVEMENT] Malformed patterns field aborts guardrail rule loading instead of degrading like other fields #4847

Description

@Beloved1310

Current state

Malformed field Behaviour
Invalid regex in patterns That pattern is skipped, rule still loads
Unrecognised action That rule is skipped, file still loads
keywords: null Normalised to an empty list, rule still loads
patterns: null Uncaught TypeError aborts loading of the entire guardrails.yml

The failure originates at platform/guardrails/rules.py#L93,
where raw.get("patterns", []) returns None because the default applies only to an
absent key, not to a present key with an empty value.

The blast radius is wider than one file failing to parse. Nothing guards the call:
the only try/except in _parse_rule is the except re.error around re.compile,
which is never entered because the exception fires while evaluating the iterable, and
load_rules's own handler wraps yaml.safe_load, which has already completed by
then. All three callers invoke load_rules() bare. Because the exception propagates
before _engine is assigned, get_guardrail_engine() raises again on every
subsequent call rather than failing once.

Desired state

Malformed field Behaviour
patterns: null The rule is skipped and the reason is logged
patterns set to a non-list type The rule is skipped and the reason is logged
Any other rule in the file Unaffected, loading continues

The obvious fix is raw.get("patterns") or [], matching the keywords handling two
lines below.

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions