Skip to content

実装: agy preflight を追加し agy --help / agy -p 契約を実機検出する #109

Closed

Description

@squne121

Machine-Readable Contract

contract_schema_version: v1
issue_kind: implementation
parent_issue: "#104"
goal_ref: "agy CLI の実機仕様を preflight で検出し、wrapper が安全に agy -p を呼べる前提条件を確立する"
change_kind: code

Parent Issue

#104

Parent Goal Ref

Current Validated Scope

現行の preflight_gemini_headless.py は Gemini CLI 専用(gemini auth login / gemini --version / --approval-mode / --output-format)。agy の実機仕様(agy -p / --print / --prompt、chat サブコマンドなし、--output-format なし)とは非互換。

agy 専用 preflight を独立スクリプトとして実装し、以下を検出する:

  1. agy バイナリの存在確認(cli_missing 分類)
  2. agy --help 実行し -p / --print / --prompt フラグの存在確認(欠如時は cli_incompatible 分類)
  3. chat サブコマンドや --output-format が存在しても FAIL にせず warnings に記録する
  4. smoke check: isolated temp cwd・timeout=20 秒・shell=False で agy -p <SMOKE_PROMPT> を実行
  5. agy バイナリは subprocess.run([agy_bin, ...], shell=False) で呼び出し、PATH injection を防ぐ

Remaining Parent Gaps

#104 の Remaining Parent Gaps を参照。

Outcome

uv run python3 .claude/skills/gemini-cli-headless-delegation/scripts/preflight_agy.py --json が実行可能であり、以下の shape で stdout に JSON を出力する:

  • agy 不在時: exit 1、{"schema": "agy_preflight_result/v1", "ok": false, "failure_class": "cli_missing", ...}
  • agy 存在・非対話フラグなし時: exit 1、{"failure_class": "cli_incompatible", ...}
  • smoke 成功時: exit 0、{"ok": true, ...}
  • smoke timeout 時: exit 1、{"failure_class": "client_subprocess_timeout", ...}

In Scope

  • .claude/skills/gemini-cli-headless-delegation/scripts/preflight_agy.py を新規作成:
    • _resolve_binary(): AGY_BIN 環境変数で上書き可能
    • _run_version(): agy --version でバイナリ存在・バージョン検出
    • _run_help(): agy --help 出力を解析し -p / --print / --prompt フラグ存在を確認
    • _parse_help_capabilities(help_text): noninteractive_flags、unexpected_capabilities を返す
    • _run_smoke(): isolated temp cwd、timeout=20 秒、shell=False で agy -p <SMOKE_PROMPT> を実行
    • run_preflight(): version → help → smoke の順に実行し agy_preflight_result/v1 JSON を返す
    • main(): --json(stdout JSON)、--output-file(JSON 保存)オプション
    • 成功 exit 0、失敗 exit 1
    • JSON schema: {"schema": "agy_preflight_result/v1", "ok": bool, "failure_reason": str|null, "failure_class": str|null, "recovery_action": str|null, "agy": {"bin": str, "resolved_path": str|null, "version": str|null}, "help": {"ok": bool, "noninteractive_flags": {"-p": bool, "--print": bool, "--prompt": bool}, "unexpected_capabilities": list}, "smoke": {"ok": bool, "argv": list, "exit_code": int|null, "timed_out": bool, "stdout_sample": str, "stderr_sample": str}, "warnings": list}
  • .claude/skills/gemini-cli-headless-delegation/tests/test_preflight_agy.py を新規作成:
    • test_cli_missing: AGY_BIN=/nonexistent/agy でスクリプトを subprocess 実行し exit 1・failure_class: cli_missing・failure_reason を含む JSON を確認
    • test_no_shell_invocation: スクリプトソースに shell=True が存在しないことを確認
    • mock agy --help に -p / --print / --prompt フラグがある場合の help.noninteractive_flags 検出テスト
    • mock agy --help に非対話実行フラグなし → cli_incompatible テスト
    • chat / --output-format が help に存在 → warnings または help.unexpected_capabilities に記録テスト
    • smoke timeout → client_subprocess_timeout テスト
    • --json 出力・exit code テスト

Out of Scope

Acceptance Criteria

  • AC1: AGY_BIN=/nonexistent/agy uv run python3 .claude/skills/gemini-cli-headless-delegation/scripts/preflight_agy.py --json が exit 1 を返し、stdout JSON に ok:false / failure_class:"cli_missing" / failure_reason を含む
  • AC2: agy バイナリの呼び出しは subprocess.run([agy_bin, ...], shell=False) で行われ、スクリプト内に shell=True が存在しない
  • AC3: mock agy --help に -p / --print / --prompt のいずれかが含まれる場合、help.noninteractive_flags に検出結果が記録される
  • AC4: mock agy --help に非対話実行フラグが一切ない場合、exit 1、failure_class:"cli_incompatible" になる
  • AC5: chat サブコマンドや --output-format が help に存在しても FAIL にしない。warnings または help.unexpected_capabilities に記録する
  • AC6: smoke check は isolated temp cwd、timeout=20 秒、shell=False で agy -p <SMOKE_PROMPT> を実行し、timeout 発生時は failure_class:"client_subprocess_timeout" として分類する
  • AC7: --json で stdout に agy_preflight_result/v1 JSON を出力し、--output-file 指定時は同じ JSON を保存する。成功時 exit 0、失敗時 exit 1
  • AC8: uv run pytest .claude/skills/gemini-cli-headless-delegation/tests/test_preflight_agy.py -q が pass する
  • AC9: 変更は Allowed Paths の 2 ファイルに限定する(docs / wrapper / setup_check の変更は別 Issue)

Verification Commands

# AC1
$ uv run pytest .claude/skills/gemini-cli-headless-delegation/tests/test_preflight_agy.py -k "test_cli_missing" -v
# AC2
$ uv run pytest .claude/skills/gemini-cli-headless-delegation/tests/test_preflight_agy.py -k "test_no_shell_invocation" -v
# AC3
$ uv run pytest .claude/skills/gemini-cli-headless-delegation/tests/test_preflight_agy.py -k "test_help_flag_detection" -v
# AC4
$ uv run pytest .claude/skills/gemini-cli-headless-delegation/tests/test_preflight_agy.py -k "test_missing_noninteractive_flags" -v
# AC5
$ uv run pytest .claude/skills/gemini-cli-headless-delegation/tests/test_preflight_agy.py -k "test_unexpected_capability" -v
# AC6
$ uv run pytest .claude/skills/gemini-cli-headless-delegation/tests/test_preflight_agy.py -k "test_smoke_timeout" -v
# AC7
$ uv run pytest .claude/skills/gemini-cli-headless-delegation/tests/test_preflight_agy.py -k "test_json_output" -v
# AC8
$ uv run pytest .claude/skills/gemini-cli-headless-delegation/tests/test_preflight_agy.py -q
# AC9
$ test -f .claude/skills/gemini-cli-headless-delegation/scripts/preflight_agy.py

Allowed Paths

  • .claude/skills/gemini-cli-headless-delegation/scripts/preflight_agy.py(新規作成)
  • .claude/skills/gemini-cli-headless-delegation/tests/test_preflight_agy.py(新規作成)

Stop Conditions

  • agy の実機仕様が -p フラグを持たない(実機確認必須、その場合は Notes を更新して人間判断)
  • Allowed Paths 外の変更が必要と判明した場合
  • preflight_gemini_headless.py の大規模リファクタリングが必要になった場合(別 Issue)
  • 新規 Issue の起票が必要と判断した場合
  • 外部サービス利用・権限昇格が必要になった場合
  • 既存テスト大規模改変が必要になった場合

Required Skills

  • Python scripting
  • subprocess / CLI 出力解析
  • pytest

Runtime Verification Applicability

applicability: deferred
decision: deferred
deferred_destination: "agy インストール環境(WSL2/OAuth 認証済み)での手動実行"
deferred_verification_condition: "agy インストール環境(WSL2/OAuth 認証済み)で手動実行"
reason: "agy 実機(WSL2/OAuth 環境)が必要。CI は mock ベース pytest のみ実行可能。実機検証は agy インストール環境で手動実行する。"

Notes

  • agy 実機仕様(2026-05-21 時点): コマンド名は agy(antigravity は存在しない)、サブコマンドは changelog / help / install / plugin / update のみ、chat サブコマンドは存在しない、単発実行は agy -p(--print / --prompt エイリアス)、--output-format は存在しない。
  • AGY_BIN 環境変数でバイナリパスを上書き可能にすること(テスト・CI での決定論的検証に必要)。
  • 本 Issue は C104-4(agy provider 追加)の前提となる。C104-4 は本 Issue 完了後に着手可能。
  • WSL2/OAuth 再認証問題は本 Issue スコープ外(GeminiCLI移譲スキルからAntigravity CLIへの段階的移行 #104 Remaining Parent Gaps 参照)。
  • AC1 VC(test_cli_missing)は subprocess 経由で main() を呼び、AGY_BIN=/nonexistent/agy を環境変数で渡して exit code・JSON 出力を検証する。
  • AC2 VC(test_no_shell_invocation)はスクリプトのソースを読み込み shell=True が存在しないことを確認する pytest テスト。

Activity

  1. squne121 commented on May 20, 2026

    @squne121
    OwnerAuthor

    create-issue transaction partial-failure

    Issue: #109
    Failure stage: label-apply
    Message: label-apply failed

    Requested:

    Failure context: command=gh issue edit --repo squne121/loop-protocol 109 --add-label phase/implementation,state/backlog
    output=failed to update #109: 'state/backlog' not found
    failed to update 1 issue
    matching_issue_numbers=[109]
    issue_number=109

    Recovery hint: stage 'label-apply' encountered an unexpected failure.
    Generic guidance: verify the issue state manually and re-run the transaction if safe.
    gh issue view 109 --repo squne121/loop-protocol --json number,title,labels,state
    idempotent re-run: depends on the specific failure

    Please recover deterministically before re-running the create issue transaction.

  2. squne121 commented on Jun 21, 2026

    @squne121
    OwnerAuthor

    判定: 差し戻し

    #109 は方向性は正しいです。ただし現状のまま実装 Issue として渡すと、「agy が PATH にある」ことは検出できても、「wrapper が安全に agy -p を呼べる」ことは保証できません。特に AC2、認証確認、JSON 契約、安全境界の定義が弱いです。#109 本文では preflight_agy.py と test_preflight_agy.py のみを Allowed Paths にしており、親 #104 の agy 移行前提を作る Issue と位置付けられていますが、Acceptance Criteria がその目的に届いていません。

    また、#109 の唯一のコメントは内容レビューではなく、state/backlog ラベルが存在せず label-apply に失敗した create-issue transaction partial-failure です。これは実装ブロッカーではありませんが、Issue 作成トランザクションの回復が未完了です。

    主要ブロッカー

    1. AC2 が自己矛盾している

    #109 本文は「chat サブコマンドが存在しないこと」「--output-format が存在しないこと」を検出し、あれば warn とする、と書いています。一方で AC2 は rg -n "chat|--output-format" preflight_agy.py がゼロ件であることを要求しています。これは実装上の矛盾です。文字列を一切書けないなら、診断名・警告文・テスト名・help 解析用 fixture にも書けません。結果として、実装者が検査を避けるか、難読化する誘因になります。

    修正: AC2 は grep ベースではなく、mock help に対する振る舞いで検証するべきです。

    uv run --with pytest python -m pytest \
      .claude/skills/gemini-cli-headless-delegation/tests/test_preflight_agy.py -q

    テストで見るべき条件は以下です。

    - help に -p / --print / --prompt のいずれかがある → ok
    - help に非対話実行フラグがない → cli_incompatible
    - help に chat がある → warning のみ
    - help に --output-format がある → warning または capability 記録のみ
    

    2. 「version check または dry-run」で認証確認、は曖昧すぎる

    Issue 本文は認証状態の確認を「agy -p "hello" dry-run または version check」としていますが、version check は通常、認証確認ではありません。既存の setup_check.py でも、Gemini CLI の認証は gemini --prompt "ok" --model ... の smoke prompt で確認しており、--version とは別扱いです。

    修正: agy --version は CLI 存在・バージョン検出、agy -p <SMOKE_PROMPT> は認証・非対話実行確認として分離してください。さらに timeout 必須です。agy -p がログイン待ち、ネットワーク待ち、TUI 起動、認証リダイレクト待ちで固まる可能性を潰す必要があります。

    推奨仕様:

    version:
      command: [AGY_BIN, "--version"]
      failure_class: cli_missing / version_failed
    
    help:
      command: [AGY_BIN, "--help"]
      failure_class: cli_incompatible
    
    smoke:
      command: [AGY_BIN, "-p", "Do not use tools. Reply with OK only."]
      timeout_sec: 20
      cwd: isolated temporary directory
      shell: false
      expected: exit 0 and stdout contains non-empty response
    

    3. JSON 契約が既存 preflight とズレている

    既存の preflight_gemini_headless.py は schema、ok、failure_reason、failure_class、recovery_action、version、help、smoke、warnings などの構造化 result を持っています。 さらに main() は --output-file または --json を要求し、ok に応じて exit code 0/1 を返します。

    #109 の JSON 例は {"ok": true/false, "agy_version": "...", "flags": [...], "warnings": [...]} 程度で、既存の failure taxonomy や caller の判断材料と接続していません。既存の failure taxonomy には cli_missing、cli_incompatible、auth_missing_or_expired、network_error、client_subprocess_timeout などが定義されています。

    修正: agy_preflight_result/v1 を明示し、少なくとも以下の shape にしてください。

    {
      "schema": "agy_preflight_result/v1",
      "ok": false,
      "failure_reason": null,
      "failure_class": null,
      "recovery_action": null,
      "agy": {
        "bin": "agy",
        "resolved_path": null,
        "version": null
      },
      "help": {
        "ok": false,
        "stdout_sample": "",
        "stderr_sample": "",
        "noninteractive_flags": {
          "-p": false,
          "--print": false,
          "--prompt": false
        },
        "missing_required_capability": []
      },
      "smoke": {
        "ok": false,
        "argv": [],
        "exit_code": null,
        "timed_out": false,
        "stdout_sample": "",
        "stderr_sample": ""
      },
      "warnings": []
    }

    4. 「-p がある」だけでは安全境界の preflight にならない

    既存の skill は、Gemini 側の file write、shell edit、GitHub mutation を禁止し、最終的な edit/shell/GitHub mutation は caller/orchestrator 側に残す設計です。 usage-contract.md でも tool_profile ごとに外部能力・禁止事項が定義されています。

    さらに #112 の ACP 安全境界調査では、approvalMode や MCP 注入、native tool registry が安全境界に直結することが確認されています。特に default は native tools が active、plan は比較的安全だが例外があり、user/admin MCP は transport 側から完全制御できない、という整理です。

    agy -p が存在しても、以下が未確認なら「安全に wrapper が呼べる」とは言えません。

    - 非対話実行時に tool / plugin / MCP / shell / file edit が有効か
    - cwd によって project settings や plugin が注入されるか
    - approval / sandbox / readonly / plan 相当のフラグがあるか
    - stdout が機械処理可能か、stderr と混ざるか
    - 認証切れ時に exit code / stderr が安定しているか
    

    #109 の Allowed Paths が preflight script/test のみであること自体は妥当ですが、その場合は「agy provider 実装の前提調査であり、wrapper 切替・契約更新・安全境界確定はしない」と明記すべきです。

    WEB 調査からの補強

    公式 Gemini CLI 側は gemini -p の非対話モード、--output-format json / stream-json、OAuth/API key/Vertex AI 認証、built-in tools、MCP、sandbox/trusted folders などが公開されています。つまり Gemini CLI は「CLI 引数」「認証」「tool 能力」「出力形式」が明示された前提で wrapper 化されています。([GitHub]1)

    一方、今回確認できた範囲では agy の -p / --print / --prompt / tool sandbox に関する公式一次資料は見つけられませんでした。TechRadar は Google が Gemini CLI 利用者を Antigravity CLI へ移行させ、1:1 parity ではないと報じていますが、これは仕様の一次情報ではありません。([TechRadar]2) したがって #109 は、Issue 本文の Notes にある 2026-05-21 時点の実機仕様を固定仕様として信じるのではなく、agy --help と smoke 実行の live probe を証跡として JSON に残す設計に寄せるべきです。

    また Gemini CLI 周辺では偽ツール・悪性パッケージ・typosquatting への注意喚起も報じられています。([IT Pro]3) agy についても、配布元・インストール方法・version/provenance を preflight が記録しないと、PATH 上の別バイナリを信用するリスクが残ります。

    修正版 Acceptance Criteria 案

    現 AC は以下に置き換えるのが安全です。

    ## Acceptance Criteria
    
    - [ ] AC1: `AGY_BIN=/nonexistent/agy uv run python3 .claude/skills/gemini-cli-headless-delegation/scripts/preflight_agy.py --json` が exit 1 を返し、JSON に `ok:false` / `failure_class:"cli_missing"` / `failure_reason` を含む。
    - [ ] AC2: `AGY_BIN` は shell 経由ではなく `subprocess.run([agy_bin, ...], shell=False)` で呼ばれ、空白を含む path でも argv として保持される。
    - [ ] AC3: mock `agy --help` に `-p` / `--print` / `--prompt` のいずれかが含まれる場合、`help.noninteractive_flags` に検出結果が記録される。
    - [ ] AC4: mock `agy --help` に非対話実行フラグが一切ない場合、exit 1、`failure_class:"cli_incompatible"` になる。
    - [ ] AC5: `chat` サブコマンドまたは `--output-format` が help に存在しても、それ自体では FAIL にしない。`warnings` または `help.unexpected_capabilities` に記録する。
    - [ ] AC6: smoke check は isolated temp cwd、timeout 付き、`shell=False` で `agy -p <SMOKE_PROMPT>` を実行し、timeout は `failure_class:"client_subprocess_timeout"` として分類する。
    - [ ] AC7: `--json` で stdout に machine-readable JSON を出し、`--output-file` 指定時は同じ JSON を保存する。成功時 exit 0、失敗時 exit 1。
    - [ ] AC8: `uv run --with pytest python -m pytest .claude/skills/gemini-cli-headless-delegation/tests/test_preflight_agy.py -q` が pass する。
    - [ ] AC9: 変更は Allowed Paths の 2 ファイルに限定する。docs / wrapper / setup_check の変更が必要なら別 Issue とする。

    実装方針

    既存テストは importlib で script module を読み込み、fake _run / fake CompletedProcess を差し替えるパターンになっています。 新規 test_preflight_agy.py もこの形式に揃えるのが最小摩擦です。既存の command-not-found テストは exit 1、JSON 保存、failure_reason、stdout summary まで見ています。

    最小の実装構造はこれで足ります。

    preflight_agy.py
    - AGY_BIN = os.environ.get("AGY_BIN", "agy")
    - _run(argv, cwd=None, timeout=...)
    - _resolve_binary()
    - _run_version()
    - _run_help()
    - _parse_help_capabilities(help_text)
    - _run_smoke()
    - run_preflight()
    - main(--json, --output-file, --compact)
    

    重要なのは、chat や --output-format を「存在したら失敗」にしないことです。将来 agy が chat や JSON 出力を追加しても、wrapper がそれを勝手に前提化しない限り破壊的ではありません。失敗条件は「agy -p 相当の非対話実行能力が検出できない」「smoke が安全に完了しない」「認証/timeout/CLI missing を分類できない」です。

    最終コメント案

    差し戻し推奨です。#109 の目的は妥当ですが、現 AC のままだと `agy` の存在確認に近く、parent #104 の「wrapper が安全に agy -p を呼べる前提」までは保証できません。
    
    Blocker:
    1. AC2 の `rg "chat|--output-format" == 0` は本文の「存在しないことを warn で検出」と矛盾しています。grep ではなく mock help に対する振る舞いで検証してください。
    2. `version check` は認証確認ではありません。`agy --version` と `agy -p <SMOKE_PROMPT>` を分け、smoke は timeout / isolated cwd / shell=False 必須にしてください。
    3. JSON は既存 preflight と同様に `schema`, `ok`, `failure_reason`, `failure_class`, `version/help/smoke`, `warnings` を持つ安定 shape にしてください。
    4. `-p` の存在だけでは tool / plugin / MCP / cwd / sandbox 安全境界は確認できません。本 Issue は provider 実装ではなく capability probe に限定し、wrapper 切替や docs/contract 更新は follow-up に分けるべきです。
    5. `AGY_BIN` は shell に渡さず argv として扱い、PATH/provenance/version/help/smoke の結果を JSON に残してください。
    
    AC は grep ではなく pytest に寄せ、missing binary、help alias、missing `-p`、help stderr、chat/output-format diagnostic、smoke success/auth fail/timeout、main exit code を網羅する形に修正するのが安全です。

    この修正後なら、#109 は「agy provider 実装前の deterministic preflight foundation」として切れます。現状のままなら、実装者が AC を満たしても親 #104 の安全な移行前提は成立しません。

  3. squne121 commented on Jun 21, 2026

    @squne121
    OwnerAuthor

    Refinement Loop: Approved

    The issue has been approved and is ready for implementation.

  4. squne121 commented on Jun 21, 2026

    @squne121
    OwnerAuthor

    Contract Review Result

    CONTRACT_REVIEW_RESULT_V1:
      status: go
      generated_at: "2026-06-21T23:37:15Z"
      generated_by: issue-contract-review
      issue_url: https://github.com/squne121/loop-protocol/issues/109
      body_sha256: "sha256:af38ca7357f529ebeede0f31fd357ceeef8ba9da36d0c5e5ca519490dbd023e3"
      checks:
        readiness: go
        blockers: pass
        product_spec: pass
        vc_preflight: pass
      source: ensure_contract_snapshot_auto
  5. squne121 commented on Jun 22, 2026

    @squne121
    OwnerAuthor

    test

  6. added a commit that references this issue on Jun 22, 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

    phase/implementation

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions