Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .claude/agents/implementation-worker.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ tools:
- MultiEdit
# Bash 制約: pnpm typecheck / lint / test / build と
# .claude/skills/*/scripts/ 配下のスクリプト実行に限定。
# 例外: gh api -X PUT repos/{owner}/{repo}/pulls/{pull_number}/update-branch(update_branch contract 実行 — #453)
# git push / gh pr create は open-pr skill 経由のみ。
model: sonnet
permissionMode: acceptEdits
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,18 @@ reviews と comments を時系列で結合してから最新 1 件を取得す
```bash
VERDICT=$(echo "$LATEST_VERDICT_BODY" | grep -E "^[[:space:]]*verdict:" | head -n1 | sed -E 's/.*verdict:[[:space:]]*//; s/[[:space:]]*$//')
MERGEABLE=$(echo "$LATEST_VERDICT_BODY" | grep -E "^[[:space:]]*mergeable:" | head -n1 | sed -E 's/.*mergeable:[[:space:]]*//; s/[[:space:]]*$//')
MERGE_STATE_STATUS=$(echo "$LATEST_VERDICT_BODY" | grep -E "^[[:space:]]*mergeStateStatus:" | head -n1 | sed -E 's/.*mergeStateStatus:[[:space:]]*//; s/[[:space:]]*$//')
# mergeStateStatus と merge_state_status の両形式に対応(dependency: Issue #56 snake_case 統一完了後は merge_state_status のみに統一する)
MERGE_STATE_STATUS=$(echo "$LATEST_VERDICT_BODY" | \
grep -E "^[[:space:]]*(mergeStateStatus|merge_state_status):" | \
head -n1 | sed -E 's/^[[:space:]]*(mergeStateStatus|merge_state_status):[[:space:]]*//; s/[[:space:]]*$//')
REVIEWED_HEAD_SHA=$(echo "$LATEST_VERDICT_BODY" | grep -E "^[[:space:]]*reviewed_head_sha:" | head -n1 | sed -E 's/.*reviewed_head_sha:[[:space:]]*//; s/[[:space:]]*$//')
RECOMMENDATIONS=$(echo "$LATEST_VERDICT_BODY" | grep -E "^[[:space:]]*recommendations:" | head -n1 | sed -E 's/.*recommendations:[[:space:]]*//; s/[[:space:]]*$//')
```

- 各 `head -n1` でコメント本文全体での最初の出現を採用(重複行記載は禁止だが防御として最初を採る)
- 値が空 → LOOP_VERDICT 不正として `human_review_required` で停止
- `RECOMMENDATIONS` の有効値は `[]` または `[update_branch]` のみ。それ以外の値(空文字を除く)は unknown recommendation として `human_escalation` で停止する
- `MERGE_STATE_STATUS` は `mergeStateStatus`(camelCase)と `merge_state_status`(snake_case)の両形式を吸収する(dependency: Issue #56 snake_case 統一)

## reviewed_head_sha 整合確認

Expand Down Expand Up @@ -68,11 +72,32 @@ CURRENT_HEAD=$(gh pr view "$PR_NUMBER" --json headRefOid --jq .headRefOid)

`APPROVE + MERGEABLE + BEHIND`(`recommendations: [update_branch]` 含む)の場合:

1. `implementation-worker` に `gh pr update-branch` の実行を委譲する(具体的な実行手順は implementation-worker 側)
2. 委譲成功後: 既存の TEST_VERDICT / LOOP_VERDICT は headRefOid が変わるため stale とみなす
3. Step 2(test-runner)→ Step 4(pr-review-judge)を再実行し、新しい reviewed_head_sha の LOOP_VERDICT で Step 5 を再判定する
4. 委譲失敗時: `termination_reason: human_escalation` を記録して停止する
5. 更新後に `mergeable=CONFLICTING` または `mergeStateStatus=DIRTY` を検出した場合: `CONFLICTING PR Escalation Runbook` を発動する
1. `UPDATE_BRANCH_REQUEST_V1` を組み立てる:

```yaml
UPDATE_BRANCH_REQUEST_V1:
repo: <REPO>
pr_number: <PR_NUMBER>
expected_head_sha: <REVIEWED_HEAD_SHA>
update_method: merge_only
caller: impl-review-loop.step-5
```

2. `implementation-worker` に `UPDATE_BRANCH_REQUEST_V1` を渡して委譲する。
実行手順(`gh api -i -X PUT`、202 poll loop、422/403 分岐)は `implement-issue` SKILL.md の `## update_branch Contract` セクションを参照。

3. `UPDATE_BRANCH_RESULT_V1` を受け取り、`status` で分岐する:

| status | 次アクション |
|---|---|
| `ok` | stale 判定 → Step 2(test-runner)→ Step 4(pr-review-judge)→ Step 5 再実行 |
| `stale_verdict` | Step 4(pr-review-judge)re-review → Step 5 再実行 |
| `forbidden` | `termination_reason: human_escalation` を記録して停止 |
| `validation_failed` | `termination_reason: human_escalation` を記録して停止 |
| `timeout` | `termination_reason: human_escalation` を記録して停止 |
| `human_escalation` | 停止して人間判断を仰ぐ |

4. 更新後に `mergeable=CONFLICTING` または `mergeStateStatus=DIRTY` を検出した場合: `CONFLICTING PR Escalation Runbook` を発動する

## 出力

Expand Down
114 changes: 114 additions & 0 deletions .claude/skills/implement-issue/SKILL.md
Original file line number Diff line number Diff line change
Expand Up @@ -216,3 +216,117 @@ IMPLEMENT_RESULT_V1:

`docs/dev/agent-skill-boundaries.md#OUTPUT_BUDGET_V1` の制約に従う。routing-critical な機械可読フィールドは削らず、人間向け説明・証跡・diff 再掲のみを削減する。
`IMPLEMENT_RESULT_V1` の全フィールドは必ず含める(routing 必須フィールド)。

## update_branch Contract

`impl-review-loop` Step 5 の BEHIND 分岐から呼び出される `update_branch` 実行手順の contract。

### UPDATE_BRANCH_REQUEST_V1

```yaml
UPDATE_BRANCH_REQUEST_V1:
pr_number: <int> # 対象 PR 番号
repo: <owner/repo> # 例: squne121/loop-protocol
expected_head_sha: <sha> # Step 4 の reviewed_head_sha(race guard 用)
update_method: merge_only # 固定値。GraphQL/rebase は out-of-scope(follow-up issue で対応)
caller: <string> # 呼び出し元識別子(例: impl-review-loop.step-5)
```

`update_method: merge_only` は REST `PUT /repos/{owner}/{repo}/pulls/{pull_number}/update-branch` の merge update 固定を表す。GraphQL `updatePullRequestBranch` mutation および rebase update は本 contract の Out of Scope — 別 Issue で対応する。

### UPDATE_BRANCH_RESULT_V1

```yaml
UPDATE_BRANCH_RESULT_V1:
status: ok | failed | stale_verdict | forbidden | validation_failed | human_escalation | bounded_backoff_or_human_escalation | timeout
update_method: merge_only # リクエストの update_method を echo(検証用)
http_status: 202 | 403 | 422 | <other>
new_head_sha: <sha> # 202 + poll 成功時のみ(head 更新後の headRefOid)
permission_diagnostics: # 403 時のみ
auth_actor: <string>
head_repo: <owner/repo>
base_repo: <owner/repo>
fork_pr: true | false
maintainer_can_modify: true | false
required_permissions: <string>
error_body: <string> # 422 その他エラー時の body(分類根拠の記録用)
```

### 呼び出し形式

```bash
set -euo pipefail

REPO=$(git remote get-url origin | sed 's/.*github.com[:/]//' | sed 's/\.git$//')
PR_NUMBER=<番号>
EXPECTED_HEAD_SHA=<reviewed_head_sha>

# gh api -i でヘッダと body を切り分ける
# 出力形式: HTTP/X.X <status> <reason>\n<headers>\n\n<body>
UPDATE_RESPONSE=$(gh api -i -X PUT "repos/$REPO/pulls/$PR_NUMBER/update-branch" \
-H "Accept: application/vnd.github+json" \
-H "X-GitHub-Api-Version: 2022-11-28" \
-f expected_head_sha="$EXPECTED_HEAD_SHA" 2>&1 || true)

# HTTP status 行を抽出
HTTP_STATUS=$(echo "$UPDATE_RESPONSE" | head -n1 | grep -oE '[0-9]{3}' | head -n1)
# body 部分(空行以降)を抽出
RESPONSE_BODY=$(echo "$UPDATE_RESPONSE" | awk '/^\r?$/{found=1; next} found{print}')
```

`gh pr update-branch` は使用しない(`expected_head_sha` オプションがないため)。

本 contract は **REST merge update 固定**(`update_method: merge_only`)。linear history またはリベース必須リポジトリは out-of-scope — 403 または validation failure 時は `human_escalation` とする。GraphQL `updatePullRequestBranch` mutation および rebase update は本 contract 対象外(Out of Scope)。

### HTTP ステータス別分岐

**202 Accepted:**

update が受け付けられた。headRefOid が `EXPECTED_HEAD_SHA` から変化するまで poll する(bounded retry: 5 秒 × 最大 12 回):

```bash
POLL_MAX=12
POLL_INTERVAL=5
UPDATED=false # 決定論的フラグ(空文字誤認防止)

for i in $(seq 1 $POLL_MAX); do
NEW_HEAD=$(gh pr view "$PR_NUMBER" --json headRefOid --jq .headRefOid)
if [ -n "$NEW_HEAD" ] && [ "$NEW_HEAD" != "$EXPECTED_HEAD_SHA" ]; then
UPDATED=true
break
fi
sleep $POLL_INTERVAL
done
```

- `UPDATED=true` → `UPDATE_BRANCH_RESULT_V1.status: ok`、`new_head_sha: $NEW_HEAD` を記録
- `UPDATED=false`(bounded retry 上限到達)→ `status: timeout`(`human_escalation` に相当)

**403 Forbidden:**

権限不足またはフォーク PR の書き込み制限。以下の `permission_diagnostics` を出力して `status: forbidden`(`human_escalation` に相当)とする:

```bash
# auth_actor の確認
gh api user --jq .login

# PR の fork / maintainer_can_modify 確認
gh pr view "$PR_NUMBER" --json headRepository,maintainerCanModify \
--jq '{head_repo: .headRepository.nameWithOwner, maintainer_can_modify: .maintainerCanModify}'
```

403 時は `UPDATE_BRANCH_RESULT_V1.permission_diagnostics` に auth_actor、head_repo、base_repo、fork_pr、maintainer_can_modify、required_permissions を含めて記録する。

**422 Unprocessable Entity:**

body 内容で分類する(422 全体を `expected_head_sha` mismatch とは断定しない):

| body の内容 | status |
|---|---|
| `expected_head_sha` mismatch | `stale_verdict` — Step 4 re-review 後 Step 5 再実行 |
| secondary rate limit | `bounded_backoff_or_human_escalation` — 指数バックオフ最大 3 回、上限で `human_escalation` |
| その他 validation failure | `validation_failed`(`human_escalation` に相当) |

### Bash 許可例外

`gh api -X PUT repos/{owner}/{repo}/pulls/{pull_number}/update-branch` の実行は `implementation-worker`(`.claude/agents/implementation-worker.md`)に許可された Bash 操作例外に含まれる。
Loading