Skip to main content

A small Codex-planned, Cursor-implemented TDD loop with Codex hook guards.

Project description

AiTdd

Codex が計画とレビューを担当し、Cursor が RED / GREEN / REFACTOR の実装を担当する、 小さな TDD オーケストレータです。Hook の入出力と policy は codex-hookkit を使います。 Codex は Python から公式 @openai/codex-sdk を呼びます。 Cursor は既定で Python から公式 @cursor/sdk の Composer を使い、 必要なら cursor-agent CLI に切り替えられます。

役割

  • Codex: 次に書くべき最小テストの計画、GREEN 後と REFACTOR 後のレビュー、完了判定
  • Cursor: テスト追加、最小実装、リファクタリング
  • Hook: RED ではテスト失敗を要求し、GREEN / REFACTOR ではテスト成功を要求する

使い方

uv sync --dev
npm install
uv run aitdd run "FizzBuzz を t-wada 流 TDD で作る" --test-command "pytest" --max-cycles 5

PyPI から使う場合も、公式 Node SDK は作業ディレクトリに入れてください。

pip install aitdd
npm install @openai/codex-sdk @cursor/sdk
aitdd run "FizzBuzz を t-wada 流 TDD で作る" --test-command "pytest"

Cursor 実装担当は既定で SDK + Composer alias を使います。

uv run aitdd run "..." --cursor-backend sdk --cursor-model composer-latest

CLI fallback を使いたい場合:

uv run aitdd run "..." --cursor-backend cli --cursor-model composer-latest

SDK は CURSOR_API_KEY があればそれを使い、無い場合は SDK 側の認証解決に任せます。

実行内容だけ確認する場合:

uv run aitdd run "TODO アプリの最小モデルを作る" --dry-run

複雑な要件は先に aitdd plan で spec draft を作れます。Codex が計画だけを担当し、 実装はしません。

uv run aitdd plan "Money クラスを作りたい" --output aitdd.yaml
uv run aitdd run --spec aitdd.yaml --test-command "pytest"

既存ファイルは --force なしでは上書きしません。dry-run で形だけ確認することもできます。

uv run aitdd plan "Money クラスを作りたい" --output aitdd.yaml --dry-run

複雑なクラスや業務要件では aitdd.yaml を使って、反復する TDD サイクルを固定できます。

uv run aitdd run "ignored when spec exists" \
  --spec examples/aitdd.yaml \
  --test-command "pytest" \
  --max-cycles 5

aitdd.yaml では次を指定できます。

  • goal: 全体ゴール
  • public_api: 育てる public API
  • constraints: 設計・進め方の制約
  • forbidden: 先回り実装や禁止事項
  • acceptance_tests: 外側の受け入れテスト
  • unit_tests: 内側のユニットテスト
  • cycles: 1 サイクル 1 public behavior の反復リスト

各 cycle には expected_red を置けます。RED では「テストが失敗したか」だけでなく、 期待した理由で失敗したか、禁止した壊れ方をしていないかも検証します。 REFACTOR フェーズではテストファイル変更を拒否します。

Codex レビューは JSON schema で機械判定します。各サイクルで次の gate がすべて true の場合だけ 次に進みます。

  • one_behavior_only
  • minimal_green
  • tests_unchanged_in_refactor
  • acceptance_unit_boundary_ok
  • forbidden_respected
  • needs_more_tests
  • missing_test_perspectives

CLI には cycle ごとの進捗として red / green / refactor / complete / one_behavior_only / minimal_green / boundary_ok が表示されます。

Codex レビューで「まだテスト観点が足りない」と判断した場合、gate は通しつつ missing_test_perspectives に不足観点を返します。AiTdd はそれを .aitdd/progress.jsontest_backlog に保存し、次の cycle で RED 候補として優先します。これにより、実装中に見つかった 境界値、例外系、責務分離などの観点も、あとからまとめてではなく TDD のリズムのまま追加できます。

REFACTOR 後には Codex Follow Up フェーズを実行します。ここでは「足りていない要件はないか」 「追加でテストした方がいい観点はないか」を振り返り、不足要件は requirements_backlog、 追加テスト観点は test_backlog に積みます。どちらかが残っている場合、完了扱いにせず 次の RED cycle に進みます。

Follow Up で「AI が勝手に決めてはいけない」と判断した場合は Clarification Gate で止まります。 質問は questions_for_user に保存され、cycle は needs_user_input になります。ユーザーの回答を spec や backlog に反映してから resume します。

実行中の状態は最小構成で次に保存されます。

  • .aitdd/progress.json: cycle ごとの behavior, red, green, refactor, review_gate, follow_up, issues, started_at, finished_attest_backlog / requirements_backlog / questions_for_user
  • .aitdd/cycles/001-red.diff: phase ごとの git diff snapshot
  • .aitdd/report.md: TDD の進行ログ

途中から再開する場合:

aitdd resume --spec aitdd.yaml --max-cycles 5

Codex hook として使う場合は .codex/hooks.json などに次を登録します。

{
  "hooks": {
    "PostToolUse": [
      {
        "matcher": "*",
        "hooks": [
          {
            "type": "command",
            "command": "uv run python hooks/aitdd_guard.py",
            "timeout": 30
          }
        ]
      }
    ]
  }
}

AITDD_PHASE=red|green|refactorAITDD_TEST_COMMAND を渡すと、そのフェーズの 期待に合わない状態を block します。

ループ

  1. Codex が次の最小ステップを計画する
  2. Cursor が RED として、失敗するテストだけを書く
  3. テストが失敗しなければ RED をやり直す
  4. Cursor が GREEN として、通すための最小実装を書く
  5. テストが通らなければ GREEN をやり直す
  6. Codex がレビューし、不足テスト観点があれば test_backlog に積む
  7. Cursor が必要なら REFACTOR する
  8. Codex Follow Up が要件漏れと追加テスト観点を振り返る
  9. ユーザー判断が必要なら Clarification Gate で止める
  10. backlog があれば次の RED に進む
  11. backlog が空で完了条件を満たしたら Codex が完了判定する

--max-cycles は安全弁です。完璧を目指しつつ、暴走しないように上限を持たせています。

詳しい設計は docs/architecture.md にまとめています。

Self Dogfood

AiTDD 自身の機能開発も同じ流れで進めます。入口になる spec は examples/aitdd-self.yaml です。

uv run aitdd run \
  --spec examples/aitdd-self.yaml \
  --test-command "uv run pytest" \
  --max-cycles 3

実際の実装前にプロセスだけ確認する場合:

uv run aitdd run \
  --spec examples/aitdd-self.yaml \
  --test-command "uv run pytest" \
  --max-cycles 1 \
  --dry-run

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

aitdd-0.1.6.tar.gz (44.2 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

aitdd-0.1.6-py3-none-any.whl (25.7 kB view details)

Uploaded Python 3

File details

Details for the file aitdd-0.1.6.tar.gz.

File metadata

  • Download URL: aitdd-0.1.6.tar.gz
  • Upload date:
  • Size: 44.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.14.2

File hashes

Hashes for aitdd-0.1.6.tar.gz
Algorithm Hash digest
SHA256 549da90adbe97c1025e094eeabfff7587ac884830c2c0f189c51f51811ef42f6
MD5 2e5a5e8575477e499c6e2bb53bb40fd7
BLAKE2b-256 73ae656d51334dd760e8e7f3b38939fa1ca92c800c976d55125f1eff6bf6bf18

See more details on using hashes here.

File details

Details for the file aitdd-0.1.6-py3-none-any.whl.

File metadata

  • Download URL: aitdd-0.1.6-py3-none-any.whl
  • Upload date:
  • Size: 25.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.14.2

File hashes

Hashes for aitdd-0.1.6-py3-none-any.whl
Algorithm Hash digest
SHA256 832f9ddbb12387da35bf8593af3cb069afa31bbdeb8ece80c38610366bca1bc2
MD5 6b8166f03fd2a4ffc8393255cca38154
BLAKE2b-256 648e0807d0d3e94a2b4c84361a89f4cc436b08cedfbc7b6ec4c821197bf9b4ad

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page