Skip to content

fix(hooks): improve local main branch guard issue1198 - #1204

Merged
squne121 merged 5 commits into
mainfrom
worktree-issue-1198-local-main-guard
Jun 27, 2026
Merged

squne121 merged 5 commits into
mainfrom
worktree-issue-1198-local-main-guard

Conversation

@squne121

@squne121 squne121 commented Jun 27, 2026 •

Copy link
Copy Markdown
Owner

Summary\nこのPRはIssue 1198の実装です。\nCloses 1198

@github-actions

github-actions Bot commented Jun 27, 2026 •

Copy link
Copy Markdown
Contributor

Preview URL

✅ verified live assets: https://squne121.github.io/loop-protocol/pr-1204/

The preview will be removed when this PR is closed.

@squne121

Copy link
Copy Markdown
Owner Author

loop review probe

@squne121

Copy link
Copy Markdown
Owner Author

LOOP_VERDICT_V2

verdict: APPROVE
reviewed_head_sha: 55803da
merge_ready: false
mergeability:
mergeable: MERGEABLE
merge_state_status: BEHIND
blockers: []
required_auto_actions:

  • kind: update_branch
    executor: implementation-worker
    skill: implement-issue.update_branch
    blocking_merge_ready: true
    mechanical: true
    auto_fix_applied: []
    follow_up_issue_requests: []

@squne121

Copy link
Copy Markdown
Owner Author

LOOP_VERDICT_V2
verdict: APPROVE
merge_ready: true
mergeability: CLEAN
required_auto_actions: []
notes:

@squne121
squne121 marked this pull request as ready for review June 27, 2026 06:18
@squne121
squne121 merged commit 423d5ce into main Jun 27, 2026
17 of 18 checks passed
@squne121
squne121 deleted the worktree-issue-1198-local-main-guard branch June 27, 2026 06:19
@squne121

Copy link
Copy Markdown
Owner Author

post-merge-cleanup report draft

@squne121

Copy link
Copy Markdown
Owner Author

post-merge-cleanup: ok (status ok)

@squne121

Copy link
Copy Markdown
Owner Author

post-merge-cleanup: 完了 (2026-06-27T06:35:00Z)

  • status: ok
  • 次アクション: なし

@squne121

Copy link
Copy Markdown
Owner Author

POST_MERGE_CLEANUP_REPORT_V1: {status: ok, generated_at: "2026-06-27T06:35:00Z", generated_by: post-merge-cleanup-worker, human_review_required: false, cleaned_branches: [], cleaned_worktrees: [], unresolved_cleanup_items: [], parent_issue_status: {parent_issue_number: null, all_children_closed: true, recommended_action: n/a}, superseded_prs: [], follow_up_issue_requests: [], stash_restored: n/a, stash_entry_ref: null, warnings: [], errors: []}

@squne121

Copy link
Copy Markdown
Owner Author

you asked

message time: 2026-06-27 15:14:35

#1204
内容について関連Issue、PR、リポジトリ資産、コメントを参照した上で敵対的レビューして。
取り扱う技術や配布物の実装をWEB検索し高品質化してほしい。


chatgpt response

PR本体だけでなく、関連Issue/PR・差分・既存設計文書・CI/コメントを突き合わせます。実装が依存する技術仕様は公開情報で確認し、最後に「マージ阻害」「仕様破綻」「改善Issue候補」に分けてレビューします。

現在のPR head は bdfebc37... で、既存コメントの「全チェック通過」記録は f32f23c... 時点のため鮮度に注意が必要です。まず現headの差分とCIを基準に見ます。

差分上の主要な危険点は2つ見えています。1つは実hookのstderr diagnosticsがACの構造化診断に追いついていない点、もう1つは gh api の公式flag集合に対して parser のdeny条件が狭い点です。

判定: REQUEST_CHANGES

PR #1204 は現 head bdfebc37... で ci は成功に更新されています。build/typecheck/lint/e2e/python-test/node-backed-hook-tests など主要jobも成功しています。一方、PR自体はまだ draft で、Issue #1198 の契約、とくに hook failure diagnostics と gh api parser policy が未達です。CI green だけではマージ判断に足りません。

良い点

rtk を bypass allow せず、一段 unwrap して inner command を既存 classifier に流す方向は、ユーザー方針「rtk は許可。ただし包括するコマンドを監視」に沿っています。rtk proxy は block、rtk gh issue edit ... --repo squne121/loop-protocol --body-file tmp/... は既存の gh issue edit classifier に委譲されます。

gh api も blanket allow ではなく、repos/squne121/loop-protocol/issues/comments/<digits> の exact GET に限定する意図は妥当です。Issue #1198 の「GitHub 書き込み許可集合を拡張しない」という要求にも方向性は合っています。

ブロッカー

B1. AC7 の “hook failure diagnostics” は実hookでは満たしていない

evaluate() / CLI JSON には decision、parser_stage、command_kind、rule_id、argv_redacted などが入るようになっていますが、実際の run_hook() が block したときに呼ぶ _emit_block_stderr() は、従来通り reason_code、branch kind、recovery、hook_flavor 程度しか出していません。Issue #1198 は hook failure 時に hook_name / event_kind / decision / reason_code / rule_id / command_kind / parser_stage / argv_redacted を要求しています。現実の hook stderr に出ないなら、agent 側の診断改善としては未達です。

さらに、追加テストの多くは evaluate() を直接呼んでおり、raw hook JSON → run_hook() → stderr という実導線を検証していません。Stop no-command だけが実スクリプト実行です。PreToolUse / PermissionRequest の raw fixture も hook script 経由で検証してください。

修正要求: block stderr に bounded な構造化診断を出す。raw command 全文は出さず、例えば以下のキーだけを最大10行内に出す。

hook_name: local_main_branch_guard
event_kind: PermissionRequest
decision: block
reason_code: github_api_command
rule_id: gh_api_not_allowed
command_kind: github_api_command
parser_stage: github_api_block
argv_redacted: ["gh","api","--raw-field","<redacted>",...]

B2. gh api deny grammar が公式flag仕様に対して狭い

現在の _parse_gh_api_command() は -f, -F, --field, --input を「完全一致 token」としてだけ block し、--raw-field、--raw-field=...、--field=...、--input=...、-X POST を明示的には扱っていません。

GitHub CLI 公式仕様では、gh api は通常 GET ですが、parameter が追加されると POST になり、-f/--raw-field は payload parameter を追加します。-F/--field は @file や @- による file/stdin 読み込みを持ち、--input も request body を file/stdin から読みます。-X/--method はHTTP method指定です。(GitHub CLI) (GitHub CLI)

修正要求: 「偶然 block される」ではなく、gh api 専用 parser で以下を明示 deny してください。

--raw-field / --raw-field=...
-f / -f=...
--field / --field=...
-F / -F=...
--input / --input=...
-X POST|PATCH|PUT|DELETE
--method POST|PATCH|PUT|DELETE
graphql

追加fixture:

gh api --raw-field body=x repos/squne121/loop-protocol/issues/comments/4814545233
gh api --raw-field=body=x repos/squne121/loop-protocol/issues/comments/4814545233
gh api --field body=@tmp/x repos/squne121/loop-protocol/issues/comments/4814545233
gh api --input tmp/body.json repos/squne121/loop-protocol/issues/comments/4814545233
gh api -X POST repos/squne121/loop-protocol/issues/comments/4814545233
rtk gh api --raw-field body=x repos/squne121/loop-protocol/issues/comments/4814545233

B3. unknown_command_requires_review が allow されており、語彙が矛盾している

非 rtk の unknown command は最終的に status="allow" かつ reason_code="unknown_command_requires_review" で返ります。fixture でも echo https://...#issuecomment... が allow なのに reason は unknown_command_requires_review です。これは telemetry と policy の両方で危険です。「requires_review」なら block / not_applicable にすべきで、allow するなら unknown_non_branch_command などに分けるべきです。

Issue #1198 の意図は「unknown command を readonly と混同しない」ことであって、「requires_review と言いながら allow する」ことではありません。ここは reason vocabulary を再設計してください。

B4. docs が新 reason_code / classifier と同期していない

docs/dev/hook-boundaries.md の reason_code 一覧は、readonly_command / branch_safe_maintenance_command / deterministic_checker_command / github_remote_ops_command / gh_mutation_denied / unparseable_branch_mutation で止まっています。今回追加した unknown_command_requires_review、github_api_command、rtk_help_command、rtk_proxy_requires_review がありません。

コード側では新 reason と command kind が追加されています。docs の5分類表も gh issue/pr 中心で、gh api と rtk wrapper の扱いが反映されていません。

B5. schema compatibility test を subset 化しており、契約退行を隠す

.claude/hooks/tests/test_local_main_branch_guard.py 側で set(result.keys()) == expected_keys を expected_keys.issubset(set(result.keys())) に緩めています。これは「診断fieldの追加」を許すための変更ですが、LOCAL_MAIN_BRANCH_GUARD_RESULT_V1 の key drift を検知できなくなります。V1 schema を拡張するなら、期待key集合を正確に更新するか、明示的に LOCAL_MAIN_BRANCH_GUARD_RESULT_V2 へ上げるべきです。

関連Issue/PRとの整合

#1014 / PR #1041 は local root checkout を default branch から drift させない guard を導入し、fail-closed と hook順序を契約化した流れです。今回もその原則は維持すべきです。

#1108 は gh issue/pr ops を過剰blockしない修正ですが、同時に gh pr merge / gh pr checkout は引き続き block としています。PR #1118 も gh_mutation_denied を導入し、gh pr checkout などを block 継続する文脈です。今回の rtk gh ... unwrap はこの歴史と衝突しないよう、rtk gh pr checkout / rtk gh pr merge --delete-branch の block fixture を明示追加してください。

RTK 公式README上も、hook-based agents は git status を rtk git status に rewrite し、RTK は git add/commit/push/pull、gh pr/issue、curl/wget、rtk proxy <command> まで扱います。従って、RTK は「安全なコマンド」ではなく「監視対象の envelope」です。今回の unwrap 方針は正しいが、fixtureがまだ薄いです。(GitHub) (GitHub) (GitHub)

追加すべきfixture

# rtk 経由 branch mutation
rtk git switch issue-1198
rtk git checkout -b issue-1198
rtk git branch -M issue-1198
rtk gh pr checkout 1204
rtk gh pr merge 1204 --delete-branch

# gh api deny grammar
gh api --raw-field body=x repos/squne121/loop-protocol/issues/comments/4814545233
gh api --field body=@tmp/x repos/squne121/loop-protocol/issues/comments/4814545233
gh api --input tmp/body.json repos/squne121/loop-protocol/issues/comments/4814545233
gh api -X POST repos/squne121/loop-protocol/issues/comments/4814545233

# hook runtime diagnostics
PreToolUse raw JSON -> block stderr contains hook_name/event_kind/rule_id/command_kind/parser_stage
PermissionRequest raw JSON -> same

マージ条件

このPRは CI green でも、現状は draft維持 / REQUEST_CHANGES が妥当です。最低限、次を満たしてから ready にするべきです。

  1. run_hook() の block stderr に AC7 診断fieldを反映する。
  2. gh api parser を公式flag grammarに合わせ、deny fixtureを増やす。
  3. unknown_command_requires_review の allow 矛盾を解消する。
  4. docs/dev/hook-boundaries.md を新 reason_code / gh api / rtk wrapper まで更新する。
  5. schema test を exact keyset へ戻す、または schema version を上げる。
  6. raw fixture を evaluate() 直呼びだけでなく実hook script経由でも検証する。

you asked

message time: 2026-06-27 15:22:21

間違ってマージしてしまった。どうしたらいい?Revertして修正?AIにはなんて指示したらいい?追加修正Issue起票?


chatgpt response

まず現状態を確認します。誤マージ後の判断は「main上で危険が既に有効化されたか」「即時影響がhook運用だけか」「revert PRで安全に戻せるか」で分けます。

結論: Revert推奨です。理由は、PR #1204 は既に merged で main に入っており、対象が通常機能ではなく AI実行時の安全ガード だからです。gh api / rtk / hook診断まわりに未達がある状態で main に残すより、いったん revert して #1198 を reopen、修正版PRを作るのが安全です。PR #1204 は merged=true、merge commit は 423d5cec8955476257bff3b3b02e1ae7059a9fc4 です。

やる順番はこれです。

  1. main を force push / reset しない
  2. PR fix(hooks): improve local main branch guard issue1198 #1204 の revert PR を作る
  3. 実装: local hook の unparseable_branch_mutation 誤判定で issue-refinement-loop が停止する問題を解消する #1198 を reopen、または reopen できない運用なら「fix(hooks): improve local main branch guard issue1198 #1204 post-merge remediation」Issueを起票
  4. その後、未達ACだけを修正するPRを作る
  5. 修正版PRでは Closes #1198 か、新規Issueを作るなら Closes #新Issue にする

#1198 の本文は、gh api blanket allow禁止、rtk は bypass ではなく wrapper監視、hook failure diagnostics、raw fixture、true-positive/false-positive fixtureを要求しています。今回の未達は同じ #1198 スコープ内なので、原則は 新規Issueより #1198 reopen が筋です。

Revertするべき条件

今回はこれに該当します。

判断 推奨
main を Claude/Codex/rtk がすぐ参照する Revert
safety hook / guardrail の緩和・誤分類が疑われる Revert
CI green だが仕様AC未達 Revert or hotfix、今回はRevert寄り
実行中ユーザー影響がない普通のUI修正 hotfixでも可

git revert は破壊的な履歴改変ではなく、戻すコミットを追加するだけなので、GitHub運用上は安全です。reset / force push は不要です。

AIに投げる指示: Revert + #1198 reopen

このまま Claude Code / Codex CLI に渡してください。

**Standard Title: **

対象リポジトリ: https://github.com/squne121/loop-protocol

目的:
誤って merged した PR #1204 を安全に revert し、Issue #1198 を未完了として reopen または post-merge remediation Issue に接続する。main の履歴改変・force push は禁止。通常の revert commit + PR で戻す。

背景:
PR #1204 は fix(hooks): improve local main branch guard issue1198 で、Issue #1198 を Closes 1198 として merged された。しかしレビューで以下の未達が判明している。

  • AC7: 実 hook failure stderr に hook_name / event_kind / decision / reason_code / rule_id / command_kind / parser_stage / argv_redacted が出ていない
  • gh api parser が --raw-field=... / --field=... / --input=... / -X など公式 flag grammar に対して不十分
  • unknown_command_requires_review が status=allow と併用され、reason vocabulary が矛盾している
  • docs/dev/hook-boundaries.md が新 reason_code / gh api / rtk wrapper と同期していない
  • schema compatibility test が exact keyset から subset check に緩められ、契約退行を隠す
  • raw fixture の実 hook script 経由検証が不足している

実施手順:

  1. 最新 main を取得する。
  2. PR fix(hooks): improve local main branch guard issue1198 #1204 の merge commit 423d5cec8955476257bff3b3b02e1ae7059a9fc4 を確認する。
  3. merge commit が2親なら git revert -m 1 423d5cec8955476257bff3b3b02e1ae7059a9fc4、1親なら git revert 423d5cec8955476257bff3b3b02e1ae7059a9fc4 を使う。必ず親数を確認してから実行する。
  4. revert 用ブランチを作成し、root checkout を不用意に main 以外へ drift させない。既存の worktree 運用・guardrail に従う。
  5. revert PR を作成する。タイトルは revert: PR #1204 local_main_branch_guard incomplete remediation。
  6. PR本文に以下を明記する。
  7. 実装: local hook の unparseable_branch_mutation 誤判定で issue-refinement-loop が停止する問題を解消する #1198 が closed なら reopen する。reopen できない場合は、実装: local hook の unparseable_branch_mutation 誤判定で issue-refinement-loop が停止する問題を解消する #1198 / fix(hooks): improve local main branch guard issue1198 #1204 / revert PR を参照する追加修正Issueを起票する。
  8. CIを確認し、revert PRが green になるまで修正する。

禁止:

  • main への force push / reset
  • secret / token / credential の表示
  • GitHub API blanket allow の追加
  • gh issue edit / gh api の書き込み許可条件の緩和
  • root checkout を default branch 以外へ drift させる操作
  • 人間承認なしの破壊的操作

停止条件:

追加修正Issueを起票する場合の草案

#1198 を reopen できるなら、それが第一候補です。新規Issueを作るなら、タイトルと本文はこれでよいです。

**Document Title: **

fix(hooks): complete post-merge remediation for PR #1204 / Issue #1198

Background

PR #1204 was merged as an implementation of Issue #1198, but post-merge review found that several Issue #1198 acceptance criteria remain incomplete. Because this code changes local AI hook guardrails, incomplete classifier / diagnostics behavior should not be treated as completed.

Problem

The merged implementation improves parts of local_main_branch_guard, but leaves the following gaps:

  1. Runtime hook failure diagnostics are not emitted through the real hook stderr path.
  2. gh api parser does not explicitly deny the full mutation/file-read flag grammar such as --raw-field=..., --field=..., --input=..., and -X.
  3. unknown_command_requires_review is used with status=allow, creating policy/telemetry ambiguity.
  4. docs/dev/hook-boundaries.md is not synchronized with new reason codes and command kinds.
  5. schema compatibility tests were relaxed from exact keyset to subset matching.
  6. raw hook event fixtures are not sufficiently verified through the actual hook script path.
  7. rtk wrapper regression coverage is too thin for branch mutation and destructive GitHub operations.

Scope

Acceptance Criteria

  • Block stderr from the actual hook path includes bounded diagnostic fields:
    • hook_name
    • event_kind
    • decision
    • reason_code
    • rule_id
    • command_kind
    • parser_stage
    • argv_redacted
  • gh api exact GET issue-comment endpoint remains allowed.
  • gh api with --raw-field, --field, -f, -F, --input, -X POST|PATCH|PUT|DELETE, --method POST|PATCH|PUT|DELETE, or graphql is blocked or review-required with distinct diagnostics.
  • rtk gh api ... follows the same gh api classifier result as the unwrapped command.
  • rtk git switch, rtk git checkout -b, rtk git branch -M, rtk gh pr checkout, and rtk gh pr merge --delete-branch are blocked.
  • unknown_command_requires_review is not returned with status=allow; either block/review it or rename the allow case to a non-review reason code.
  • docs/dev/hook-boundaries.md lists all new reason codes and classifier categories.
  • schema compatibility tests assert the exact intended keyset or explicitly migrate to a new schema version.
  • PreToolUse / PermissionRequest / Stop raw fixtures are tested through the actual hook script path, not only evaluate() direct calls.
  • pnpm typecheck, pnpm lint, pnpm test, pnpm build, and relevant Python hook tests pass.

Verification Commands

pnpm typecheck
pnpm lint
pnpm test
pnpm build
uv run pytest tests/codex/test_local_main_branch_guard.py -q
uv run pytest .claude/hooks/tests/test_local_main_branch_guard.py -q
uv run python3 scripts/check_hook_boundaries.py
uv run python3 scripts/check_codex_agent_config.py --assert-local-main-branch-guard

References

その後の修正版AI指示

Revert後に改めて修正させるなら、次の短い指示で十分です。

**Standard Title: **

Issue #1198 を再実装してください。PR #1204 は未達ACがあるため revert 済み、または revert PR と並行して修正します。

必須:

  • 実装: local hook の unparseable_branch_mutation 誤判定で issue-refinement-loop が停止する問題を解消する #1198 のACを再確認し、未達だった AC7 / AC3 / AC10 / AC11 / AC12 / AC15 / AC16 を重点的に満たす
  • gh api は exact GET issue-comment endpoint のみ allow
  • --raw-field=... / --field=... / --input=... / -X / mutation method / graphql は明示 block
  • rtk は bypass allow しない。unwrap して inner command を既存 classifier に委譲する
  • actual hook script path で PreToolUse / PermissionRequest / Stop raw fixture を検証する
  • block stderr に bounded redacted diagnostics を出す
  • unknown command の allow/review/block vocabulary を矛盾なく整理する
  • docs/dev/hook-boundaries.md と tests を同期する
  • schema keyset を subset check で逃がさない

禁止:

  • GitHub remote write policy の拡張
  • gh api blanket allow
  • gh issue edit の --repo squne121/loop-protocol + --body-file tmp/... 条件緩和
  • local root default branch protection の緩和
  • force push / reset

完了条件:

  • PR本文に Closes #1198 または該当 follow-up Issue を記載
  • すべての関連CI green
  • レビュー指摘の各項目に対して、対応ファイル・テスト名・検証コマンドをPR本文に明記

最短で安全に進めるなら、まず revert PR、次に #1198 reopen、最後に 修正版PR です。

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