# How the loop works

ub-agents passes work between agents with GitHub labels. A label says which role works on an issue or pull request next. GitHub holds all the state, so there is no server to run.

We call this loop engineering. You design the loop once: the roles, the labels and where a person steps in. Then you improve it from the agents' [retros](https://agents.uberblick.ai/docs/best-practices/retrospectives.md) instead of steering each run by hand.

See the loop play through different outcomes in the [workflow gallery](https://agents.uberblick.ai/workflows/), starting with the default loop and Dependabot on autopilot.

## Labels are the queue

Each launcher looks for labeled issues and pull requests, sorts them by priority and milestone, and hands the next eligible one to its agent.

1. **Claim.** The run claims the item with a comment, so no other agent picks it up.
2. **Run.** The agent works in its own worktree and branch. Nothing touches `main`.
3. **Report.** A second comment releases the claim and posts the next steps, and the labels move on to the next role. Older claim and report comments are hidden, so the thread shows the latest state.
4. **Move.** Issues become pull requests, and pull requests merge as your gates allow, some automatically, others after a maintainer approves.

The diagrams show the starter labels that `ub-agents init` writes. Every label, role and outcome is a line in `ub-agents.yaml`; change it and the launcher follows. See [Add a step to the workflow](https://agents.uberblick.ai/docs/guides/add-a-step.md).

## Issues: from idea to ready

```flow
flow v0
# How an issue is picked up, prepared and handed to delivery.

columns main person

panel open col=main tone=muted
  strong "A team member writes an issue"
pill needs-preparation tone=muted
panel queue tone=muted
  strong "The loop"
  muted "claims eligible items in order"
box issue-preparer "grounds it in the code, challenges assumptions, aims for an MVP"
pill ready tone=done
end delivery "Delivery picks it up"


panel human col=person row=issue-preparer tone=stop
  pill needs-human
  strong "A person reviews or answers"

open --> needs-preparation : someone sets it
needs-preparation --> queue
queue --> issue-preparer : starts the agent
issue-preparer --> ready : successfully prepared; acceptance criteria set
ready --> delivery
issue-preparer --> human tone=stop : needs-human, blocked
human --> needs-preparation tone=stop from=top to=right label=end : comment on GitHub, remove needs-human, re-add the label

legend
  pill tone=done "GitHub label, like ready"
  panel tone=stop "human involved"
```

The preparer gives each acceptance criterion a short check: a command, test or observation, or a manual check naming who performs it.

- **Ask.** When the issue needs a decision, the preparer reports `needs-human` and asks on the issue. Answer there, remove `needs-human` and add `needs-preparation` to prepare again, or `ready` to build it.
- **Skip.** A small, clear issue can go straight to `ready`.

### Complex issues

Instead of reporting `prepared`, the issue-preparer can:

- **Split it.** When the work doesn't fit one reviewable pull request, the preparer creates sub-issues and keeps the original open, blocked by each of them. The [generated workflow](https://agents.uberblick.ai/docs/commands/init.md#close-parent-issues) closes the parent after its last sub-issue closes. Agents never set trigger labels, so someone starts each sub-issue with `needs-preparation`.
- **Ask for a second opinion.** An issue-reviewer, ideally on another model, approves the issue to `ready` or sends it back with findings for the preparer to apply.

```flow
flow v0
# Optional: the issue-preparer asks for a second opinion before ready.

columns rework main

box issue-preparer col=main "grounds the issue, writes criteria"
pill needs-review tone=agent
box issue-reviewer "second opinion, ideally on another model"
pill ready tone=done
end delivery "Delivery picks it up"

pill needs-preparation col=rework row=issue-reviewer tone=rework

issue-preparer --> needs-review : review
needs-review --> issue-reviewer
issue-reviewer --> ready : approved
ready --> delivery
issue-reviewer --> needs-preparation tone=rework : changes-requested
needs-preparation --> issue-preparer tone=rework from=top to=left label=end : apply the findings

legend
  pill tone=done "GitHub label, like ready"
```

Both reviewers use `needs-review`: the issue-reviewer takes only issues, the reviewer only pull requests.

## Delivery: implementer, reviewer, integrator

```flow
flow v0
# Delivery: implementer, reviewer and integrator pass one pull request along.

columns rework main stop

pill ready col=main tone=done "ready (issue)"
box implementer "branch, code, checks, opens the PR"
pill needs-review tone=agent
box reviewer "reviews the exact head commit"
pill ready-to-merge tone=done
box integrator "updates base, checks, merges"
end merged "Merged · issue closed"

pill needs-changes col=rework row=reviewer tone=rework

panel human col=stop row=implementer span=5 tone=stop
  pill needs-human
  strong "A person reviews or answers"

ready --> implementer
implementer --> needs-review : handed-off
needs-review --> reviewer
reviewer --> ready-to-merge : approved
ready-to-merge --> integrator
integrator --> merged : merged

reviewer --> needs-changes tone=rework : changes-requested
ready-to-merge -.-> needs-changes tone=rework to=bottom.2 : on merge conflict
integrator --> needs-changes tone=rework to=bottom.1 : changes-requested
needs-changes --> implementer tone=rework from=top to=left label=end : revise the same PR

implementer --> human tone=stop : needs-human
reviewer --> human tone=stop : blocked
integrator --> human tone=stop : maintainer-merge, blocked

legend
  pill tone=done "GitHub label, like ready"
  panel tone=stop "human involved"
```

- **Acceptance checks.** The reviewer lists each criterion as checked by running, checked by reading or not checked, explaining any gap. Manual checks are listed for the maintainer and do not block approval on their own; an unchecked runnable criterion is P1.
- **Merge checks.** The integrator runs the project's declared checks and waits for every CI check on the exact head it merges, including its own base merge. Pending checks, even a just-pushed head with none reported yet, mean wait. It skips waiting only when CI configuration and recent default-branch runs confirm no checks are expected for that head. Immediately before merging, it confirms the PR targets the default branch and checks the item's labels, reporting blocked if a configured [stop label](https://agents.uberblick.ai/docs/configuration/stop-labels.md) is present. It rechecks `UNKNOWN` mergeability and, if the merge command fails, checks whether the PR actually merged before reporting.
- **Back and forth.** The usual round trip is between implementer and reviewer. The integrator also sends a PR back to `needs-changes` for a merge conflict or a check failure the PR caused. For a failure in code the diff doesn't touch, it reruns the failed jobs once. A passing rerun gets an issue for the flaky check, then integration continues. A repeated failure is compared with the same check on the current base branch, running it there if needed: a matching base failure gets an issue and a blocked report; a passing base sends the PR back with both results. If the base cannot be checked, integration blocks for missing evidence.
- **Maintainer merge.** When your merge policy says a person merges this PR, for example because it changes workflows, permissions or release settings, the integrator reports `maintainer-merge` with the reason and the PR gets `needs-human`.
- **Three rounds.** Each review finding is P1 (must not merge as is) or P2. The reviewer sends a PR back at most three times, and always reviews a P1 fix again. After the third round, a PR with only P2 findings left is approved with them listed; an open P1 gets `needs-human`.

What may merge, and who decides, is your policy in `.agents/ub_agents.md`. See [Instructions](https://agents.uberblick.ai/docs/configuration/instructions.md).

## Outside contributors and approvals

```flow
flow v0
# Approvals: whose input reaches the agents (on by default for public repositories).

columns main held

panel input col=main tone=muted
  strong "An issue or PR carries a trigger label"
panel check tone=muted
  strong "The launcher checks the input"
  muted "who applied the trigger"
  muted "who edited it since"
  muted "who approved the PR head"
end runs "The agent runs"

panel held col=held row=check tone=stop
  pill needs-human
  strong "Held for a maintainer"
  muted "an authorized GitHub org member"
  muted "must approve it"

input --> check
check --> runs : a maintainer started it
check --> held tone=stop : outside input
held --> input tone=stop from=top to=right label=end : maintainer acts

legend
  pill tone=done "GitHub label, like ready"
  panel tone=stop "human involved"
```

On open source repositories anyone can open an issue or a pull request. With approvals on, someone with the maintain or admin role starts the work by adding the trigger label, and approves a pull request's current head with a review. Without approvals, any labeled work runs, which is usually enough for private repositories. Approvals are on by default for public repositories. See [Approvals](https://agents.uberblick.ai/docs/configuration/approvals.md).

## When work is paused

Agents pause when an issue is ambiguous, contradicts itself or your instructions (such as `AGENTS.md`), or needs a decision. The item gets `needs-human`, or an **Action needed** comment when the role has no such outcome, with concrete recommendations you can answer on GitHub. A crash or exhausted retries also posts an error report. [Unstick an item](https://agents.uberblick.ai/docs/guides/stuck-items.md) covers each case.
