Skip to content

docs(hooks): document public APIs and privatize internals ahead of ruff D and N - #133

Merged
furedea merged 1 commit into
mainfrom
docs/dn-c-hook-docstrings
Nov 10, 2026
Merged

furedea merged 1 commit into
mainfrom
docs/dn-c-hook-docstrings

Conversation

@furedea-agent

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

Copy link
Copy Markdown
Contributor

概要

#74 段階 1B のうち,ruff D(google)と N を有効化する前段として,lane dn-c の範囲(agents/hooks/*.py のうち guard_file.py と verification_session.py を除くもの,scripts/,agents/skills/,tests/ere_to_z3.py)の違反を設計から直す.pyproject.toml の ruff 設定は変えない(有効化は最後の PR).

対象:cleanup_worktrees.py,guard_command.py,guard_git.py,guard_suppression.py,scripts/replay_commands.py,tests/ere_to_z3.py,agents/skills/explain-visually/scripts/verify_page.py.

修正の内訳

種別 件数 内容
private 化 44 同一モジュール内でしか使われない関数・クラスの先頭に _ を付け,呼び出しを更新
docstring 追加 15 外から使われる API に,契約(戻り値,拒否する入力,副作用,失敗の仕方)を書く
N818 改名 2 UnsupportedPattern → UnsupportedPatternError,_VerificationFailure → _VerificationError
モジュール分割 1 lib/primary_workspaces.py を新設

private 化した名前と根拠

git grep で agents/,scripts/,tests/ を横断し,同一ファイル外から参照がないことを確認した.

  • cleanup_worktrees.py(12): primary_checkout,lane_primary,real_primary,git_file_target,is_merged,has_branch,local_branches,current_branch(テストは toplevel,linked_worktrees,worktree_is_clean,remove_worktree,sandbox_writable_paths,run,HERDR_WORKTREES だけを使う.これらは公開のまま docstring を付けた).残り 4 つ(recorded_primary_workspaces,primary_workspace_idle,herdr_items と idle_primary_workspaces の内部)は,分割で lib/primary_workspaces.py に移り private になった(下記).
  • guard_git.py(6): judged,segment_reason,hides_verdict,gh_hides_verdict,git_hides_verdict,session(テストは decide,RULE,audit_log だけを使う).
  • guard_suppression.py(5): Change,new_markers,changes,patch_changes,existing_text(テストは decide だけを使う).
  • guard_command.py(1): record(listing_rules は command_objections が使うので公開のまま docstring を付けた).
  • scripts/replay_commands.py(20): テストが使う Replayer,RecordedCommand と CLI の main,Judgment(Replayer.judge の戻り値)以外のすべて.

docstring を付けた公開 API

Worktree,main(cleanup / guard_command / guard_git / guard_suppression / replay_commands),toplevel,worktree_is_clean,linked_worktrees,run,Policy.listing_rules,guard_suppression.decide,RecordedCommand,Judgment.いずれも名前の言い換えではなく,失敗時の扱い(fail closed,終了コード)や,環境の遮断など名前から読めない契約を書いた.

名前の変更

  • UnsupportedPattern → UnsupportedPatternError: ERE を z3 に変換できないときの例外.使用箇所(tests/test_ere_to_z3.py,tests/test_rule_overlap.py)も更新.
  • _VerificationFailure → _VerificationError: verify_page.py が終了コードと一緒に投げる検証失敗.

新モジュール lib/primary_workspaces.py が必要だった理由

設計上の必要からではなく,file-length 上限(500 行)に当たったためである.docstring 追加で 528 行になったが,コメントや docstring を削らず,責務で分けた(「サイズ上限は設計の信号」).新モジュールが持つ責務は「エージェントが作った primary checkout の Herdr workspace のうち,誰も使っていないものを特定する読み取り専用の問い合わせ」である.これは session record と Herdr の live 状態だけを読み,副作用を持たない.worktree の掃除や workspace を閉じる処理は元のファイルに残る.docstring の作業が本来の目的なので,分割はこの 1 回に留め,他の整理はしていない.

cleanup_worktrees.py は docstring 追加で 500 行を超えた.コメントや docstring を削らず,責務で分けた.lib/primary_workspaces.py に「エージェントが作った primary の Herdr workspace のうち,誰も使っていないものを探す」問い合わせ(idle_primary_workspaces)を移した.cleanup_worktrees.py に残るのは,実際に閉じる側(run を使う副作用)である.テストは偽の herdr コマンドを使うので,移動で壊れない.

この新モジュールを deptry が first party と認識するよう,pyproject.toml の known_first_party に 1 行だけ追加した(ruff 設定は変更していない).最後の有効化 PR の pyproject.toml 変更とは別の箇所なので衝突しない.

確認

  • ruff check --extend-select D,N --ignore D104,D105,D107 と google convention,tests/** の D 除外で,lane の範囲(agents/hooks/lib,guard_file.py,verification_session.py を除く)が 0 件.
  • ruff check,ruff format --check,ty check: 通過.
  • pytest 全件:4531 passed, 1 skipped.
  • pre-commit(file-length,ruff,ty,commitlint): 通過.

計画からの変更:計画にない lib/primary_workspaces.py を file-length のために追加した.

Refs #74

🤖 Generated with Claude Code

…ff D and N

Make module-internal functions private and document the remaining public API.

Rename UnsupportedPattern and _VerificationFailure to the Error suffix.

Move idle workspace detection to lib/primary_workspaces.py (file length limit).

Refs #74

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

furedea commented Nov 10, 2026

Copy link
Copy Markdown
Owner

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown
Codex Review Summary: ❌ Failed

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

Review Status Commit Review trigger
📝 Code Review ❌ Failed 2026-11-10T13:54:22.094177Z
We couldn’t complete this review. Comment @codex review to try again.
d3075eb 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

Copy link
Copy Markdown

You have reached your Codex usage limits for code reviews. You can see your limits in the Codex usage dashboard.

@github-actions

Copy link
Copy Markdown

Review summary: no findings.

This PR is a behavior-preserving refactor (private renames, docstrings, N818 renames, and moving the idle-primary-workspace query to lib/primary_workspaces.py). What I checked:

  • Old names: no references to any renamed public name remain anywhere in agents/, scripts/ or tests/. That includes module-attribute access such as cleanup_worktrees.X and monkeypatch. Tests only use the names that stayed public (linked_worktrees, worktree_is_clean, run, toplevel, remove_worktree, sandbox_writable_paths, HERDR_WORKTREES, main).
  • Moved code: idle_primary_workspaces, _recorded_primary_workspaces, _is_idle and _herdr_items match the removed code line for line. The caller still catches OSError, SubprocessError, ValueError and TypeError, so the fail-closed behavior is unchanged.
  • Imports: cleanup_worktrees.py no longer imports herdr_ownership or session_store directly, and it no longer uses them anywhere.
  • Test selection: the new lib/primary_workspaces.py has no entry in related_test_extensions.json. By design, an unmapped hook module falls back to the full test suite, so the cleanup tests still run when it changes.
  • Error renames: UnsupportedPatternError and _VerificationError are updated at every raise and catch site.

Blocking findings: 0.

🤖 Generated with Claude Code

@furedea
furedea merged commit 0e744c2 into main Nov 10, 2026
22 of 24 checks passed
@furedea
furedea deleted the docs/dn-c-hook-docstrings branch November 10, 2026 14:25
furedea added a commit that referenced this pull request Nov 10, 2026
## 概要

ruff の D (pydocstyle, google),DOC,N (pep8-naming) を有効化する最終 PR (#74 stage
1B の持ち越し分).設定は template-python / dotfiles と同じ形にした.先行 lane (#130, #131,
#132, #133) の修正は既にマージ済みで,この PR は残りの違反を直したうえで設定を有効化する.

`ruff check .` は有効化後の設定で 0 件.ブランチは 1 コミットで,lefthook の全フックを除外なしで通過している.

Refs #74

## 設定 (pyproject.toml)

- `select` に `D`,`DOC`,`N` を `W` と `F` の間に追加 (template-python / dotfiles
と同じ並び).`DOC` による新規指摘は 0 件を確認済み
- `ignore` に `D104`,`D105`,`D107` を追加
- `per-file-ignores` の `tests/**` に `D` を追加
- `[tool.ruff.lint.pydocstyle] convention = "google"` を追加

既存の ignore は削除していない.

## この PR での修正

### 改名 (N818)

ドメイン概念はそのまま,`Error` 接尾辞のみ追加し,agents と tests の全使用箇所を更新した.

- `UnexpandableGlob` → `UnexpandableGlobError`
- `UnsupportedSyntax` → `UnsupportedSyntaxError`
- `UnplacedCommit` → `UnplacedCommitError`
- `UnknownTestCommand` → `UnknownTestCommandError`

### 非公開化 (外部から使われないことを `git grep` で agents/ scripts/ tests/ に対して確認)

| ファイル | 件数 | 名前 |
| ---------------- | ---- |
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------
|
| `guard_file.py` | 10 | `_policy_path`, `_load_policy`,
`_protected_reason`, `_validate_filename_rule`, `_commit_reason`,
`_content_match`, `_scan_content`, `_judged_commit`, `_context`,
`_session` |
| `shell_paths.py` | 1 | `_brace_words` |

### docstring を書いた公開 API

| ファイル | 件数 | 名前 | 理由 |
| ---------------- | ---- |
----------------------------------------------- |
------------------------------------------------------------------ |
| `guard_file.py` | 3 | `commit_filename_rules`, `content_text`, `main`
| テストと CLI から使われる公開 API |
| `shell_words.py` | 1 | `home_directory` |
`tests/hooks/test_shell_words.py` が直接呼ぶため非公開化しなかった |

docstring は名前の言い換えではなく,返す値・拒否する入力・例外を述べている.

## リポジトリ全体の内訳 (ac-dn の全 lane)

| PR | 非公開化 | docstring 追加 | 改名 (N818) |
| -------------------------------------------- | -------: |
-------------: | -----------------: |
| #130 (hooks/lib) | 38 | 23 | 0 (他に表記修正 2) |
| #131 (session_store ほか 4 ファイル) | 48 | 20 | 0 |
| #133 (hooks/\*.py, scripts, skills, tests) | 44 | 15 | 2 |
| #132 (verification_session, check_execution) | 8 | 7 | 0 |
| この PR | 11 | 4 | 4 |
| 合計 | 149 | 69 | 6 |

#130 と #133 の件数は各 PR 本文の表,#131 は本文の合計行,#132 は ac-dn の表 (非公開化 7 +
`collect`,文書化
`main`/`dispatch`/`failure`/`Status`/`Result`/`Result.failed`/`execute`)
による.

## 検証

- `ruff check .`: All checks passed
- `ruff format --check .`: 239 files already formatted
- `ty check`: All checks passed
- `pytest`: 手元 (sandbox) では 2 件を除き通過.失敗は
`tests/hooks/test_formatter_config.py` の dprint 統合テスト 2 件のみで,sandbox が
`plugins.dprint.dev` への通信を遮断するため (この PR の変更とは無関係).CI で確認する
- pre-commit (lefthook) は `LEFTHOOK_EXCLUDE` なしで全フック通過

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-authored-by: furedea <[email protected]>
Co-authored-by: Claude Sonnet 5.5 <[email protected]>
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