Skip to content

実装: VC baseline-fail 用 pytest command を canonical form に正規化して contract-review 往復を削減する #1285

Closed

Description

@squne121

Machine-Readable Contract

contract_schema_version: v1
issue_kind: implementation
parent_issue: "none"
goal_ref: "pytest baseline-fail VC を canonical form へ決定論的に正規化し、contract-review の VC 表現往復を削減する"
change_kind: code

Parent Issue

なし(単独改善)

Parent Goal Ref

  • Goal: Issue author が最初から canonical な baseline-fail VC を生成できる状態にする
  • Desired Destination: VC shape 起因の contract-review 往復ゼロ化

Current Validated Scope

  • .claude/skills/issue-contract-review/scripts/vc_baseline_shape_compiler.py(新規)で pytest baseline-fail command を canonical form に変換
  • .claude/skills/edit-issue/scripts/issue_contract_hygiene_autofix.py の autofix 対象に VC baseline shape を追加
  • .claude/skills/create-issue/references/body-authoring.md に canonical table を追記
  • compiler のテスト追加

Remaining Parent Gaps

  • reviewer_claim_replay の taxonomy parity は別 Issue で扱う
  • Issue metadata mutation の controlled executor 化は別 Issue で扱う

Runtime Verification Applicability

  • decision: not_applicable
  • reason: 静的検証(typecheck / lint / unit test / build)のみで完結し、プロセス起動・通信・I/O は不要(compiler script 追加、AST 静的解析、unit test 追加、docs 追記のみ)

Outcome

pytest baseline-fail VC の非 canonical 形(既存 test file への -k <未作成関数> 指定、既存 file への missing node-id 指定)が vc_baseline_shape_compiler.py により machine-readable な rewrite hint(未作成 test file の node-id 形式)へ決定論的に変換され、compiler テストが CI で PASS している状態。

Background

#1277 の contract-review では VC 表現だけで 3 回の修正往復が発生した。既存ファイルに対する -k <未作成関数> は pytest exit 5 で vc_no_tests_collected として hard block、既存ファイルへの missing node-id は判定不能、最終的に「まだ存在しない新規 test file の node-id」で expected baseline fail になった。baseline_vc_preflight.py は pytest exit 4 かつ stderr が file or directory not found に一致する場合のみ expected baseline fail とする。この保守的挙動は維持し、author 側が canonical form を最初から生成できるよう compiler を追加する。

Scope Clarifications

  • baseline_vc_preflight.py の pytest exit code contract は変更しない。
    • exit 4 + file or directory not found のみ expected baseline fail。
    • exit 5 no tests collected は vc_no_tests_collected hard block のまま。
  • 既存テスト test_pytest_exit5_no_tests_collected_expected_fail(exit 5 を expected_baseline_fail/go と期待)は現行 baseline_vc_preflight.py の実装(exit 5 を常に vc_no_tests_collected/blocked に分類)と矛盾しているため、本 Issue の実装スコープ内で現行契約に合わせて更新・削除する。
  • -k rewrite は単一 bare test_* identifier のみを対象にする。
    • boolean expression(and/or/not)/ class selector / parametrized selector / quoted complex expression は not_autofixable として rewrite しない。
  • existing file の missing node-id 判定は pytest 実行に依存せず、ast.parse() による静的解析(top-level def test_* / class Test*)に限定する。class/method や parametrization は not_autofixable とする。
  • suggested missing-file node-id は、baseline で存在しない・Allowed Paths 内・*.py::test_* 形式の場合のみ生成する。安全な候補を作れない場合は rewrite しない。既存候補と衝突する場合は deterministic suffix を付与する。

In Scope

  • vc_baseline_shape_compiler.py 新規追加。禁止形(existing_test_file + -k 新規テスト名 / existing_test_file::missing_test_function)を検出し、canonical 形(missing_new_test_file.py::test_name)の suggested_command を machine-readable JSON で返す
  • issue_contract_hygiene_autofix.py の autofix 対象へ VC baseline shape 変換を追加
  • body-authoring.md に短い canonical table(禁止形 / 推奨形 / 理由)を追記
  • compiler の unit テスト追加(既存 CI target set である .github/ci/python-test-plan.json に既に含まれるディレクトリ配下に配置するため追加登録は不要)

Out of Scope

  • baseline_vc_preflight.py の exit code 判定契約の変更(exit 5 hard block は維持)
  • issue-author SubAgent 定義自体の書き換え
  • pytest 以外の VC 種別(rg / diff 等)の正規化

Acceptance Criteria

  • AC1: existing_test_file.py -k test_new_name は、test_new_name が単一 bare identifier の場合のみ、Allowed Paths 内の missing-file node-id への rewrite hint(suggested_command)を返す
  • AC2: existing_test_file.py::test_missing_name は、AST 静的解析で missing と確認できる単純 function node-id の場合のみ canonical missing-file node-id への rewrite hint を返す
  • AC3: complex -k expression(boolean / class selector / parametrized selector)または Allowed Paths 外候補の場合は not_autofixable を返し、body を変更しない
  • AC4: missing_new_test_file.py::test_name は already_canonical と判定され、hygiene autofix は no-op になる
  • AC5: issue_contract_hygiene_autofix.py 経由で VC shape rewrite が適用され、2 回目実行は sha256 guard により no_change になる
  • AC6: body-authoring.md に marker pytest_baseline_fail_canonical_rules を持つ canonical table が追加される
  • AC7: exit 5 を expected fail と見なす stale test(test_pytest_exit5_no_tests_collected_expected_fail)が更新・削除され、baseline_vc_preflight.py の exit 5 hard block 契約が regression しない

Verification Commands

# AC1
$ uv run --locked pytest .claude/skills/issue-contract-review/tests/test_pytest_vc_baseline_shape_compiler.py::test_ac1_dash_k_new_test_name_rewritten -q
# AC2
$ uv run --locked pytest .claude/skills/issue-contract-review/tests/test_pytest_vc_baseline_shape_compiler.py::test_ac2_missing_node_id_on_existing_file_rewritten -q
# AC3
$ uv run --locked pytest .claude/skills/issue-contract-review/tests/test_pytest_vc_baseline_shape_compiler.py::test_ac3_not_autofixable_complex_expression -q
# AC4
$ uv run --locked pytest .claude/skills/issue-contract-review/tests/test_pytest_vc_baseline_shape_compiler.py::test_ac4_missing_file_node_id_already_canonical -q
# AC5
$ uv run --locked pytest .claude/skills/issue-contract-review/tests/test_pytest_vc_baseline_shape_compiler.py::test_ac5_hygiene_autofix_applies_vc_shape_then_no_change -q
# AC6
$ rg -c "pytest_baseline_fail_canonical_rules" .claude/skills/create-issue/references/body-authoring.md
# 補足: 本 Issue 実装後は旧テスト名が更新/削除される想定。実装前は現行ファイルへの参照として baseline 実行する
# AC7
$ uv run --locked pytest .claude/skills/issue-contract-review/scripts/tests/test_baseline_vc_preflight.py -k test_pytest_exit5_no_tests_collected_expected_fail -q

Allowed Paths

  • .claude/skills/issue-contract-review/scripts/vc_baseline_shape_compiler.py
  • .claude/skills/issue-contract-review/tests/
  • .claude/skills/issue-contract-review/scripts/tests/test_baseline_vc_preflight.py
  • .claude/skills/edit-issue/scripts/issue_contract_hygiene_autofix.py
  • .claude/skills/edit-issue/tests/
  • .claude/skills/create-issue/references/body-authoring.md
  • .claude/skills/issue-contract-review/scripts/tests/fixtures/pytest_exit5_no_tests.md

Stop Conditions

  • Allowed Paths 外の変更が必要と判明した場合
  • In Scope の固定契約(キー集合・スキーマ・型定義)の変更が必要になった場合
  • 新規 Issue の起票が必要と判断した場合(スコープ分割が発生する場合)
  • 後続 Phase / 別スコープへの波及が判明した場合
  • nested SubAgent delegation が必要になった場合
  • 外部サービス利用・権限昇格・既存テスト大規模改変が必要になった場合

Required Skills

なし

Scope Delta(任意)

  • 追加日: 2026-07-03
  • 追加パス: .claude/skills/issue-contract-review/scripts/tests/fixtures/pytest_exit5_no_tests.md
  • 理由: AC7 で stale test test_pytest_exit5_no_tests_collected_expected_fail を更新・削除する際、その専用フィクスチャ(main に feat: scope rollup result を self-validating 化し verify を 1 コマンドに集約する #850 由来で既存)も不要になり削除が必要だったため。Scope Clarifications で既に許可されている stale test 更新に直接付随する削除であり、Allowed Paths への列挙漏れを補うもの。

Notes for Reviewer

  • compiler は advisory でなく hygiene autofix に配線するところまでを本 Issue の完了条件とする
  • baseline_vc_preflight.py の既存判定(exit 4 + not-found stderr のみ expected_baseline_fail)は変更しない
  • Allowed Paths から scripts/python-test-plan.json を削除した。既存 CI target SSOT である .github/ci/python-test-plan.json には .claude/skills/issue-contract-review/tests/、.claude/skills/issue-contract-review/scripts/tests/、.claude/skills/edit-issue/tests/test_issue_contract_hygiene_autofix.py が既に含まれており、追加登録は不要である。

compiler output schema(固定):

{
  "schema": "vc_baseline_shape_compiler/v1",
  "status": "changed | already_canonical | not_autofixable | invalid_input",
  "rewrites": [
    {
      "ac": "AC1",
      "line_number": 12,
      "reason_code": "pytest_dash_k_new_test_on_existing_file",
      "original_command": "...",
      "suggested_command": "...",
      "confidence": "high"
    }
  ],
  "errors": [],
  "warnings": []
}

Activity

  1. added
    enhancement
    phase/implementation
    triage-required
    agent/implementer
    on Jul 2, 2026
  2. squne121 commented on Jul 3, 2026

    @squne121
    OwnerAuthor

    判定: REQUEST_CHANGES

    #1285 の方向性は正しいです。pytest baseline-fail VC の表現ゆれを author 側で canonical 化し、contract-review の往復を減らす狙いは妥当です。Issue 本文も、baseline_vc_preflight.py の保守的契約を変えず、author 側に compiler を追加する方針を明示しています。コメントは現時点で 0 件でした。

    ただし、この Issue のまま実装に流すと、Allowed Paths / CI 登録 / pytest selector 解釈 / hygiene autofix 境界で破綻する可能性が高いです。実装前に Issue 本文を修正すべきです。

    Blocking findings

    1. scripts/python-test-plan.json は危険な誤パス

    #1285 は scripts/python-test-plan.json の登録を要求していますが、実在する python-test plan の SSOT は .github/ci/python-test-plan.json です。このファイル自身も「python-test job の pytest target set の SSOT。変更時はこのファイルを編集」と説明しています。

    さらに、その SSOT にはすでに .claude/skills/issue-contract-review/tests/、.claude/skills/issue-contract-review/scripts/tests/、.claude/skills/edit-issue/tests/test_issue_contract_hygiene_autofix.py が含まれています。新規テストをこの既存ディレクトリ配下に置くなら、追加登録は不要な可能性が高いです。

    修正: Issue から scripts/python-test-plan.json を削除する。CI plan の変更が本当に必要なら、Allowed Paths に .github/ci/python-test-plan.json を追加し、なぜ既存 target では足りないのかを明記する。

    2. pytest exit code 契約は正しいが、Issue は stale test との矛盾を潰していない

    pytest 公式では exit code 4 は command line usage error、exit code 5 は no tests collected です。pytest は -k に keyword expression を使い、node-id は file.py::test_func のような形式を取ります。([pytestドキュメント]1) ([pytestドキュメント]2)

    現行 baseline_vc_preflight.py は、pytest exit 4 かつ file or directory not found だけを expected_baseline_fail/go にし、pytest exit 5 は vc_no_tests_collected/blocked にしています。これは #1285 の背景説明と一致します。

    一方で、既存テスト側には test_pytest_exit5_no_tests_collected_expected_fail という、exit 5 を expected_baseline_fail/go と期待するテストが残って見えます。これは現行 classifier と矛盾します。

    修正: #1285 に「exit5 expected_fail を期待する stale test が残っていれば更新・削除する」を明記する。ここを放置すると、実装者が古いテストに合わせて baseline_vc_preflight.py を逆戻りさせるリスクがあります。

    3. -k <未作成関数> の rewrite 条件が粗すぎる

    pytest の -k は単一関数名ではなく keyword expression です。-k "test_new and not slow"、-k "foo or bar"、-k "ClassName and method" のような表現を compiler が勝手に node-id 化すると、意味を壊します。pytest 公式も -k を “keyword expression” として説明しています。([pytestドキュメント]2)

    修正: compiler の対象を strict subset に限定する。

    • 許可: -k test_<identifier> の単一 bare identifier のみ
    • 拒否: and / or / not / parentheses / quotes / wildcard 風文字列 / class 名 / parameterized selector
    • 拒否時: status=not_autofixable, reason_code=complex_k_expression

    4. existing file の missing node-id 判定は、pytest 実行でなく静的解析に寄せるべき

    #1285 は「existing file への missing node-id 指定を rewrite」としていますが、ここを pytest 実行で判定すると、テスト import の副作用、依存欠落、環境差で compiler が不安定になります。現行 preflight は shlex.split で argv 化し shell=False で実行しているため、安全境界を強く意識しています。

    修正: vc_baseline_shape_compiler.py は原則として実行系にしない。既存 .py file は ast.parse() で top-level def test_* と class Test* を静的に読む。今回の AC2 はまず existing_file.py::test_missing_func の単純形だけに限定し、class/method や parametrization は not_autofixable にする。

    5. canonical missing-file path の生成規則が未定義

    「未作成 test file の node-id 形式へ rewrite」とありますが、どの missing file を作るのかが未定義です。compiler が勝手に tests/test_foo.py を作る提案を出すと、Allowed Paths 外・既存ファイル衝突・命名不整合を起こします。

    修正: suggested path の規則を明示する。

    推奨規則:

    existing_file = .claude/skills/<skill>/tests/test_existing.py
    test_name     = test_new_behavior
    
    suggested missing file:
    .claude/skills/<skill>/tests/test_new_behavior_contract.py::test_new_behavior
    

    ただし、以下を必須条件にする。

    • suggested file は baseline で存在しない
    • suggested file は Allowed Paths 内
    • suggested node-id は *.py::test_*
    • 既存候補と衝突する場合は test_<slug>_<8char_command_hash>_contract.py::test_<name> のように deterministic suffix を使う
    • Allowed Paths 内に安全な missing file を作れない場合は rewrite しない

    6. hygiene autofix は現状 C4/C9 専用で、VC shape 修正を入れる境界が足りない

    issue_contract_hygiene_autofix.py は現状、C4 $ prefix と C9 Runtime Verification Applicability の deterministic autofix だけを扱う設計です。exit code も repaired / no_change / not_autofixable の三値に近い契約です。

    main flow も、non-C4/C9 blocker check → C4 repair → C9 repair → sha256 guard という順序です。ここに VC shape compiler を雑に差し込むと、非 C4/C9 blocker の文字列 filter と干渉します。

    修正: VC shape 修正は独立ステップにする。

    推奨順序:

    1. check_non_c4_c9_blockers
    2. repair_c4_in_vc_block
    3. repair_pytest_baseline_vc_shape
    4. repair_c9
    5. sha256 no_change guard
    

    追加すべき hygiene tests:

    • C4 + VC shape + C9 の combined repair
    • VC shape のみ repaired
    • already canonical は exit 1 no_change
    • complex -k は exit 2 not_autofixable
    • Allowed Paths 外 suggested file は exit 2
    • body output は raw stderr diagnostics と混ざらない
    • 2 回目実行で idempotent

    7. machine-readable JSON schema が Issue にない

    #1285 は “machine-readable JSON” と書いていますが、schema がありません。これでは downstream が suggested_command だけ拾う実装になり、複数 VC・部分 rewrite・非 autofixable 理由・source span を扱えません。

    修正: compiler output を固定する。

    {
      "schema": "vc_baseline_shape_compiler/v1",
      "status": "changed | already_canonical | not_autofixable | invalid_input",
      "rewrites": [
        {
          "ac": "AC1",
          "line_number": 12,
          "reason_code": "pytest_dash_k_new_test_on_existing_file",
          "original_command": "uv run --locked pytest existing.py -k test_new -q",
          "suggested_command": "uv run --locked pytest missing_contract_file.py::test_new -q",
          "confidence": "high"
        }
      ],
      "errors": [],
      "warnings": []
    }

    関連 Issue / PR からの品質基準

    #1277 → PR #1278 の流れは、この種の workflow 修正の品質基準として有用です。PR #1278 は producer 契約を壊さず builder 境界で projection し、投影不能要素を fail-closed にし、end-to-end schema validation と fail-closed tests を追加しています。

    #1285 も同じ基準に揃えるべきです。つまり「preflight の exit code 契約を変えない」「author/hygiene 側で deterministic projection する」「不明な selector は silent rewrite しない」「compiler 単体テストだけでなく hygiene 経由の body rewrite test を持つ」が最低ラインです。

    Issue 本文への修正案

    #1285 は以下を追記・修正してください。

    ## Scope Clarifications
    
    - `baseline_vc_preflight.py` の pytest exit code contract は変更しない。
      - exit 4 + `file or directory not found` のみ expected baseline fail。
      - exit 5 `no tests collected` は `vc_no_tests_collected` hard block のまま。
    - `-k` rewrite は単一 bare `test_*` identifier のみ対象にする。
      - boolean expression / class selector / parametrized selector / quoted complex expression は `not_autofixable`。
    - existing file の missing node-id 判定は pytest 実行に依存せず、AST ベースの静的判定に限定する。
    - suggested missing-file node-id は Allowed Paths 内かつ baseline で file missing の場合だけ生成する。
    - `scripts/python-test-plan.json` は存在しないため Allowed Paths から削除する。
      必要な場合のみ `.github/ci/python-test-plan.json` を明示する。

    改訂 Acceptance Criteria

    - [ ] AC1: `existing_test_file.py -k test_new_name` は、`test_new_name` が単一 bare identifier の場合のみ、Allowed Paths 内の missing-file node-id `.../test_<slug>_contract.py::test_new_name` へ rewrite hint を返す。
    - [ ] AC2: `existing_test_file.py::test_missing_name` は、AST 静的解析で missing と確認できる単純 function node-id の場合のみ canonical missing-file node-id へ rewrite hint を返す。
    - [ ] AC3: complex `-k` expression / class method selector / parametrized selector / Allowed Paths 外候補は `not_autofixable` とし、body を変更しない。
    - [ ] AC4: `missing_new_test_file.py::test_name` は `already_canonical` と判定され、hygiene autofix は no-op になる。
    - [ ] AC5: `issue_contract_hygiene_autofix.py` 経由で VC shape rewrite が適用され、2 回目実行は sha256 guard により `no_change` になる。
    - [ ] AC6: `body-authoring.md` に marker `pytest_baseline_fail_canonical_rules` を持つ canonical table と、`vc_no_tests_collected` repair contract が追加される。
    - [ ] AC7: exit 5 を expected fail と見なす stale test が存在する場合は更新・削除され、`baseline_vc_preflight.py` の exit 5 hard block 契約が regression しない。

    AI エージェント向け修正プロンプト

    GitHub Issue #1285 を実装前に精査し、Issue body の契約を破らずに実装してください。
    
    最優先の制約:
    - baseline_vc_preflight.py の pytest exit code 判定は変更禁止。
      - pytest exit 4 + `file or directory not found` のみ expected_baseline_fail/go。
      - pytest exit 5 は vc_no_tests_collected/blocked のまま。
    - `scripts/python-test-plan.json` は存在しない。CI plan 変更が必要かを確認し、不要なら Issue body/実装から除外する。必要なら `.github/ci/python-test-plan.json` を Allowed Paths に追加する提案で停止する。
    - compiler は pytest 実行に依存しない。既存 test file の node-id 欠落判定は AST 静的解析に限定する。
    - `-k` rewrite は単一 bare `test_*` identifier のみ対象。boolean expression、class selector、parametrized selector、quoted complex expression は not_autofixable。
    - suggested missing-file node-id は Allowed Paths 内かつ baseline で file missing の場合のみ生成する。候補が作れなければ body を変更しない。
    
    実装対象:
    - `.claude/skills/issue-contract-review/scripts/vc_baseline_shape_compiler.py`
    - `.claude/skills/issue-contract-review/tests/test_pytest_vc_baseline_shape_compiler.py`
    - `.claude/skills/edit-issue/scripts/issue_contract_hygiene_autofix.py`
    - `.claude/skills/edit-issue/tests/test_issue_contract_hygiene_autofix.py`
    - `.claude/skills/create-issue/references/body-authoring.md`
    
    実装要件:
    1. compiler output schema `vc_baseline_shape_compiler/v1` を固定する。
    2. `changed | already_canonical | not_autofixable | invalid_input` を返す。
    3. hygiene autofix は C4 repair 後、C9 repair 前に VC shape repair を適用する。
    4. no-op / not_autofixable / combined C4+VC+C9 / idempotency / Allowed Paths 外候補をテストする。
    5. body-authoring.md には marker `pytest_baseline_fail_canonical_rules` と `vc_no_tests_collected` 修復方針を追加する。
    6. 既存の exit 5 expected_fail テストが残っていれば、exit 5 hard block 契約に合わせて修正する。
    
    破壊的操作、秘密情報、外部課金、権限不足、Allowed Paths 外変更が必要な場合だけ停止してください。
    

    この Issue は「実装してよいが、Issue 本文を先に直すべき」です。特に python-test-plan パス誤りと exit5 stale test 矛盾は、後で PR レビュー往復を増やす直接原因になります。

  3. squne121 commented on Jul 3, 2026

    @squne121
    OwnerAuthor

    Refinement Loop: Approved

    The issue has been approved and is ready for implementation.

  4. squne121 commented on Jul 3, 2026

    @squne121
    OwnerAuthor

    Contract Review Result

    CONTRACT_REVIEW_RESULT_V1:
      status: go
      generated_at: "2026-07-03T15:30:47Z"
      generated_by: issue-contract-review
      issue_url: https://github.com/squne121/loop-protocol/issues/1285
      body_sha256: "sha256:48d085b4a3c94ed960e2a0daa5ed0ecbbe0b7b1444e2dbdaa6dd37a8755555c7"
      checks:
        readiness: go
        blockers: pass
        product_spec: pass
        vc_preflight: pass
      source: ensure_contract_snapshot_auto
  5. squne121 commented on Jul 3, 2026

    @squne121
    OwnerAuthor

    implement-issue: 実装完了 (2026-07-04)

  6. squne121 commented on Jul 3, 2026

    @squne121
    OwnerAuthor

    Contract Review Result (re-materialized after Scope Delta)

    CONTRACT_REVIEW_RESULT_V1:
      status: go
      generated_at: "2026-07-03T16:50:19Z"
      generated_by: issue-contract-review
      issue_url: https://github.com/squne121/loop-protocol/issues/1285
      body_sha256: "sha256:63b36dff96f7cb8b2ae8752be6f2e36fad98acc8b07c8f29feb1192fa7b07e7a"
      checks:
        readiness: go
        blockers: pass
        product_spec: pass
        vc_preflight: pass
      source: ensure_contract_snapshot_auto_post_scope_delta
  7. added 2 commits that reference this issue on Jul 3, 2026
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/implementationtriage-required

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions