Repository navigation
refactor(hooks): split guard_command.py by responsibility - #128
Conversation
|
@codex review |
Codex Review Summary: ❌ FailedThis comment shows the latest Codex review activity on this pull request.
ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings. |
|
You have reached your Codex usage limits for code reviews. You can see your limits in the Codex usage dashboard. |
レビュー結果指摘なし(P0/P1: 0 件).
🤖 Generated with Claude Code |
guard_command.py reached 499 of the 500-line file limit. Split it along its real responsibilities instead of trimming comments; every function, docstring, and comment moves verbatim and behavior is unchanged. - guard_command.py: entry point, composition, audit record, and the hook's own configuration (Policy, policies, ignored_project_rules) - lib/command_objections.py: every objection the guard can raise - lib/literal_words.py: predicates over shell text for literal_words command_objections declares the three Policy attributes it reads as a Protocol, so it never imports guard_command. Tests only change import paths. Refs #127 Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
3481f74 to
f7d07ba
Compare
|
@codex review |
|
You have reached your Codex usage limits for code reviews. You can see your limits in the Codex usage dashboard. |
|
Review summary: no findings (0 blocking) I compared the moved code line by line. Every function, constant, regex and docstring removed from
🤖 Generated with Claude Code |
) ## 概要 #74 段階 1B の PR 2.template-python が有効にしている小規模な ruff 規則を,agent-config でも有効にした(ignore から削除). | 削除した ignore | 違反数 | 対応 | | ---------------------------------------- | ------ | ------------------------------------------------- | | I001 | 152 | 自動修正(import の並び替え) | | E501 | 27 | docstring・コメント・文字列を折り返し,または短縮 | | PTH(104, 105, 109, 111, 115, 117, 119) | 12 | pathlib へ置換 | | PERF401 | 3 | `extend` とジェネレータ | | RUF005 | 3 | アンパック | | F541 | 1 | 自動修正 | | FURB162 | 1 | 不要な `replace("Z", ...)` を削除 | | PLW2901 | 1 | ループ変数の名前を分離 | 残した ignore(`E402`,`INP001`,`PLR2004`,`PLR0917`)のうち,理由を持つ `E402` と `PLR0917` は,テンプレートと dotfiles と同じく理由を 1 行上の `# CODE: reason` に書く形にそろえた(#124 の行末コメントから変更,末尾のピリオドなし). `noqa` と per-file-ignores は追加していない.D/N(PR 3),ARG001 と tests の PLR0917(c-ac1)はこの PR の範囲外. ## 削除した ignore の元の理由 dotfiles 8428059 で付いたコメントを,agent-config 59b76e7 がそのまま取り込んでいた.各理由が今も成り立つかを確認した.成り立つものはなく,ignore を戻す必要はない. | ignore | 元の理由(dotfiles 8428059) | 今は成り立たない理由 | | ------- | --------------------------------------------------------------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | | E501 | 埋め込み HTML/JS テンプレートの長い文字列を保つ | agent-config に埋め込みテンプレートはない.違反 27 件は docstring・コメント・テストの文字列で,意味を変えずに折り返せた | | I001 | `sys.path` を整える間,スクリプトの import ブロックを安定させる | ruff は `sys.path` の操作などのコードで import ブロックを分けて整列するので,path 設定の前後の順序は変わらない.hook の import は path 設定の後でのみ並び替わり,`ty` と全テストが通る | | PTH | 古いスクリプトが `os.path` を使う.`Path` への移行は別の作業 | その移行がこの PR.12 か所を置換し,未知の `~user` の挙動差は上の変換で扱った | | PERF401 | バッチ処理のコードは段階的な変更のほうが読みやすい | 違反 3 件は `verify_page` の同形のループで,1 つの `extend` にまとめたほうが読みやすい(バッチ処理のコードではない) | | RUF005 | パス再構築のコードはリスト連結のほうが明快 | 違反 3 件はタプルの連結とテストのパラメータで,パス再構築ではない.アンパックで同じ読みやすさ | | F541 | レポートや changelog の書き込みに無害な f 文字列がある | 該当は 1 件で,不要な `f` を外した | | FURB162 | タイムスタンプのパーサーが末尾の `Z` を明示的に正規化する | 該当はテストの `replace("Z", "+00:00")` のみ.対象は Python 3.15 で,`fromisoformat` が `Z` を直接読む(テストが通る) | | PLW2901 | JSONL パーサーが空白除去後に `line` を再利用する | 該当は 1 件で,JSONL パーサーではなくテストのループ変数 `block`.別名にして解消した | ## 設計上の判断(自明でない修正) - `os.path.expanduser(pattern)`(shell_paths): `Path` は末尾の `/` や `//` を正規化し,glob の意味が変わる.先頭セグメントだけ `Path(head).expanduser()` で展開し,残りはそのまま繋ぐ. - `home_directory`(shell_words): `expanduser("~")` が `"~"` を返す失敗判定をやめ,`Path.home()` の `RuntimeError` を `UnsupportedSyntax` に変換する.失敗を値の比較ではなく例外で表す. - `session_snapshot.link_signature`: `os.readlink` を `Path.readlink` にしたため,リンク文字列が正規化される(`./x` は `x`,`x/` は `x`,`a//b` は `a/b`).解決先は同じで,該当する綴りのリンクの署名が 1 度だけ変わる.`os.readlink` は PTH115 に該当し,生の文字列を保つには抑制が要るため,この正規化を承知のうえで受け入れた. - `verify_page`: 3 つの同形のループを,(出所の表示,リソース列)の対応に対する 1 つの `extend` にまとめた.メッセージは変わらない. - `test_skill_commands`: ループ変数を `fenced_block` とし,dedent 後を `block` とした(PLW2901). - `test_forbidden_command`: 長い文字列リテラルの重複を `COMPANION` / `HOME_COMPANION` 定数にまとめた. - 長い docstring は,元の文言を保ったまま,要約と補足に折り返した(`noqa` は使わない).`guard_command.py` の分割(#128)のあとに rebase し,`pinned_program_objection`(`command_objections.py`)と `has_literal_words`(`literal_words.py`)の元の文言を復元した.サイズ上限に合わせた短縮はしていない. - `Path.expanduser` / `Path.home` は,未知の `~user` で `RuntimeError` を出す(`os.path.expanduser` は文字のまま返す).この PR が新たに導入した箇所を次のように扱う. - `shell_paths.expanded_paths`: `UnexpandableGlob` に変換し,確認(ask)に回す. - `shell_words.home_directory`: `UnsupportedSyntax` に変換する. - `guard_file.home_expanded`: 未知の `~user` は文字のまま扱い,保護パスに一致させない. - `provider_adapter.Adapter.main`: 例外の一覧に `RuntimeError` を加え,hook が exit 1(非ブロック)で終わらず,ブロック(exit 2)になるようにした. - `hook_input.py`,`audit_log.py`,`session_store.py`,`trusted_owners.py` の `expanduser` は,この PR より前からある呼び出しで,今回の差分では変えていない. ## 確認 - `uv run ruff check` / `ruff format --check`: 通過 - `uv run ty check`: 通過 - `uv run pytest`: 4526 passed, 1 skipped(未知の `~user` の glob を paths hook に通す回帰テスト,`home_directory` と `shell_paths` の単体テストを含む) - lefthook の pre-commit(ruff, ty, file-length ほか): 通過 Refs #74 🤖 Generated with [Claude Code](https://claude.com/claude-code) --------- Co-authored-by: furedea <[email protected]> Co-authored-by: Claude Sonnet 5.5 <[email protected]>
概要
agents/hooks/guard_command.py(499 行,上限 500 行)を責務ごとに 3 つに分けた.動きは変えず,関数・docstring・コメントはそのまま移した(削っていない).guard_command.pymain/check/decide),組み立て,監査記録,このフックの設定(Policy,policies(),ignored_project_rules())lib/command_objections.pylib/literal_words.pyallows_literal_rules,has_literal_words,is_read_only_filterと正規表現)各ファイルは 500 行の上限に十分な余裕がある.
境界と循環の避け方
guard_command→command_objections→literal_words.command_objectionsが読むPolicyはprefixes,rules,listing_rulesの 3 属性だけなので,command_objections側にその 3 属性のProtocol(Policy)を宣言した.具体型guard_command.Policyはそれを構造的に満たす.こうすればcommand_objectionsはguard_commandを import せず(guard_commandはスクリプトとして実行されるため,逆向きの import は循環になる),関数のシグネチャも変わらない.Policyを別モジュールに出す案は,既存のlib/command_policyと紛らわしくなるので採らなかった.agents/hooks/lib/に置いた.hooks はディレクトリごと配布され,guard_command.pyがlibをsys.pathに入れ,各 provider アダプタもlibから import するため,既存のlibモジュールと同じ経路で全アダプタから読める.tests/test_python_runtime.pyの rendered 検証も通る.pyproject.tomlの deptryknown_first_partyにcommand_objectionsとliteral_wordsを追加した.テストの変更(import 経路のみ)
guard.allows_literal_rulesとguard.command_policyは,guard_commandの属性として使えなくなった(または使うべきでなくなった)ので,libのモジュールを直接読み込むようにした(test_allowed_command,test_command_policy,test_rule_overlap,test_ere_to_z3).補足
LITERAL_PROGRAMSの直前のコメントの 1 文目("Settings that can send a request ...")は,元からREQUEST_SETTINGSの説明のように読める位置にあった.純粋な移動にするため,そのままLITERAL_PROGRAMSの上に移した.直すなら別 PR で扱う.検証
ruff check/ruff format --check/ty check:通過lefthook pre-commit(file-length を含む):通過pytest全件(integration を含む):4521 passed, 1 skipped#125 は同じファイルを触るため,この PR の後に rebase される想定.差分は移動のみ.
Closes #127
🤖 Generated with Claude Code