# Loop iteration prompt (Mode B autonomous)

This is the single self-contained prompt that `/loop` re-fires until the queue empties. Each iteration handles exactly one PR end-to-end.

---

You are running one iteration of the autonomous plan-execution loop. The queue lives in Gitea at `dohertj2/lmxopcua`. Helpers: `scripts/queue/*.sh`.

## Step 1 — pick the next PR
Run `bash scripts/queue/next-pr.sh`. It returns JSON.
- If `{"empty": true}` → the queue is drained. **Do not call ScheduleWakeup.** Report "queue empty — loop terminating" and exit. The /loop will end.
- Otherwise parse: `issue_num`, `canonical_id`, `driver`, `phase`, `plan_pr_id`, `branch`, `title`, `url`.

## Step 2 — claim it
Run `bash scripts/queue/start-pr.sh "$ISSUE_NUM" "$BRANCH"`. This swaps `queue/queued` → `queue/in-progress` and creates the branch off `auto/driver-gaps`.

## Step 3 — pull the issue body
Run `curl -sf -H "Authorization: token $(awk '/token:/{print $2}' "$LOCALAPPDATA/tea/config.yml")" "https://gitea.dohertylan.com/api/v1/repos/dohertj2/lmxopcua/issues/$ISSUE_NUM"` and extract the `body` field. The body contains the Plan link, summary, source files, docs/fixture/e2e files.

## Step 4 — implement on a worktree
Dispatch a general-purpose Agent with `isolation: "worktree"`. Brief it with:
- the issue body verbatim
- the linked plan section (read `docs/plans/<driver>-plan.md` and quote the relevant per-PR detail)
- explicit instructions: implement the source-file changes, the doc updates, the fixture extensions, and the e2e test additions named in the issue
- run `dotnet build c:/Users/dohertj2/Desktop/lmxopcua/ZB.MOM.WW.OtOpcUa.slnx` until green
- run `dotnet test` for the relevant test project until green
- commit on `$BRANCH` with message `Auto: <canonical_id> — <short summary>` followed by `Closes #$ISSUE_NUM`
- return a brief summary of what changed

## Step 5 — verify and push
Verify the agent did commit + push. If branch isn't pushed, push it: `git push origin "$BRANCH"`.

## Step 6 — open PR
Build a body file: include the issue summary + the agent's summary. Then:
```
PR_NUM=$(bash scripts/queue/open-pr.sh "$ISSUE_NUM" "$BRANCH" "$TITLE" /tmp/pr-body.md)
```

## Step 7 — auto-merge (Mode B)
Run `bash scripts/queue/merge-pr.sh "$PR_NUM"`.

## Step 8 — close issue
Run `bash scripts/queue/finish-pr.sh "$ISSUE_NUM" success "$PR_NUM"`.

## On failure
If anywhere from Step 4 onward fails (build red, tests red, agent gives up, push fails, merge conflict):
- write the failure log to `/tmp/loop-fail-$ISSUE_NUM.log`
- run `bash scripts/queue/finish-pr.sh "$ISSUE_NUM" failed /tmp/loop-fail-$ISSUE_NUM.log`
- the issue keeps `queue/failed` and stays open for retry
- **do not** retry the same issue this iteration; let the loop pick a different one next fire

## Re-arm
At the very end of the iteration (success OR failure), call `ScheduleWakeup` with the same `/loop` prompt and `delaySeconds: 60` to fire the next iteration.

If the queue was empty in Step 1, do NOT call ScheduleWakeup.

Report a one-line summary to the user before re-arming.
