Skip to content

調査: gemini-cli-headless-delegation の failure_class taxonomy と retry policy を schema 化する #268

Closed

Description

@squne121

Machine-Readable Contract

contract_schema_version: v1
issue_kind: research
parent_issue: "#14"
goal_ref: "gemini-cli-headless-delegation の preflight / run wrapper に failure_class taxonomy と retry policy を schema 化し、transient error の誤分類を解消する"
change_kind: research-only

Parent Issue

#14

Outcome

gemini-cli-headless-delegation の preflight / run wrapper の result JSON に failure_class(retry 可否付き)、retryable、attempts、last_stderr_summary フィールドが追加され、config error は retry せず quota/transient error のみ backoff retry する方針が SKILL.md または references/ 配下のドキュメントに記録された状態となり、後続の実装 Issue が起票可能になる。

Background

Issue #71 の refinement-loop で判明した問題として、auth.ok:false が transient エラー(quota / 一時的 API エラー)と真の認証失効を同一フィールドに丸めているため、retry 可否の判断ができず、config error でも quota error でも同じ fail-close になっている。

Issue #40 の観測では:

  • codebase-investigator が auth.ok:false で fail-close
  • 同時刻に web-researcher が quota retry で成功
  • 数分後に codebase-investigator が再試行で成功

これは auth.ok:false が transient error の誤分類である証拠であり、retry policy が非対称になっている。

現行の result JSON には failure_class / retryable / attempts / last_stderr_summary フィールドが存在せず、caller 側で retry 可否を判断する根拠が得られない。

In Scope

  • failure_class の分類体系(taxonomy)の定義と文書化(最低限: config_invalid、cli_missing、auth_missing_or_expired、quota_or_rate_limited、transient_api_error、network_error、output_parse_error)
  • preflight result JSON への retryable: true|false、attempts: int、last_stderr_summary: string フィールド仕様の策定
  • retry policy の方針確定(config_invalid / cli_missing / cli_incompatible は retry 不可、quota_or_rate_limited / transient_api_error / network_error は backoff retry 可)と SKILL.md または references/ への記録
  • 後続の実装 Issue 起票

Out of Scope

Acceptance Criteria

  • AC1: failure_class の分類体系が定義され SKILL.md または references/ 配下のドキュメントに記録されている(最低限: config_invalid、cli_missing、auth_missing_or_expired、quota_or_rate_limited、transient_api_error、network_error、output_parse_error)
  • AC2: preflight の result JSON 仕様に retryable: true|false、attempts: int、last_stderr_summary: string が追加されている
  • AC3: config_invalid / cli_missing / cli_incompatible は retry 不可(fail-close)、quota_or_rate_limited / transient_api_error / network_error は backoff retry 可という方針が SKILL.md または references/ に記録されている
  • AC4: 本 Issue を受けた後続の実装 Issue が起票されており、本 Issue がそれを link している

Verification Commands

# AC1: failure_class taxonomy の記述確認
$ rg "failure_class|config_invalid|quota_or_rate_limited|transient_api_error" .claude/skills/gemini-cli-headless-delegation/ && echo "PASS: AC1" || echo "FAIL: AC1"

# AC2: retryable / attempts / last_stderr_summary フィールドの記述確認
$ rg "retryable|last_stderr_summary" .claude/skills/gemini-cli-headless-delegation/ && echo "PASS: AC2" || echo "FAIL: AC2"

# AC3: retry 不可 / retry 可方針の記述確認
$ rg "retry.*config_invalid|cli_missing.*retry|fail-close|backoff retry" .claude/skills/gemini-cli-headless-delegation/ && echo "PASS: AC3" || echo "FAIL: AC3"

# AC4: 後続実装 Issue が起票されているか確認(本 Issue クローズ前に番号を差し替える)
$ gh issue list --search "実装: gemini-cli-headless-delegation failure_class" --state open --json number,title,url

Allowed Paths

  • 読み取り専用。リポジトリ変更なし(既定)
  • 調査成果物として failure_class taxonomy を文書化する場合のみ: .claude/skills/gemini-cli-headless-delegation/

Stop Conditions

  • Outcome / Scope / AC が検索可能な形で記載されていない場合は即停止
  • Allowed Paths 外への書き込みを試みた場合は即停止
  • 権限不足(permission denied)により操作が完了できない場合は即停止
  • 成果物の書き込みに失敗した場合(I/O エラー等)は即停止

Runtime Verification Applicability

decision: not_applicable
reason: research-only Issue。調査レポートの存在確認と grep による記述確認のみ。実装変更なし。

Handoff Contract

  • Current Objective: failure_class taxonomy の確定と result JSON フィールド仕様の策定
  • Bounded Current Context: .claude/skills/gemini-cli-headless-delegation/ の preflight / run wrapper 周辺
  • Open Questions: 既存 stderr 出力から failure_class を判別するロジックの実現可能性
  • Next Action: taxonomy と retry policy 確定後、後続の実装 Issue(result JSON フィールド追加・preflight wrapper 改修)を起票する
  • Artifact Refs: .claude/skills/gemini-cli-headless-delegation/SKILL.md、references/ 配下の新規文書

Source

dedupe_key: transient-quota-auth-failure-taxonomy-retry-policy
source_issue: #71
source_url: #71

Activity

  1. added
    phase/research
    agent/research
    on May 23, 2026
  2. squne121 commented on May 23, 2026

    @squne121
    OwnerAuthor

    issue-refinement-loop: C4(VC コマンド行に $ プレフィックス追加)、C9 warn 解消(Runtime Verification Applicability 追加)。

  3. squne121 commented on May 23, 2026

    @squne121
    OwnerAuthor

    敵対的レビュー結論

    判定: needs-fix before implementation issue.

    Issue #268 の問題意識は正しいです。#71 は未解決項目として「transient / quota / API failure の auth 誤分類」を明示しており、#268 はその follow-up として妥当です。
    ただし、現状の #268 は taxonomy を定義するには粒度が粗く、後続実装 Issue に渡すと再び auth.ok:false 的な過剰集約スキーマを作る危険が高いです。


    重大指摘

    1. 「Issue #40 の観測」根拠が弱い

    #268 は「Issue #40 の観測」として、codebase-investigator が auth.ok:false fail-close、web-researcher が quota retry 成功、数分後に codebase-investigator が成功した、という事象を根拠にしています。
    しかし #40 本文は DoR / state label / Plan Mode を束ねる parent tracker であり、少なくとも取得できた本文だけではその具体的 runtime 観測の一次証跡には見えません。

    修正: #268 の Background に、実際の auth.ok:false / quota retry / later success が記録された 正確な issuecomment URL または runtime log artifact を追加すべきです。根拠が曖昧なまま taxonomy 化すると、「現象の再現性」ではなく「推測」をスキーマに固定します。


    2. #101 と衝突している

    #268 は preflight / run wrapper の failure classification を扱いますが、Out of Scope で #101 の per-profile preflight を外しています。
    一方、#101 は現行 preflight_gemini_headless.py が profile に関係なく local_asset_research の Serena 設定を検証し、無関係な github_research / grounded_research / no_tools まで top-level ok:false にする問題を扱っています。

    現行 preflight 実装でも local_asset_errors があれば failure_reason に設定して即 return しており、smoke test へ進みません。

    修正: #268 は #101 を単に Out of Scope にするのではなく、次のどちらかを明示すべきです。

    この区別なしに config_invalid を導入すると、#101 の誤 fail-close を「仕様」として固定するリスクがあります。


    3. retryable: true|false は情報量が不足している

    Gemini API 公式の troubleshooting では、400 は malformed request、403 は権限・認証、429 は rate limit、500/503 は一時的な Google 側障害や過負荷、504 は deadline 超過として整理されています。特に 500/503 は待機・モデル切替・再試行が候補ですが、504 は timeout 拡大や context 削減が必要になる場合があります。([Google AI for Developers]1)
    また rate limit は RPM / TPM / RPD など複数軸で発生します。([Google AI for Developers]2)

    したがって retryable: boolean だけでは足りません。必要なのは少なくとも以下です。

    retryable: true | false
    retry_scope: none | same_model | next_model | same_request_after_backoff | after_external_state_change
    retry_after_ms: int | null
    retry_reason: string

    Google Cloud の retry guidance でも、retry 可否は「レスポンス」と「操作の冪等性」の両方で決まるとされ、408/429/5xx や socket timeout は一般に retryable だが、永続的エラーは authorization/config 変更が必要と整理されています。([Google Cloud]3)


    4. 現行 wrapper には retry があるが、caller に見えない

    run_gemini_headless.py は RETRY_LIMIT = 2 を持ち、capacity / 429 系を _is_retryable_capacity_failure() で検出して同一 model を retry し、さらに model downgrade も行います。
    しかし result JSON には attempts、attempts_by_model、retry_budget_exhausted、last_stderr_summary が出ていません。最終的に model_chain_exhausted の reason_code は出るものの、どの model で何回失敗したかは caller が安定して読めません。

    修正: attempts は単なる int ではなく、最低限こう定義すべきです。

    attempts: 4                 # subprocess invocation total
    attempts_by_model:
      - model: gemini-3-flash-preview
        attempts: 3
        final_failure_class: quota_or_rate_limited
      - model: gemini-2.5-flash
        attempts: 1
        final_failure_class: null
    retry_budget_exhausted: false

    5. ACP transport の既存知見を taxonomy に反映していない

    ACP 側には既に failure_class の先行実装があります。auth_required は fallback で隠さず hard failure として surface する設計になっており、authMethods が存在するだけでは auth_required と判定しない、というコメントも実装に残っています。
    さらに ACP fallback は gemini_not_found / launch_failed / initialize_failed / session_new_failed のような early failure class に限定され、auth_required は明示的に除外されています。

    #268 は ACP transport を Out of Scope にしていますが、taxonomy 設計の prior art としては必ず参照すべきです。ここを無視すると、headless_json 側でまた「auth っぽい文字列があったら auth_missing_or_expired」型の誤分類を再発させます。


    推奨 taxonomy

    #268 の最低限 taxonomy は以下に置き換えるべきです。

    failure_taxonomy_version: v1
    
    fields:
      failure_class:
        type: enum
        nullable: true
        meaning: "最も具体的な失敗分類。成功時は null。"
    
      failure_origin:
        type: enum
        values:
          - request_validation
          - runtime_preflight
          - cli_process
          - api_backend
          - output_contract
          - github_preflight
          - acp_transport
          - post_processing
    
      retryable:
        type: boolean
    
      retry_scope:
        type: enum
        values:
          - none
          - same_model
          - next_model
          - same_request_after_backoff
          - after_external_state_change
    
      attempts:
        type: int
        meaning: "Gemini CLI subprocess invocation total"
    
      attempts_by_model:
        type: list
        item:
          model: string
          attempts: int
          final_failure_class: string|null
    
      last_stderr_summary:
        type: string|null
        constraints:
          max_chars: 240
          redact:
            - API keys
            - OAuth tokens
            - access tokens
            - absolute home paths where feasible
    
      classification_confidence:
        type: enum
        values: [high, medium, low]

    failure_class は最低限これくらい必要です。

    non_retryable:
      - request_schema_invalid
      - request_policy_denied
      - config_invalid
      - cli_missing
      - cli_incompatible
      - trusted_workspace_required
      - auth_missing_or_expired
      - gh_auth_required
      - mcp_config_invalid
      - mcp_tool_policy_invalid
      - github_research_command_denied
    
    retryable_or_conditional:
      - quota_or_rate_limited
      - model_capacity_exhausted
      - transient_api_error
      - network_error
      - timeout
      - output_parse_error
      - empty_response
    
    terminal_or_exhausted:
      - model_chain_exhausted
      - retry_budget_exhausted
      - unknown_cli_failure
      - unknown_api_error

    output_parse_error は常時 retryable にしない方がよいです。CLI の JSON 出力契約破壊、prompt leakage、stderr/stdout 混線、CLI version incompatibility の可能性があるため、最大 1 回の retry か、classification_confidence: low で human escalation が妥当です。


    #268 本文への具体的パッチ方針

    追加すべき AC

    - [ ] AC5: `delegation_result/v1` と `gemini_headless_preflight_result/v1` の producer / consumer inventory が記録されている
    - [ ] AC6: `retryable` だけでなく `retry_scope` / `retry_after_ms` / `attempts_by_model` の要否が判断されている
    - [ ] AC7: raw signal → failure_class → retry policy の fixture table が references/ に記録されている
    - [ ] AC8: auth 判定は explicit auth signal のみに基づき、`authMethods` や `auth.ok:false` のような aggregate field だけでは `auth_missing_or_expired` に分類しない
    - [ ] AC9: #101 未解決中の preflight top-level classification 方針が明記されている

    Verification Commands は grep だけでは弱い

    現行 AC は rg で文字列があることしか見ません。
    docs-only research でも、次のような fixture table 検査を入れるべきです。

    # taxonomy table に必須 raw signal があること
    rg -n "HTTP 429|RESOURCE_EXHAUSTED|HTTP 500|HTTP 503|PERMISSION_DENIED|INVALID_ARGUMENT|trusted_workspace_required|auth_required" \
      .claude/skills/gemini-cli-headless-delegation/references/
    
    # retry_scope が boolean だけに潰れていないこと
    rg -n "retry_scope|same_model|next_model|after_external_state_change|attempts_by_model" \
      .claude/skills/gemini-cli-headless-delegation/references/

    後続実装 Issue の切り方

    #268 の AC4 は「後続実装 Issue 起票」ですが、1 Issue に preflight と run wrapper を両方入れるとまた大きすぎます。分割推奨です。

    Issue A: run wrapper の result schema 拡張

    対象:

    .claude/skills/gemini-cli-headless-delegation/scripts/run_gemini_headless.py
    .claude/skills/gemini-cli-headless-delegation/tests/
    .claude/skills/gemini-cli-headless-delegation/references/failure-taxonomy.md
    

    内容:

    • failure_class
    • failure_origin
    • retryable
    • retry_scope
    • attempts
    • attempts_by_model
    • last_stderr_summary
    • quota / transient / timeout / parse error fixtures

    Issue B: preflight result schema 拡張

    #101 と依存関係を明示。#101 完了前なら top-level ok は触らず、section-local classification のみ。

    対象:

    .claude/skills/gemini-cli-headless-delegation/scripts/preflight_gemini_headless.py
    .claude/skills/gemini-cli-headless-delegation/tests/test_preflight_gemini_headless.py
    

    内容:

    • cli_missing
    • cli_incompatible
    • trusted_workspace_required
    • mcp_config_invalid
    • gh_auth_required
    • output_parse_error
    • retryable/retry_scope

    最終提案

    #268 は close せず、まず本文を修正してください。特に次の 4 点は必須です。

    1. 導入: Issue contract / DoR / dependency 判定による AI 実装前提基盤の整備 (parent) #40 観測の正確な anchor を貼る
    2. 実装: preflight_gemini_headless.py を per-profile 検証化し local_asset_research 不備で preflight 全体を fail させない #101 との依存・非依存境界を明確にする
    3. retryable boolean ではなく retry_scope と attempts_by_model を入れる
    4. ACP transport の auth_required / fallback 除外の知見を taxonomy 設計に反映する

    この修正後なら、#268 は「調査 Issue」として有効です。現状のまま後続実装に進むと、auth.ok:false を failure_class に名前だけ変えたような、caller が正しく判断できない schema になる可能性が高いです。

  4. squne121 commented on May 23, 2026

    @squne121
    OwnerAuthor

    Contract Snapshot (2026-05-23T07:00:00Z)

    CONTRACT_REVIEW_RESULT_V1

    status: go
    generated_at: "2026-05-23T07:00:00Z"
    generated_by: issue-contract-review
    issue_url: https://github.com/squne121/loop-protocol/issues/268
    checks:
      template_compliance: pass
      state_label: "warning (state/queued 不在 — non-blocking)"
      allowed_paths_present: pass
      vc_present: pass
      stop_conditions_complete: n/a
      vc_preflight:
        passed: true
        vc_failed_as_expected: 1
        vc_passed_unexpectedly: 3
        vc_unrunnable: 0
        classifications:
          - ac: AC1
            command: "rg 'failure_class|config_invalid|quota_or_rate_limited|transient_api_error' .claude/skills/gemini-cli-headless-delegation/"
            exit_code: 0
            category: vc_passed_unexpectedly
            confidence: high
            evidence:
              stdout_excerpt: "failure_class が ACP transport コード (transport-acp.md 等) に既存マッチ。routing_config_invalid が config_invalid に部分マッチ"
              stderr_excerpt: ""
            decision: go_with_warning
          - ac: AC2
            command: "rg 'retryable|last_stderr_summary' .claude/skills/gemini-cli-headless-delegation/"
            exit_code: 0
            category: vc_passed_unexpectedly
            confidence: high
            evidence:
              stdout_excerpt: "_is_retryable_capacity_failure 関数名が 'retryable' にマッチ。last_stderr_summary は NOT FOUND"
              stderr_excerpt: ""
            decision: go_with_warning
          - ac: AC3
            command: "rg 'retry.*config_invalid|cli_missing.*retry|fail-close|backoff retry' .claude/skills/gemini-cli-headless-delegation/"
            exit_code: 0
            category: vc_passed_unexpectedly
            confidence: high
            evidence:
              stdout_excerpt: "'fail-close' が既存 docs にマッチ。'backoff retry' は model-routing.md 等にマッチ"
              stderr_excerpt: ""
            decision: go_with_warning
          - ac: AC4
            command: "gh issue list --search '実装: gemini-cli-headless-delegation failure_class' --state open --json number,title,url"
            exit_code: 0
            category: expected_baseline_fail
            confidence: high
            evidence:
              stdout_excerpt: "後続実装 Issue 未起票(#268 自身と無関係の Issue のみヒット)"
              stderr_excerpt: ""
            decision: go
      ac_verifiability: pass
      worktree_branch_collision: clear
    next_action: implement_issue
    blocked_reasons: []
    warnings:
      - "AC1/AC2/AC3 の VC が緩すぎる。taxonomy 新規値 (auth_missing_or_expired, quota_or_rate_limited, transient_api_error, network_error, output_parse_error, cli_missing, last_stderr_summary) は NOT FOUND — 実装作業は確実に未完。VC 修正を PR 改善点として記録。"
      - "ユーザー指示 '承認不要で行って' に基づきユーザー override を適用し go として proceed"

    実装詳細

    • Outcome: failure_class taxonomy 確定 + retry policy 文書化 + 後続実装 Issue 起票
    • Worktree: .claude/worktrees/issue-268-gemini-cli-headless-delegation-failure-c
    • Branch: worktree-issue-268-gemini-cli-headless-delegation-failure-c
    • VC preflight: baseline fail = 1 (AC4), 想定外 pass = 3 (AC1/AC2/AC3 — loose VCs, user override applied), 実行不可 = 0
  5. squne121 commented on May 23, 2026

    @squne121
    OwnerAuthor

    implement-issue: AC4 後続実装 Issue 起票

    後続の実装 Issue を起票しました:

    この Issue の taxonomy と retry policy 文書(references/failure-class-taxonomy.md)を仕様の正本として #277 で実装を進めます。

  6. squne121 commented on May 23, 2026

    @squne121
    OwnerAuthor

    implement-issue: 実装完了 (2026-05-23T06:58:45Z)

  7. added 2 commits that reference this issue on May 23, 2026
  8. squne121 commented on May 23, 2026

    @squne121
    OwnerAuthor

    post-merge-cleanup: 完了 (2026-05-23T07:35:00Z)

    FOLLOW_UP_MATERIALIZATION_RESULT_V1:
      follow_up_issues:
        - request_dedupe_key: "follow-up:squne121/loop-protocol:pr/278:last-error-summary-canonical"
          issue_number: 288
          issue_url: "https://github.com/squne121/loop-protocol/issues/288"
          status: created
    
        - request_dedupe_key: "follow-up:squne121/loop-protocol:pr/278:preflight-checks-structure"
          issue_number: 289
          issue_url: "https://github.com/squne121/loop-protocol/issues/289"
          status: created
    
        - request_dedupe_key: "follow-up:squne121/loop-protocol:pr/278:mcp-tool-policy-security-note"
          issue_number: 290
          issue_url: "https://github.com/squne121/loop-protocol/issues/290"
          status: created
    
      note_only_observations: []
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/researchphase/research

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions