From 83e3029f6d6399c9c5b6cb946efdf6d2293163d9 Mon Sep 17 00:00:00 2001 From: Nikolay Petrov Date: Fri, 4 Dec 2026 23:13:40 -0800 Subject: [PATCH 1/2] fix(contract): refuse a list pattern whose element is not a regex A list-valued matcher key was joined with `|`, each element wrapped in `(?:...)`, and only the joined pattern was compiled. `["a)|(?:b"]` joins to two groups and was accepted, though the element is not a regex. Each element is now compiled on its own and refused by name. A list whose elements are each a regex compiles to the same pattern as before. A list where one element refers to a group of another is refused now, since that element is not a regex alone. Signed-off-by: Nikolay Petrov --- ...0261204_130000_napetrov_list-pattern-elements.md | 8 ++++++++ dokimi/dokimi/context/contract.py | 13 +++++++++++-- dokimi/tests/test_context_consistency.py | 11 +++++++++++ 3 files changed, 30 insertions(+), 2 deletions(-) create mode 100644 dokimi/changelog.d/20261204_130000_napetrov_list-pattern-elements.md diff --git a/dokimi/changelog.d/20261204_130000_napetrov_list-pattern-elements.md b/dokimi/changelog.d/20261204_130000_napetrov_list-pattern-elements.md new file mode 100644 index 000000000..2ef362bfc --- /dev/null +++ b/dokimi/changelog.d/20261204_130000_napetrov_list-pattern-elements.md @@ -0,0 +1,8 @@ + + +### Fixed + +- **A list of regexes in a `trace consistency --contract` action is checked one element at a time.** The list was joined with `|`, each element wrapped in `(?:...)`, and only the joined pattern was compiled, so `["a)|(?:b"]` was accepted (it joins to two groups) though `a)|(?:b` is not a regex. Each element is now compiled on its own, and a bad one is refused by name as `bad regex`. A list whose elements are each a regex compiles to the same pattern as before. The behaviour change: a list in which one element refers to a group of another (`["(a)", "\1"]`, `["(?Pa)", "(?P=x)"]`) is now refused, since that element is not a regex on its own; it was accepted before. The same holds for a list whose elements are each invalid alone but join to a valid pattern (`["(a", "b)"]`): it is now refused. diff --git a/dokimi/dokimi/context/contract.py b/dokimi/dokimi/context/contract.py index 0afde557c..f74e777e7 100644 --- a/dokimi/dokimi/context/contract.py +++ b/dokimi/dokimi/context/contract.py @@ -75,9 +75,18 @@ def _names(value: Any, where: str) -> tuple[str, ...]: def _pattern(value: Any, where: str) -> str: """A regex, or a list of them as one alternation; anything else (a number, a mapping) is refused, - so that `[pytest, tox]` is not read as the character class `['pytest', 'tox']`.""" + so that `[pytest, tox]` is not read as the character class `['pytest', 'tox']`. Each element must be a regex + on its own: joined, `a)|(?:b` would compile as two groups and pass. So a list element cannot refer to a group + of another element (`["(a)", "\\1"]`), though the joined pattern compiled before.""" if isinstance(value, list) and value: - return "|".join(f"(?:{p})" for p in _names(value, where)) + parts = [] + for p in _names(value, where): + try: + compile_user_regex(p) + except ValueError as exc: + raise ValueError(f"{where}: element {p!r}: bad regex ({exc})") from exc + parts.append(f"(?:{p})") + return "|".join(parts) if isinstance(value, str): return value raise ValueError(f"{where} wants a regex or a list of regexes, got {value!r}") diff --git a/dokimi/tests/test_context_consistency.py b/dokimi/tests/test_context_consistency.py index abf9d97e3..ae4b5380e 100644 --- a/dokimi/tests/test_context_consistency.py +++ b/dokimi/tests/test_context_consistency.py @@ -132,6 +132,17 @@ def test_a_pattern_that_is_not_a_regex_is_refused(value): parse_contract({"actions": {"test": {"tool": "Bash", "command": value}}, "rules": []}) +@pytest.mark.parametrize("value", [ + ["a)|(?:b"], [r"\bpytest\b", "a)|(?:b"], ["(", "x"], + ["(a)", "\\1"], ["(?Pa)", "(?P=x)"], # each joins to a pattern that compiled before; alone, neither element does +]) +def test_each_element_of_a_list_pattern_must_be_a_valid_regex_on_its_own(value): + """Joined with `|` and each wrapped in `(?:...)`, `a)|(?:b` compiles; it is not a regex on its own. The + backreference cases are refused on the same rule: `\\1` alone is not a regex.""" + with pytest.raises(ValueError, match="bad regex"): + parse_contract({"actions": {"test": {"tool": "Bash", "command": value}}, "rules": []}) + + @pytest.mark.parametrize("value", ["a{999999999999}", "(" * 2000 + ")" * 2000]) def test_a_regex_that_overflows_or_nests_too_deep_is_refused(value): """re.compile raises OverflowError or RecursionError for these; the contract refuses them as bad input.""" From 6bbde72beb1d2072218f7100bb17b2cc0692d193 Mon Sep 17 00:00:00 2001 From: Nikolay Petrov Date: Sat, 5 Dec 2026 22:19:44 -0800 Subject: [PATCH 2/2] docs: correct regex refusal exit codes Thread: PRRT_kwDOVBg7g87AQkB0 Signed-off-by: Nikolay Petrov --- .../changelog.d/20261204_120000_napetrov_user-regex-refusal.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/dokimi/changelog.d/20261204_120000_napetrov_user-regex-refusal.md b/dokimi/changelog.d/20261204_120000_napetrov_user-regex-refusal.md index 859cd2d50..66e8d7669 100644 --- a/dokimi/changelog.d/20261204_120000_napetrov_user-regex-refusal.md +++ b/dokimi/changelog.d/20261204_120000_napetrov_user-regex-refusal.md @@ -1,3 +1,3 @@ ### Fixed -- **A regex a user writes that Python cannot compile is refused, not a traceback.** `re.compile` raises `OverflowError` for a repeat count that does not fit (`a{999999999999}`) and `RecursionError` for groups nested too deep, not the `re.error` the `process_metrics` and contract loaders caught. Those patterns now refuse as bad input (exit 2, `Error: ...`) instead of crashing: a `process_metrics` counter in the repo manifest and a `trace consistency --contract` pattern. +- **A regex a user writes that Python cannot compile is refused, not a traceback.** `re.compile` raises `OverflowError` for a repeat count that does not fit (`a{999999999999}`) and `RecursionError` for groups nested too deep, not the `re.error` the `process_metrics` and contract loaders caught. A `trace consistency --contract` pattern that triggers either error is refused with exit 2 and `Error: ...`. If a `process_metrics` counter in a repo manifest triggers either error, `eval check` and `repo run` report a manifest error with `✗` and exit 1.