Skip to content

実装: issue-refinement-loop producer の fail-closed routing を固定する #1165

Closed

Description

@squne121

Machine-Readable Contract

contract_schema_version: v1
issue_kind: implementation
parent_issue: "#1154"
goal_ref: "agent-skill-runtime-hook-relaxation"
change_kind: workflow

Parent Issue

#1154

Parent Goal Ref

  • Goal: AI agent の skill 実行経路を、security hook を無効化せずに通せるようにする。
  • Desired Destination: issue-refinement-loop の producer / publisher 契約が、root allow 設計とは独立に fail-closed で動作し、schema mismatch や output budget 違反時に termination report 直接投稿へ逸脱しない状態。

Current Validated Scope

  • .claude/skills/issue-refinement-loop/scripts/ 配下の producer / publisher 契約のうち、output_budget_violation、schema_mismatch、termination_bypass に関わる fail-closed 動作だけを対象にする
  • run_refinement_preflight.py / compact_review_result.py / compact_author_result.py / publish_termination_report.py 周辺の異常時 routing を検証し、termination report 直接投稿へ進まない契約を固定する
  • 対応テストは .claude/skills/issue-refinement-loop/scripts/tests/ に閉じる

Remaining Parent Gaps

  • preflight.run 以外の skill runtime command の root allow 拡張
  • publish_termination_report.py の mutation-capable root allow policy
  • registry の追加 command ID を privileged set に広げる設計

Outcome

issue-refinement-loop の producer / publisher 系が schema mismatch、output budget violation、termination bypass 試行時に fail-closed で停止し、artifact path と reason を返しつつ direct termination report 投稿へ進まないことをテスト付きで保証した状態。

In Scope

  • output budget 契約の機械可読 field 保持と raw dump 抑止
  • schema mismatch 時の environment_failure / human_judgment_required routing 固定
  • termination report 直接投稿バイパスの防止
  • 上記の fixture / pytest 追加
  • 必要最小限の docs 更新

Out of Scope

  • root allow policy や privileged executor の導入
  • publish_termination_report.py 自体を root から許可する設計
  • preflight.run 以外の runtime command 拡張
  • guard / settings の exact command class 実装

Acceptance Criteria

  • AC1: producer / compact producer の全異常系は、canonical failure envelope を stdout に出す。 必須 fields: STATUS, NEXT_ACTION, REASON_CODE, ARTIFACT, ARTIFACT_SHA256。 stdout は 2048 UTF-8 bytes 以下。raw issue body / raw comment / raw diff / raw renderer body は stdout に出さない。
  • AC2: schema mismatch は script ごとの fixture matrix で検証する。 run_refinement_preflight.py / compact_review_result.py / compact_author_result.py の各 failure path が environment_failure または human_judgment_required に routing され、failure artifact を残す。
  • AC3: output budget violation は REASON_CODE=output_budget_violation で fail-closed し、 original output は stdout に出さず sha256 / byte_count / bounded preview を artifact にだけ保存する。
  • AC4: termination_bypass_denied の negative test を持つ。 producer failure / compact schema mismatch / output budget violation のいずれでも publish_termination_report.py、publish()、_post_github_comment() は呼ばれない。
  • AC5: 実装: issue-refinement-loop producer の fail-closed routing を固定する #1165 は root allow / privileged executor / termination_report.publish policy を実装しない。 docs に 実装: hooks: preflight.run 専用 privileged executor を root checkout から安全実行可能にする #1154 / 実装: issue-refinement-loop producer の fail-closed routing を固定する #1165 / 実装: hooks: publish_termination_report 用 controlled mutation policy を分離する #1166 の責務分割を明記する。
  • AC6: compact_author_result.py は ISSUE_AUTHOR_RESULT_V1 の schema validation を追加するか、 schema-less consumer として検査対象フィールド・拒否条件を明文化し、fixture で固定する。
  • AC7: artifact path は production invocation で .claude/artifacts/issue-refinement-loop// 以下に固定する。 --artifact-dir 自由指定や --repo-root 省略は canonical path として扱わない。
  • AC8: 標準ゲート pnpm typecheck / lint / test / build が PASS。

Verification Commands

# baseline-expect: fail
# AC1
# AC2
$ rg -n "def test_review_compact_schema_mismatch_emits_failure_artifact" .claude/skills/issue-refinement-loop/scripts/tests/
# baseline-expect: fail
# AC3
$ rg -n "def test_review_compact_output_budget_violation_is_machine_readable" .claude/skills/issue-refinement-loop/scripts/tests/
# baseline-expect: fail
# AC2
# AC6
$ rg -n "def test_author_compact_schema_mismatch_emits_failure_artifact" .claude/skills/issue-refinement-loop/scripts/tests/
# baseline-expect: fail
# AC2
$ rg -n "def test_preflight_planner_fail_closed_payload_invalid_routes_environment_failure" .claude/skills/issue-refinement-loop/scripts/tests/
# baseline-expect: fail
# AC4
$ rg -n "def test_producer_failure_never_invokes_publish_termination_report" .claude/skills/issue-refinement-loop/scripts/tests/
# baseline-expect: fail
# AC4
$ rg -n "def test_termination_bypass_fixture_call_count_zero" .claude/skills/issue-refinement-loop/scripts/tests/
# baseline-expect: fail
# AC1
$ rg -n "def test_failure_stdout_never_contains_raw_issue_body_or_comment" .claude/skills/issue-refinement-loop/scripts/tests/
# baseline-expect: fail
# AC5
$ rg -n "1154.*1165.*1166" docs/dev/agent-skill-boundaries.md
# baseline-expect: fail
# AC7
$ rg -n "artifacts/issue-refinement-loop" .claude/skills/issue-refinement-loop/scripts/tests/
# AC8
$ pnpm typecheck
$ pnpm lint
$ pnpm test
$ pnpm build

Allowed Paths

  • .claude/skills/issue-refinement-loop/scripts/
  • docs/dev/agent-skill-boundaries.md

Stop Conditions

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

Required Skills

  • Python 3
  • pytest

Scope Delta(任意)

N/A

Runtime Verification Applicability

本 Issue の変更対象はワークフロー文書・スクリプトのみです。ゲームの実行時動作には影響しません。

decision: not_applicable
reason: "Workflow documentation and script changes only. No runtime game behavior changed."

Delivery Rule

1 Issue = 1 PR。worktree: worktree-issue-{N}-producer-fail-closed-routing。Draft PR で起票。

Activity

  1. added
    phase/implementation
    agent/implementer
    enhancement
    on Jun 25, 2026
  2. squne121 commented on Jun 26, 2026

    @squne121
    OwnerAuthor

    判定

    REQUEST_CHANGES。#1165 の目的は妥当ですが、現行 contract のまま実装すると “fail-closed routing” が hollow test になりやすいです。

    確認した範囲では、#1165 自体にコメントはなく、実質的な判断材料は parent #1154、#1154 のレビューコメント、#1137 / PR #1139 / PR #1143 の guard 境界議論、sibling #1166 の publish_termination_report.py mutation policy 分離です。#1165 は「producer / publisher の異常系を fail-closed に固定する」Issue であり、root allow・privileged executor・publish_termination_report.py の controlled mutation policy は明示的に out of scope です。


    P0-1: compact producer の失敗経路が “artifact path + reason” 契約を満たしていない

    #1165 の Outcome は、schema mismatch / output budget violation / termination bypass 試行時に fail-closed で停止し、artifact path と reason を返す状態を求めています。ところが現行 compact_review_result.py は JSON parse error・schema/value error・stdout 2048 bytes 超過時に STATUS: failed と stderr エラーだけを出して exit 2 し、NEXT_ACTION、REASON_CODE、ARTIFACT を返しません。

    同じ問題は compact_author_result.py にもあります。JSON parse error、input read error、conversion ValueError、stdout budget violation のいずれも、失敗 artifact を生成せず、機械可読の NEXT_ACTION / reason / artifact path を返しません。

    必須修正: #1165 の AC は「エラー時にも必ず canonical failure envelope を出す」と明記すべきです。最低限、以下の stdout を producer 共通 contract にしてください。

    STATUS: failed
    NEXT_ACTION: human_judgment_required | fix_environment
    REASON_CODE: schema_mismatch | output_budget_violation | termination_bypass_denied
    ARTIFACT: producer_failure_v1=<path>
    ARTIFACT_SHA256: <sha256>
    

    さらに、failure artifact には raw body/comment をそのまま出さず、producer_id、reason_code、input_sha256、stderr_sha256、redaction_applied、publisher_allowed: false を入れるべきです。


    P0-2: termination_bypass の境界が #1166 と混線している

    #1165 は publish_termination_report.py の root allow policy を out of scope にしています。一方、対象ファイルには publish_termination_report.py が含まれており、AC3 は「termination report 直接投稿へ進まない契約」を固定するとしています。これはそのままだと、publisher の controlled mutation policy まで #1165 で直したくなる危険な境界です。

    現行 publisher は publishable=true の renderer result を受けると _post_github_comment() を呼び、内部で gh issue comment <issue> --body-file - を実行しますが、--repo を渡していません。GitHub CLI の gh issue comment は --body-file - で stdin を本文にでき、親 option として --repo <OWNER/REPO> を持ちます。したがって remote mutation として扱うなら repo binding は必須です。 ([GitHub CLI]1) (GitHub CLI)

    この問題は #1166 が正しく切り出しています。#1166 は termination_report.publish を別 policy / 別 executor / repo・issue・artifact・env・idempotency binding 付きの controlled remote mutation として扱い、direct script allow を deny する設計です。#1165 側でやるべきことは、publisher を安全に root allow することではなく、producer 異常系から publisher が呼ばれないことを fixture で証明することです。

    必須修正: #1165 の AC3 は次に差し替えるべきです。

    AC3: schema_mismatch / output_budget_violation / producer_failed / invalid_compact_result のいずれかが発生した場合、publish_termination_report.py / publish() / _post_github_comment() は呼ばれない。テストは fake publisher または monkeypatch で call count = 0 を検証する。publish_termination_report.py 自体の root allow・repo binding・idempotency は #1166 の責務として参照のみ行う。
    

    P0-3: run_refinement_preflight.py は一部良いが、全失敗経路で artifact を保証していない

    run_refinement_preflight.py は canonical stdout fields を明示し、raw issue body/comments を stdout に出さない設計になっています。これは #1165 の AC1 方向に合っています。

    また、planner fail-closed payload の rewrite constraints が非 string・非 JSON serializable・invariant mismatch の場合に planner_fail_closed_payload_invalid 等を足し、_apply_exit_code_mapping() 側で environment failure に倒す経路もあります。この設計は PR #1071 の reason semantics / consumer rewrite budget 修正と整合しています。

    ただし、fixture input schema invalid などの early failure は _emit_failure_result() に raw_snapshot / planner_input が渡らず、artifact を書けない経路があります。#1165 の Outcome が「artifact path と reason」を要求するなら、early failure 用の minimal failure artifact を必須にする必要があります。

    必須修正: run_refinement_preflight.py も compact scripts も、失敗時 artifact writer を共通化してください。raw_snapshot が存在しない場合でも、少なくとも producer_id、phase、reason_code、exit_code、input_origin、input_sha256、raw_payload_stored: false を持つ failure artifact を書くべきです。


    P0-4: compact_author_result.py は schema mismatch を十分に検出していない

    compact_review_result.py は review result schema を読み、jsonschema.validate() を呼んでいます。

    一方、compact_author_result.py は status と body hash を中心に見ており、ISSUE_AUTHOR_RESULT_V1 の producer schema 全体を validate していません。つまり、status=ok と checked_body_sha256 だけがある不完全 payload が、意図せず通る可能性があります。

    必須修正: #1165 の schema mismatch fixture matrix に、最低限この 2 系統を入れてください。

    compact_review_result:
    - producer schema missing
    - invalid verdict
    - invalid status
    - non-finite JSON
    - missing blocking_issues shape
    - schema additionalProperties violation
    
    compact_author_result:
    - producer schema missing
    - status ok but checked_body_sha256 missing
    - status unknown
    - checked_body_sha256 malformed
    - mutation_result wrong type
    - comment_url present on failed path
    

    compact_author_result.py には author result schema validation を追加するか、意図的に schema-less consumer にするなら、その理由と検査対象フィールドを Issue contract に固定する必要があります。


    P0-5: output budget violation の検査対象が stdout だけでは足りない

    現行 compact scripts は stdout 2048 bytes 超過を検出しますが、超過時に artifact を残さず、STATUS: failed だけを出します。これは「budget violation で fail-closed」とは言えても、orchestrator が次の route を機械判定できません。

    必須修正: output budget violation は、次の仕様で固定すべきです。

    - stdout は常に 2048 UTF-8 bytes 以下
    - 超過時は raw stdout を出さない
    - failure artifact に original_stdout_sha256 / original_stdout_bytes / truncated_preview を保存
    - stdout は REASON_CODE=output_budget_violation と ARTIFACT のみ返す
    - publish_termination_report.py は呼ばれない
    

    Claude Code hooks は hook output に長さ上限があり、上限超過時はファイル保存と preview/path 置換になるため、producer 側でも「出しすぎた後に runtime が処理してくれる」前提にしない方がよいです。([Claude Platform Docs]2)


    P1-1: artifact containment は root allow とは別に、handoff artifact として最低限固定すべき

    #1154 のレビューは、mutation:false や registry metadata を認可根拠にせず、effect/path binding を持つ単一 executor に寄せるべきだと指摘しています。特に compact_review_result.py は --repo-root が optional で、--artifact-dir が自由指定できる点が既にリスクとして挙げられています。

    現行 compact_review_result.py / compact_author_result.py は repo_root が渡された場合だけ Path.resolve().is_relative_to(base) で containment を確認します。CLI では --artifact-dir が任意で、--repo-root は optional です。

    Python の Path.resolve() は symlink と .. を解決できますが、PurePath.is_relative_to() は文字列ベースで filesystem にアクセスせず、.. も特別扱いしません。したがって privileged path policy では、単なる lexical containment ではなく、realpath / symlink component / regular file / owner-mode / size を明示的に扱う必要があります。([Python documentation]3) ([Python documentation]3)

    #1165 での落としどころ: root-level hardening は #1154 / #1166 に残してよいです。ただし #1165 の producer failure artifact は、少なくとも production path では .claude/artifacts/issue-refinement-loop/<issue>/... に固定し、test でも --repo-root なしの任意 --artifact-dir 成功を canonical path として扱わないでください。


    P1-2: docs AC5 は弱い。責務境界を明文化しないと再発する

    docs/dev/agent-skill-boundaries.md は Agent / Skill 責務境界の SSOT とされています。現行 docs には issue-refinement-loop が thin orchestrator で、raw context を保持・再解釈しないという責務境界がありますが、#1154 / #1165 / #1166 の三分割はまだ十分に固定されていません。

    必須修正: docs には次の責務分割を明記してください。

    #1154:
      preflight.run の root checkout 実行を、専用 privileged executor で許可する。
      producer/publisher の fail-closed contract は扱わない。
    
    #1165:
      issue-refinement-loop producer / compact producer の異常系 routing を data contract として固定する。
      schema_mismatch / output_budget_violation / termination_bypass_denied は publisher を呼ばず、failure artifact と reason を返す。
    
    #1166:
      publish_termination_report.py を root checkout から扱う必要がある場合の controlled remote mutation policy。
      direct script allow は deny。repo/issue/input/idempotency/read-back/env/import を executor が束縛する。
    

    Codex の PreToolUse hooks は Bash / apply_patch / MCP tool calls を intercept できますが、別 tool path で同等作業ができるため完全な enforcement boundary ではない、と公式 docs が明記しています。Claude Code でも Bash hook if filter は best-effort で、hard allow/deny には permission system を使うべきとされています。docs では「supported Bash PreToolUse path の guardrail」であって sandbox ではない、と書くべきです。([OpenAI 開発者]4) ([Claude Platform Docs]2)


    P1-3: テストが rg existence check に寄ると hollow になる

    #1165 の Verification Commands は rg による存在確認が中心です。これは baseline check としては有用ですが、#1139 / #1143 の経緯を見ると、関数直接呼び出しや存在確認だけでは実行経路の欠陥を隠します。PR #1139 の OWNER review では、preflight / materialize が実 hook route では deny されるのに、テストが関数直接呼び出しで隠していたことが blocker になりました。

    #1165 では hook route を直接扱わないにしても、最低限 behavioral fixture にしてください。

    tests:
    - test_review_compact_schema_mismatch_emits_failure_artifact
    - test_review_compact_output_budget_violation_is_machine_readable
    - test_author_compact_schema_mismatch_emits_failure_artifact
    - test_preflight_planner_fail_closed_payload_invalid_routes_environment_failure
    - test_producer_failure_never_invokes_publish_termination_report
    - test_termination_bypass_fixture_call_count_zero
    - test_failure_stdout_never_contains_raw_issue_body_or_comment
    

    修正版 Acceptance Criteria 案

    現行 AC1〜AC6 は抽象度が高すぎます。次のように落とすべきです。

    AC1: producer / compact producer の全異常系は、canonical failure envelope を stdout に出す。
    必須 fields: STATUS, NEXT_ACTION, REASON_CODE, ARTIFACT, ARTIFACT_SHA256。
    stdout は 2048 UTF-8 bytes 以下。raw issue body / raw comment / raw diff / raw renderer body は stdout に出さない。
    
    AC2: schema mismatch は script ごとの fixture matrix で検証する。
    run_refinement_preflight.py / compact_review_result.py / compact_author_result.py の各 failure path が
    environment_failure または human_judgment_required に routing され、failure artifact を残す。
    
    AC3: output budget violation は REASON_CODE=output_budget_violation で fail-closed し、
    original output は stdout に出さず sha256 / byte_count / bounded preview を artifact にだけ保存する。
    
    AC4: termination_bypass_denied の negative test を持つ。
    producer failure / compact schema mismatch / output budget violation のいずれでも
    publish_termination_report.py、publish()、_post_github_comment() は呼ばれない。
    
    AC5: #1165 は root allow / privileged executor / termination_report.publish policy を実装しない。
    docs に #1154 / #1165 / #1166 の責務分割を明記する。
    
    AC6: compact_author_result.py は ISSUE_AUTHOR_RESULT_V1 の schema validation を追加するか、
    schema-less consumer として検査対象フィールド・拒否条件を明文化し、fixture で固定する。
    
    AC7: artifact path は production invocation で
    .claude/artifacts/issue-refinement-loop/<issue>/ 以下に固定する。
    --artifact-dir 自由指定や --repo-root 省略は canonical path として扱わない。
    
    AC8: 標準ゲート pnpm typecheck / lint / test / build が PASS。
    

    実装品質メモ

    subprocess.run() は args sequence で渡す方が推奨され、shell=True は shell を介します。また POSIX では executable resolution が cwd / env / PATH に影響され得るため、privileged 経路では sys.executable や canonical path、明示 env を使う方が安全です。#1165 の producer tests でも、shell string の逆照合ではなく argv / call count / artifact content を検証してください。([Python documentation]5)

    shlex は shell-like syntax の lexical analyzer であり、quote() も非 POSIX shell では正しさが保証されないため、authorization boundary として使うべきではありません。#1154 で問題になった registry reverse matching や newline bypass の再導入を避けるには、producer failure routing のテストも「文字列に含まれるか」ではなく、実際に publisher 関数が呼ばれないことを検証してください。([Python documentation]6) ([Python documentation]6)


    最終結論

    #1165 は必要です。ただし現行 contract は、実装者が「エラー時に STATUS: failed を出すだけ」「rg でテスト名だけ置く」「publisher を触らずに termination bypass を名目だけ固定する」実装でも通ってしまう余地があります。

    着手前に、failure envelope、artifact guarantee、publisher non-invocation、#1154/#1165/#1166 の責務分割を AC に明記してください。
    その修正後であれば、#1165 は root allow とは独立した producer fail-closed hardening Issue として成立します。

  3. squne121 commented on Jun 26, 2026

    @squne121
    OwnerAuthor

    Refinement Loop: Approved

    The issue has been approved and is ready for implementation.

  4. squne121 commented on Jun 26, 2026

    @squne121
    OwnerAuthor

    CONTRACT_REVIEW_RESULT_V1

    status: go
    source: all_checks_pass
    issued_at: 2026-06-27

    VC Preflight Summary

    AC Command Classification Decision
    AC2 rg -n "def test_review_compact_schema_mismatch_emits_failure_artifact" expected_baseline_fail go
    AC3 rg -n "def test_review_compact_output_budget_violation_is_machine_readable" expected_baseline_fail go
    AC6 rg -n "def test_author_compact_schema_mismatch_emits_failure_artifact" expected_baseline_fail go
    AC2 rg -n "def test_preflight_planner_fail_closed_payload_invalid_routes_environment_failure" expected_baseline_fail go
    AC4 rg -n "def test_producer_failure_never_invokes_publish_termination_report" expected_baseline_fail go
    AC4 rg -n "def test_termination_bypass_fixture_call_count_zero" expected_baseline_fail go
    AC1 rg -n "def test_failure_stdout_never_contains_raw_issue_body_or_comment" expected_baseline_fail go
    AC5 rg -n "1154.*1165.*1166" docs/dev/agent-skill-boundaries.md expected_baseline_fail go
    AC7 rg -n "artifacts/issue-refinement-loop" expected_baseline_fail go
    AC8 pnpm typecheck regression_gate go (exit 0)
    AC8 pnpm lint regression_gate go (exit 0)
    AC8 pnpm test regression_gate go (81 files, 1123 tests passed)
    AC8 pnpm build regression_gate go (exit 0)

    Checks

    • readiness: go
    • blockers: pass
    • product_spec: pass
    • vc_preflight: pass

    Result: この Issue は実装着手可能です。

  5. squne121 commented on Jun 26, 2026

    @squne121
    OwnerAuthor

    Contract Review Result

    CONTRACT_REVIEW_RESULT_V1:
      status: go
      generated_at: "2026-06-26T22:56:10Z"
      generated_by: issue-contract-review
      issue_url: https://github.com/squne121/loop-protocol/issues/1165
      body_sha256: "sha256:2f64fb40533d894a885e9c2922468fe92d4680c06331b54693bc5ba2df75d85a"
      checks:
        readiness: go
        blockers: pass
        product_spec: pass
        vc_preflight: pass
      source: ensure_contract_snapshot_auto
  6. squne121 commented on Jun 26, 2026

    @squne121
    OwnerAuthor

    IMPLEMENT_RESULT_V1:
    status: ok
    pr_url: #1194
    pr_number: 1194
    head_sha: fb6682f
    branch: worktree-issue-1165-issue-refinement-loop-producer-fail-clos
    issue_number: 1165
    allowed_paths_compliance: true
    verification_summary:
    - "11 新規 pytest PASS (test_producer_fail_closed.py — AC1-AC7)"
    - "34 skill pytest PASS (既存 23 + 新規 11)"
    - "pnpm typecheck: PASS"
    - "pnpm lint: PASS"
    - "pnpm test: 1123 PASS"
    - "pnpm build: PASS"
    - "test_model_routing.py: 21 PASS"
    key_implementation_notes:
    - "jsonschema.ValidationError は jsonschema 4.x では ValueError サブクラスでないため except (ValueError, Exception) で補足"
    - "compact_review_result.py / compact_author_result.py に _write_failure_artifact() + _emit_failure_envelope() 追加"
    - "compact_author_result.py に ISSUE_AUTHOR_RESULT_V1_SCHEMA_LESS_CONTRACT 定数追加 (AC6)"
    - "docs/dev/agent-skill-boundaries.md に #1154/#1165/#1166 責務分割テーブル追加 (AC5)"

  7. added 2 commits that reference this issue on Jun 27, 2026
    024b8a3
    23b08f5
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