Skip to content

実装: staging guard で git add -A と Allowed Paths 外 commit 混入を防ぐ #963

Closed as not planned

Description

@squne121

Machine-Readable Contract

contract_schema_version: v1
issue_kind: implementation
parent_issue: "none"
goal_ref: "`git add -A` / `git add .` と Allowed Paths 外の staged diff 混入を PreToolUse hook で遮断し、無関係ファイルの巻き込み commit と amend 復旧コストを防ぐ"
change_kind: workflow

Parent Issue

none(単独改善 / mutation safety hardening の一部)

Parent Goal Ref

  • Goal: git add -A / git add . を常に block し、git commit 前に cached diff を contract Allowed Paths と照合して外部 path 混入を block する
  • Desired Destination: staging_guard が PreToolUse Bash hook として動作し、broad add と Allowed Paths 外 staging を fail-closed にする

Current Validated Scope

  • .claude/hooks/staging_guard.sh と .claude/hooks/staging_guard.py を新規追加する
  • .claude/settings.json の PreToolUse Bash matcher に guard を追加する
  • .claude/hooks/tests/test_staging_guard.py を新規追加する

Remaining Parent Gaps

  • worktree 外 mutation 全般の遮断は別 Issue(worktree scope guard)で扱う
  • PR body wrapper enforcement は別 Issue で扱う

Outcome

git add -A / git add . / git add --all が常に block され、git add <path> と git commit が contract Allowed Paths と照合され外部 path 混入時に block、Allowed Paths が解決できない場合は implementation worktree では block / 通常 read-only session では warn、read-only git は allow。新規 pytest と pnpm typecheck/lint/test/build が PASS する状態。

In Scope

  • git add -A / git add . / git add --all の常時 block
  • git commit 前の git diff --cached --name-only 取得と Allowed Paths 外検出
  • deleted tracked file の staged_deletion 明示
  • git add <path...> の path 単位 Allowed Paths 照合
  • contract snapshot 解決不能時の indeterminate(worktree=block / read-only=warn、read-only git は allow)
  • stderr の changed path 上限(最大 20 件)
  • settings.json への PreToolUse 追加と対応テスト

Out of Scope

  • worktree 外 Write/Edit 全般の遮断(別 Issue)
  • PR body 更新 wrapper 強制(別 Issue)
  • Allowed Paths 抽出ロジック自体の再実装(既存 extractor を利用)
  • Allowed Paths を .claude/hooks/** + settings + tests 以外へ拡張すること

Acceptance Criteria

  • AC1: git add -A / git add . / git add --all を常に block する。
  • AC2: git add allowed/path.py(Allowed Paths 内)は allow する。
  • AC3: git add unrelated/path.md(Allowed Paths 外)は block する。
  • AC4: git commit 時、cached diff に Allowed Paths 外が 1 件でもあれば block する。
  • AC5: Allowed Paths が取得できない場合、implementation worktree では block、通常 read-only session では warn のみとする。
  • AC6: hook の stderr は changed path を最大 20 件までに制限し、token bloat を避ける。
  • AC7: uv run pytest .claude/hooks/tests/test_staging_guard.py, pnpm typecheck, pnpm lint, pnpm test, pnpm build が PASS する。

Runtime Verification Applicability

decision: not_applicable
reason: "PreToolUse Bash hook の判定ロジックは pytest で stdin/stdout 契約として検証でき、外部 service 実行を成功条件に含まない"

Verification Commands

baseline では新規 hook / test が未作成のため、AC1〜AC6 の pytest は file not found(pytest exit 4)で baseline-fail する(baseline-expect: fail)。実装後に PASS する。AC7 は regression gate。

# AC1
# baseline-expect: fail
$ uv run pytest .claude/hooks/tests/test_staging_guard.py -k "block_add_all" -v
# AC2
# baseline-expect: fail
$ uv run pytest .claude/hooks/tests/test_staging_guard.py -k "allow_in_scope_path" -v
# AC3
# baseline-expect: fail
$ uv run pytest .claude/hooks/tests/test_staging_guard.py -k "block_out_of_scope_path" -v
# AC4
# baseline-expect: fail
$ uv run pytest .claude/hooks/tests/test_staging_guard.py -k "block_commit_with_out_of_scope" -v
# AC5
# baseline-expect: fail
$ uv run pytest .claude/hooks/tests/test_staging_guard.py -k "indeterminate_allowed_paths" -v
# AC6
# baseline-expect: fail
$ uv run pytest .claude/hooks/tests/test_staging_guard.py -k "stderr_bounded" -v
# AC7
$ pnpm typecheck
$ pnpm lint
$ pnpm test
$ pnpm build

Allowed Paths

  • .claude/hooks/staging_guard.sh
  • .claude/hooks/staging_guard.py
  • .claude/hooks/tests/test_staging_guard.py
  • .claude/settings.json

Stop Conditions

  • Allowed Paths 外の変更が必要と判明した場合
  • 既存 Allowed Paths extractor の再実装が必要になった場合
  • 既存 PreToolUse hook の挙動を変更する必要が出た場合
  • 新規 Issue の起票が必要と判断した場合(スコープ分割)
  • 外部サービス利用・権限昇格・既存テスト大規模改変が必要になった場合

Required Skills

  • Bash / Python 3(PreToolUse Bash hook の stdin/stdout 契約 / git コマンドパース)
  • contract Allowed Paths 抽出の理解

Scope Delta(任意)

N/A

Activity

  1. added
    phase/implementation
    agent/implementer
    enhancement
    on Jun 17, 2026
  2. squne121 commented on Jul 18, 2026

    @squne121
    OwnerAuthor

    既存 Issue #1611(実装: Issue scope snapshot と controlled executor で共通 staging/commit 境界を構築する)にスコープが supersede されました。

    本 Issue が扱っていた「raw git add -A と Allowed Paths 外 commit 混入を防ぐ」課題は、#1611 の controlled executor による raw add/commit deny 設計に統合されます。#1611 のマージ後、本 Issue が扱う課題は解消される見込みです。

    参照: #1611
    参照 parent: #1592
    SUPERSEDE_1611_MARKER_963

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    agent/implementerenhancementphase/implementation

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions