Skip to content

fix(contract): refuse a list pattern whose element is not a regex - #1649

Open
napetrov wants to merge 2 commits into
intel:mainfrom
napetrov:arch/r14-2
Open

napetrov wants to merge 2 commits into
intel:mainfrom
napetrov:arch/r14-2

Conversation

@napetrov

@napetrov napetrov commented Dec 5, 2026 •

Copy link
Copy Markdown
Contributor

Summary

_pattern(value)            dokimi/context/contract.py
  list of regexes
    before: "|".join("(?:p)" for p in list)     compile once, joined
    after:  for p in list: compile_user_regex(p)  # each element alone
            "|".join("(?:p)" ...)                 unchanged join
 def _pattern(value, where):
   if isinstance(value, list) and value:
-    return "|".join(f"(?:{p})" for p in _names(value, where))
+    parts = []
+    for p in _names(value, where):
+      compile_user_regex(p)   # ValueError: "<where>: element 'p': bad regex (...)"
+      parts.append(f"(?:{p})")
+    return "|".join(parts)

A list whose elements are each a regex compiles to the same pattern as before. A list in which one element refers to a
group of another (["(a)", "\1"]) is now refused: that element is not a regex on its own, and only the joined form compiled. The same holds for a list whose elements are each invalid alone but join to a valid pattern (["(a", "b)"]).

Evidence

  • Before: ["a)|(?:b"] accepted (joins to (?:a)|(?:b)); ["(a)", "\1"] accepted.
    After: both refused with bad regex, naming the element.
  • Red then green: test_each_element_of_a_list_pattern_must_be_a_valid_regex_on_its_own (5 cases) fails 4 on the parent
    commit (["(", "x"] was already refused by the joined compile) and passes on this branch.
  • Local: make ci-local --offline --jobs static,typecheck on the head: 0 failed, 17 passed, 3 skipped. Focused tests: 126 passed.
  • Self-review (local mode, fresh-context children, one pass on the final head): no blocking findings. The cross-element
    refusal is stated above and pinned by the tests. One nit, not applied: docs/run-consistency.md still says a list is
    "one alternation" without the per-element rule.

Merge Danger

Door: two-way. No persisted format, flag or public API changes; a contract that breaks can be fixed by editing the list.

Blast Radius: narrow. Only trace consistency --contract list-valued matcher keys. Affected: a contract whose list
element uses a backreference to a group in another element, now refused with exit 2 instead of accepted.

Not in this PR: ["(a)", "(b)\1"] is still accepted but matches differently from its element alone (group numbers shift
when joined). Pre-existing on main; a fix needs renumbering, so it is left for a decision.

🤖 Generated with Claude Code

@codecov-commenter

codecov-commenter commented Dec 5, 2026 •

Copy link
Copy Markdown

⚠️ Please install the 'codecov app svg image' to ensure uploads and comments are reliably processed by Codecov.

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 96.42%. Comparing base (19e5222) to head (6bbde72).
❗ Your organization needs to install the Codecov GitHub app to enable full functionality.

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #1649      +/-   ##
==========================================
- Coverage   96.44%   96.42%   -0.02%     
==========================================
  Files         585      585              
  Lines       76600    76607       +7     
  Branches    13175    13176       +1     
==========================================
- Hits        73876    73871       -5     
- Misses       1671     1676       +5     
- Partials     1053     1060       +7     
Files with missing lines Coverage Δ
dokimi/dokimi/context/contract.py 98.87% <100.00%> (+0.04%) ⬆️

... and 7 files with indirect coverage changes

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@napetrov
napetrov marked this pull request as ready for review December 6, 2026 06:06
@napetrov
napetrov requested a review from a team as a code owner December 6, 2026 06:06
Copilot AI balanced review requested due to automatic review settings December 6, 2026 06:06

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

The changelog incorrectly claims exit 2 and Error: for manifest regex failures in eval check and repo run, which report ManifestError with ✗ and exit 1.

1 open issue
What changed in this PR

1 new finding.

This PR validates each contract list-pattern element independently, building on #1646’s handling of regex compilation failures.

Changes:

  • Reject malformed list elements and cross-element backreferences with an element-specific error.
  • Normalize regex syntax, overflow, and recursion failures to ValueError.
  • Add regression tests, architecture registration, and changelog entries.
File Description
dokimi/​tests/​test_user_regex.py Tests compilation failures and branch-filter behavior.
dokimi/​tests/​test_repo_process_metrics.py Covers oversized and deeply nested regexes.
dokimi/​tests/​test_repo_manifest.py Tests manifest rejection of overflowing regexes.
dokimi/​tests/​test_import_architecture.py Registers the shared helper in the foundation layer.
dokimi/​tests/​test_context_consistency.py Tests invalid list elements and compilation failures.
dokimi/​dokimi/​user_regex.py Adds shared regex error normalization.
dokimi/​dokimi/​repos/​process_metrics.py Uses the shared compiler for counters.
dokimi/​dokimi/​repos/​l0/​branches.py Uses the shared compiler for glob atoms.
dokimi/​dokimi/​context/​contract.py Validates list elements before joining patterns.
dokimi/​changelog.d/​20261204_130000_napetrov_list-pattern-elements.md Documents list validation and compatibility changes.
dokimi/​changelog.d/​20261204_120000_napetrov_user-regex-refusal.md Documents normalized regex refusals.

Review effort: Balanced

@exai-bot

exai-bot commented Dec 6, 2026 •

Copy link
Copy Markdown

✅ OpenCodeReview: Review complete: 0 finding(s) across 4 selected item(s).

CI at 6bbde72: 21 passing, 4 skipped.

Review details
  • Commit reviewed: 6bbde72 against merge base 19e5222
  • Coverage: 4 files in 1 groups
  • Engine: OpenCodeReview v1.13.5-exai.4.8f73eada, us.openai.gpt-6.1-sol
  • Usage: 67k tokens (40k from cache), 10s
  • Tool calls: 8 (5 file_read, 1 ci_status, 1 code_search, 1 local_check_results)
  • Context contract pattern handling and consistency checks: 4 files

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 <nikolay.a.petrov@intel.com>
@napetrov napetrov changed the title [stacked on #1646] fix(contract): refuse a list pattern whose element is not a regex fix(contract): refuse a list pattern whose element is not a regex Dec 6, 2026
@github-actions

github-actions Bot commented Dec 6, 2026 •

Copy link
Copy Markdown

Mutation of changed lines

⚠️ Not a clean result: some breaks or tests were left out

At 6bbde72: 0 caught · 0 missed

This is not a clean result:

  • 4 changed lines were not mutated

Warning only: a missed break does not fail the job. Updated on every push; the full report is in the ci run summary.

Thread: PRRT_kwDOVBg7g87AQkB0
Signed-off-by: Nikolay Petrov <nikolay.a.petrov@intel.com>

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants