## Context
If @.ralph/guides/AGENTS.md exists, study it for commands and patterns.
Study @.ralph/specs/$FEATURE.md for feature specification.
Study @.ralph/specs/$FEATURE-implementation-plan.md for completed tasks.

## Learnings
Read @.ralph/LEARNINGS.md for patterns from previous features.
Capture any review feedback patterns for future iterations.

## Task
All implementation and E2E tasks are complete. Create PR and request review.
Complete ALL steps in a single pass — do not end the session between steps.

### Step 0: Verify Spec Requirements
Before creating the PR, verify the implementation meets the spec requirements and update spec files.

1. Read @.ralph/specs/$FEATURE.md and for each requirement under "## Requirements":
   - Check if it was implemented (review implementation plan tasks)
   - Mark as `[x]` if complete, leave as `[ ]` if not implemented (add a note explaining why)

2. For each item under "## Acceptance Criteria":
   - Verify against implementation and test results
   - Mark as `[x]` if verified, leave as `[ ]` with a note if not met

3. Update spec status:
   - Change `**Status:** Planned` (or `Draft`) to `**Status:** Completed` if all requirements met, or `**Status:** Partial` if some are missing
   - Update `**Last Updated:**` to today's date

4. Update @.ralph/specs/README.md Active Specs table:
   - Update ONLY the row for $FEATURE (do NOT remove or modify other rows)
   - Change status and update Last Updated date

5. If any requirements were not fully met, add an "## Implementation Notes" section documenting gaps and reasons.

6. Commit spec updates:
   ```bash
   git -C {{appDir}} add ../.ralph/specs/
   git -C {{appDir}} commit -m "docs($FEATURE): verify spec requirements"
   git -C {{appDir}} push origin feat/$FEATURE
   ```

### Step 1: Verify Ready State
1. Check all tasks are complete in implementation plan (no `- [ ]` items)
2. Verify tests pass: `cd {{appDir}} && {{testCommand}}`
3. Verify build succeeds: `cd {{appDir}} && {{buildCommand}}`

If any fail, fix before proceeding.

### Step 2: Check Git Status
```bash
cd {{appDir}} && git status
cd {{appDir}} && git log --oneline -5
```

Ensure:
- On branch `feat/$FEATURE`
- All changes are committed
- Branch is pushed to remote

If uncommitted changes exist:
```bash
git -C {{appDir}} add -A && git -C {{appDir}} commit -m "chore($FEATURE): final cleanup"
git -C {{appDir}} push origin feat/$FEATURE
```

### Step 3: Create PR
Check if PR already exists:
```bash
cd {{appDir}} && gh pr list --head feat/$FEATURE
```

If no PR exists, create one:
```bash
cd {{appDir}} && gh pr create --base main --head feat/$FEATURE \
  --title "feat($FEATURE): [read description from spec]" \
  --body "$(cat <<'EOF'
## Summary
[Read from spec Purpose section]

## Changes
[Read from implementation plan - list completed phases]

## Testing
- [x] Unit/integration tests: passing
- [x] E2E tests: All scenarios passed
- [x] Build succeeds

## E2E Test Results
[Copy from implementation plan E2E section]

Closes #[Read the source issue number from the spec file metadata or context section]

Generated with Wiggum Loop
EOF
)"
```

### Step 4: Run Automated Review

Use the current review CLI session (`$REVIEW_CLI`) to perform a full review:

1. Review `git diff main` for:
   - Code quality and consistency
   - Test coverage and missing tests
   - Potential bugs and edge cases
   - Security risks (injection, auth, data exposure)
   - Performance and error handling
2. Post review feedback to the PR with `gh pr comment --body ...`
3. Print your final verdict as the **LAST** line of stdout, exactly one of:
   - `VERDICT: APPROVED`
   - `VERDICT: NOT APPROVED`
4. Before printing `VERDICT: APPROVED`, wait for CI to complete and pass:
   - Run `cd {{appDir}} && gh pr checks feat/$FEATURE --watch --interval 10`
   - If checks are pending, keep waiting
   - If any check fails, output `VERDICT: NOT APPROVED` and list failing checks

**Handle review feedback:**
- If verdict is `VERDICT: APPROVED` -> Done. The PR is ready for manual merge by the user.
- If verdict is `VERDICT: NOT APPROVED`:
  1. Address each issue with code fixes
  2. Commit: `git -C {{appDir}} add -A && git -C {{appDir}} commit -m "fix($FEATURE): address review feedback"`
  3. Push: `git -C {{appDir}} push origin feat/$FEATURE`
  4. Re-run this review step
- Max 3 review iterations before requiring manual intervention

### Step 5: Final Summary
After review is complete (approved or max iterations reached):
1. Post a summary comment on the PR with the review outcome
2. Do NOT merge — the user will review and merge manually

## IMPORTANT: Review scope
If you discover that no implementation code exists (empty diff, no source files changed),
do NOT implement the feature yourself. Instead, report "VERDICT: NOT APPROVED —
no implementation found" so the harness can trigger a new implementation iteration.

## Rules
- **NEVER approve if any tests are failing.** Output "VERDICT: NOT APPROVED — test failures" if any tests fail.
- **NEVER approve while CI checks are pending/failing.** Wait until checks complete and pass.
- Do NOT merge the PR — auto mode only reviews, the user merges
- Do NOT implement missing features — only review and fix minor issues
- Address ALL review comments before marking as approved
- If gh CLI fails, check authentication: `gh auth status`
- Keep review conversation focused and professional

## Troubleshooting
- **gh: command not found** -> Install GitHub CLI: `brew install gh`
- **gh auth error** -> Run: `gh auth login`
- **PR already exists** -> Use: `gh pr view` to see status
- **Review CLI missing** -> Install the configured `$REVIEW_CLI` binary and retry

## Learning Capture
If the review revealed patterns worth remembering, append to @.ralph/LEARNINGS.md:
- Code quality feedback -> Add under "## Anti-Patterns" or "## Patterns"
- Common review issues -> Add under "## Anti-Patterns"
- Good practices identified -> Add under "## Patterns"

Format: `- [YYYY-MM-DD] [$FEATURE] Brief description`
