Skip to content

test(rules): list overlaps between allow prefixes and forbidden regexes with Z3 - #108

Merged
furedea merged 8 commits into
mainfrom
test/73-forbidden-overlap
Nov 9, 2026
Merged

furedea merged 8 commits into
mainfrom
test/73-forbidden-overlap

Conversation

@furedea-agent

@furedea-agent furedea-agent Bot commented Nov 9, 2026 •

Copy link
Copy Markdown
Contributor

概要

#73 の P2(allow prefix と禁止正規表現の重なりの列挙)を Z3 で追加する.Refs #73.#102 の引き継ぎ.#100 のマージで base ブランチが削除され,#102 が自動で close されたあと,force push 済みのため reopen できなかった.レビューの経緯(スレッドと返信)は #102 に残っている.#100 は main にマージ済みで,この PR の base は main.#104 がこの PR の上に積まれている.

  • tests/ere_to_z3.py: POSIX ERE から Z3 正規表現への変換器.規則が使う文法(^ $ . [] [:class:] () | * + ? {m,n} とエスケープ)だけを扱い,後方参照・先読み・\w などは UnsupportedPattern で拒否する.
  • tests/rule_overlap.py: allow prefix の語列(と,その後ろに空白と任意の文字列が続く形)と,禁止規則(例外を除く)の共通部分を Z3 で解き,witness を 1 行返す.
  • tests/test_forbidden_overlap.py: 現行規則の 20 件を「確認済み」として固定する.増えた重なりも,規則の削除で消えた重なりも赤になる.
変換器の検査 内容
文法ごとの例 受理 / 拒否する文字列 26 組
grep との差分 全 forbidden パターン × 大小文字の区別あり / なし で,ランダム文字列と,Z3 の witness を少し編集した文字列を grep -E と比較
範囲外の文法 後方参照,\w,括弧の不整合などを拒否
witness Z3 の witness を grep が受理する.充足不能な a^b などは witness なし

判断

  • 未アンカーのパターン((^|[[:space:]])git add)は,どの prefix の後ろの自由な tail にも現れるため,全 278 prefix と重なり 1405 件のノイズになった.マッチが prefix の語の範囲内から始まるものだけを数える定義に絞り,20 件になった.
  • agent-browser の file: など,引数に現れる禁止形は tail から始まるため P2 の対象外.これらは P4 の変形検査と既存テストで見る.
  • 20 件はいずれも「provider は prefix を許可し,hook が狭い形を deny する」意図どおりの構成で,修正は不要と判断した(git add と git add .,uv run ... mutmut apply など).自動修正はしない.
  • REVIEWED リスト(禁止形 7 種と,それに重なる allow prefix)は,2026-11-09 にユーザーが確認し,すべて意図どおりであることを確認済み.
  • .hypothesis/ を ls-lint の ignore に追加した(ローカルで Hypothesis を動かすと pre-commit の ls-lint が落ちるため.P4 のコミット時点では未生成で気づかなかった).

検証

  • 壊したルールの検査:allow prefix を広げる(git を追加),deny 規則を消すと,確認済みリストと一致せず赤になる 2 件

  • pytest 224 件 pass(関連テスト選択),ruff check / format,ty

  • 実行時間:20 件の列挙に約 5 秒

  • flake.nix:Linux の devShell で libstdc++ が見つからず libz3.so を読めなかったため,LD_LIBRARY_PATH を追加した(CI の test が落ちた原因).

  • 変換器は C ロケールの grep を模す.ロケール依存の構文(文字クラス,等価クラス,非 ASCII の大小畳み込みなど)は拒否する.[[:space:]] だけは ASCII の空白として扱う限界を docstring に記載した.

計画:#73 (comment)
未対応のレビュー指摘(xargs など wrapper の内側コマンドの重なり):#107

🤖 Generated with Claude Code

furedea and others added 8 commits November 9, 2026 18:45
…es with Z3

Add the P2 check of #73. A POSIX ERE to Z3 converter covers the dialect the
forbidden rules use and refuses anything else; it is compared with the system
grep on every forbidden pattern, with and without case folding, on random text
and on edited Z3 witnesses. The overlap finder names one line that an allow
prefix lets through and a forbidden rule would deny, skipping matches that
begin only in the free tail after the prefix words. The 20 overlaps found are
listed as reviewed; a new or vanished one fails the test.

Refs #73

Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
The z3-solver wheel links libstdc++, which a Nix Python on Linux does not find by
itself, so every test that imports z3 failed to load libz3 in CI. Point
LD_LIBRARY_PATH at the compiler runtime library in the dev shell on Linux only.

Refs #73

Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
Address review of the P2 check.

- Key each reviewed overlap by the complete forbidden pattern, so a new pattern
  that shares its first characters with a reviewed one no longer collapses into
  it.
- Move the line sentinels to the two highest code points so U+0001 and U+0002
  are ordinary text characters, as in a shell argument.
- Refuse a backslash inside a bracket expression, where the grep dialects
  differ, instead of widening the set.
- Refuse a prefix word that is empty or holds whitespace, which joining with
  spaces would merge into several arguments.
- Pin that a leading hyphen in a bracket expression is a literal.

Refs #73

Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
…e converter

Refuse the grep anchor escapes \< \> \` and \' instead of reading them as plain
characters, and refuse the alpha, alnum, upper, and lower classes, whose members
grow with a UTF-8 locale. The space and digit classes stay, read as ASCII.

Refs #73

Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
… converter

A cased non-ASCII letter folds by locale in grep, so a scoped rule with one is
refused instead of folded as ASCII only. State in the module docstring that the
converter models the C locale and where [[:space:]] differs under UTF-8.

Refs #73

Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
[=a=] and [.a.] depend on the locale and were read as ordinary bracket members,
which silently widened or shifted the set. Refuse them.

Refs #73

Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
grep reads each line of a pattern as a separate pattern, which the converter
would otherwise treat as one literal newline. Refuse it.

Refs #73

Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
main widened the forbidden patterns for abbreviated --no-verify and for python
options before -m. The reviewed overlaps keep the same prefixes against the same
rule families; only the pattern text in the reviewed list changes.

Refs #73

Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
@furedea

furedea commented Nov 9, 2026

Copy link
Copy Markdown
Owner

@codex review

@github-actions

github-actions Bot commented Nov 9, 2026

Copy link
Copy Markdown

レビュー結果

ブロッキングな指摘(P0/P1)はありません.インラインコメントもありません.

確認した点:

  • tests/ere_to_z3.py:番兵の扱い(. と否定ブラケットは TEXT_CHARACTERS に限定され,START/END は行頭・行末にしか現れない),ブラケットの先頭 ] や末尾 -,否定ブラケットでの大文字小文字の畳み込みの順序(畳み込んでから補集合),区間 {m} {m,} {m,n},範囲外の文法の拒否.いずれも grep の C ロケールの挙動と一致しています.
  • tests/rule_overlap.py:denied_from_within の Loop(any, 0, span) で,マッチの開始位置は START(^)から prefix の最後の文字までになり,説明どおり「prefix の語の範囲内から始まる」重なりだけを数えます.スコープ付きの規則(programs)を prefix[0] で絞り,大文字小文字を無視する点は command_policy.argument_reason と一致しています.
  • スコープ付きの規則では,guard がタブを削除してから照合しますが,モデルの tail はタブを含みます.この差で生じうるのは偽陽性だけで,重なりの見逃しにはなりません.また,結果は確認済みリストで固定されているため問題ありません.
  • z3-solver と hypothesis は main の pyproject.toml と uv.lock にすでに入っています.related_test_extensions.json の追加分も対応するテストがあります.

🤖 Generated with Claude Code

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 9, 2026 •

Copy link
Copy Markdown
Codex Review Summary: ✅ Completed

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-09T11:48:06.303408Z 1ce78b4 Manual request
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@chatgpt-codex-connector chatgpt-codex-connector Bot 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.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 1ce78b409f

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread tests/ere_to_z3.py
Comment on lines +146 to +148
shifted = [(low + CASE_OFFSET, high + CASE_OFFSET) for low, high in intersect(intervals, (UPPERCASE,))]
shifted += [(low - CASE_OFFSET, high - CASE_OFFSET) for low, high in intersect(intervals, (LOWERCASE,))]
return normalize(intervals + tuple(shifted))

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2 Badge Model UTF-8 case equivalents when folding ASCII

When the guard inherits a UTF-8 locale, grep's case-insensitive matching is broader than adding only the opposite ASCII case: for example, under the inspected C.UTF-8 environment, grep -qiE '^s$' accepts ſ, while this language rejects it. Consequently, a scoped forbidden rule containing s can overlap an allow prefix containing such a Unicode equivalent without find_overlaps reporting it. Either force the guard and tests to use the C locale or model/refuse these locale-dependent equivalents.

Useful? React with 👍 / 👎.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

This is a real gap in the converter, but it is out of scope for #108. The converter models grep in the C locale (stated in the module docstring), and the case-insensitive fold covers ASCII only. It is tracked in #107 with this thread linked; the fix is either to pin the guard and the tests to the C locale or to widen the model.

@furedea

furedea commented Nov 9, 2026

Copy link
Copy Markdown
Owner

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. 👍

Reviewed commit: 1ce78b409f

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

@furedea
furedea merged commit d3f706d into main Nov 9, 2026
41 of 45 checks passed
@furedea
furedea deleted the test/73-forbidden-overlap branch November 9, 2026 11:58
@furedea-agent
furedea-agent Bot restored the test/73-forbidden-overlap branch November 9, 2026 11:59
@furedea-agent
furedea-agent Bot deleted the test/73-forbidden-overlap branch November 9, 2026 11:59
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.

1 participant