description = """
Merge queue processor patrol loop.

The Refinery is the Engineer in the engine room. You process polecat branches, merging them to main one at a time with sequential rebasing.

**The Scotty Test**: Before proceeding past any failure, ask yourself: \"Would Scotty walk past a warp core leak because it existed before his shift?\""""
formula = "mol-refinery-patrol"
version = 1

[[steps]]
description = """
Check mail for MR submissions, escalations, messages.

```bash
gt mail inbox
# Process any urgent items
```

Handle shutdown requests, escalations, and status queries."""
id = "inbox-check"
title = "Check refinery mail"

[[steps]]
description = """
Fetch remote and identify polecat branches waiting.

```bash
git fetch origin
git branch -r | grep polecat
gt refinery queue <rig>
```

If queue empty, skip to context-check step. Track branch list for this cycle."""
id = "queue-scan"
needs = ["inbox-check"]
title = "Scan merge queue"

[[steps]]
description = """
Pick next branch. Rebase on current main.

```bash
git checkout -b temp origin/<polecat-branch>
git rebase origin/main
```

If rebase conflicts and unresolvable:
- git rebase --abort
- Notify polecat to fix and resubmit
- Skip to loop-check for next branch"""
id = "process-branch"
needs = ["queue-scan"]
title = "Process next branch"

[[steps]]
description = """
Run the test suite.

```bash
go test ./...
```

Track results: pass count, fail count, specific failures."""
id = "run-tests"
needs = ["process-branch"]
title = "Run test suite"

[[steps]]
description = """
**VERIFICATION GATE**: This step enforces the Beads Promise.

If tests PASSED: This step auto-completes. Proceed to merge.

If tests FAILED:
1. Diagnose: Is this a branch regression or pre-existing on main?
2. If branch caused it:
   - Abort merge
   - Notify polecat: \"Tests failing. Please fix and resubmit.\"
   - Skip to loop-check
3. If pre-existing on main:
   - Option A: Fix it yourself (you're the Engineer!)
   - Option B: File a bead: bd create --type=bug --priority=1 --title=\"...\"

**GATE REQUIREMENT**: You CANNOT proceed to merge-push without:
- Tests passing, OR
- Fix committed, OR
- Bead filed for the failure

This is non-negotiable. Never disavow. Never \"note and proceed.\""""
id = "handle-failures"
needs = ["run-tests"]
title = "Handle test failures"

[[steps]]
description = """
Merge to main and push immediately.

```bash
git checkout main
git merge --ff-only temp
git push origin main
git branch -d temp
git branch -D <polecat-branch>  # Local delete (branches never go to origin)
```

Main has moved. Any remaining branches need rebasing on new baseline."""
id = "merge-push"
needs = ["handle-failures"]
title = "Merge and push to main"

[[steps]]
description = """
More branches to process?

If yes: Return to process-branch with next branch.
If no: Continue to generate-summary.

Track: branches processed, branches skipped (with reasons)."""
id = "loop-check"
needs = ["merge-push"]
title = "Check for more work"

[[steps]]
description = """
Summarize this patrol cycle.

Include:
- Branches processed (count, names)
- Test results (pass/fail)
- Issues filed (if any)
- Branches skipped (with reasons)
- Any escalations sent

This becomes the digest when the patrol is squashed."""
id = "generate-summary"
needs = ["loop-check"]
title = "Generate handoff summary"

[[steps]]
description = """
Check own context usage.

If context is HIGH (>80%):
- Write handoff summary
- Prepare for burn/respawn

If context is LOW:
- Can continue processing"""
id = "context-check"
needs = ["generate-summary"]
title = "Check own context limit"

[[steps]]
description = """
End of patrol cycle decision.

If queue non-empty AND context LOW:
- Burn this wisp, start fresh patrol
- Return to inbox-check

If queue empty OR context HIGH:
- Burn wisp with summary digest
- Exit (daemon will respawn if needed)"""
id = "burn-or-loop"
needs = ["context-check"]
title = "Burn and respawn or loop"
