Watching a PR with steward

Read a pull request's state and decide the agent's next action

steward reads a pull request and tells the agent what to do next — merge a conflict, fix a check, answer a thread, or wait — without ever writing to GitHub.

Diagram

Steward — Read & Decide

CONFLICTING

failing check

open thread

nothing

closed/merged

📥 gh pr view

🔍 read state

🧭 next_action

🔀 merge_base

🛠 fix_check

💬 answer_thread

⏸ wait

🛑 stop

Quick Start

  1. Read a PR and see what to do
    python
    from praisonai_code.ship.steward import read_pr_state, next_action
    
    state = read_pr_state("6786")           # or read_pr_state() for the current branch's PR
    action = next_action(state, attempts={})
    
    print(action.kind, action.key, action.detail)
    # e.g. "fix_check check:unit unit"
  2. Loop with an attempt cap
    python
    from praisonai_code.ship.steward import read_pr_state, next_action
    
    attempts = {}
    while True:
        state = read_pr_state("6786")
        action = next_action(state, attempts, max_attempts=3)
        if action.kind in ("stop", "wait"):
            print(action.kind, "-", action.detail)
            break
        # Hand `action` to your agent to actually fix it, then:
        attempts[action.key] = attempts.get(action.key, 0) + 1

How It Works

Diagram
gh CLIstewardAgentgh CLIstewardAgentread_pr_state(“6786”)gh pr view 6786 –json …state, checks, head, basegh api graphql (viewer + reviewThreads)viewer login, threadsPRStatenext_action(state, attempts)Action(kind, key, detail)
  1. read_pr_state runs gh pr view [pr] --json number,url,state,mergeable,headRefName,baseRefName,statusCheckRollup.
  2. Failing checks are those whose CheckRun conclusion or StatusContext state is in {"FAILURE", "TIMED_OUT", "CANCELLED", "ACTION_REQUIRED", "STARTUP_FAILURE", "ERROR"}. Duplicates are collapsed, order is kept.
  3. One GraphQL query fetches the viewer and the PR’s review threads.
  4. next_action picks the first item to work on.

Which Action Comes Next?

OrderConditionReturned action
0state.state != "OPEN" (MERGED or CLOSED)Action("stop", detail="pull request is merged") / "... closed"
1state.mergeable == "CONFLICTING"Action("merge_base", key="conflict", detail=f"merge {state.base} in")
2Each name in state.failing (in order)Action("fix_check", key=f"check:{name}", detail=name)
3Each thread in state.threadsAction("answer_thread", key=f"thread:{t.id}", detail=t.body)
4Nothing to do, or every item skipped by the capAction("wait", detail="nothing to do") or detail="gave up on: ..."
Diagram

No

Yes

Yes

No / UNKNOWN

Yes

No

Yes

No

PRState

state == OPEN?

stop

mergeable == CONFLICTING?

merge_base

any failing check?

fix_check

any open thread?

answer_thread

wait

UNKNOWN mergeability counts as neither mergeable nor conflicting — with no failing checks or threads, you get wait.


What Threads Does steward Surface?

Thread shapeSurfaced?
Resolved❌ skipped
Outdated (comment on a line no longer in the diff)❌ skipped
Last comment is by the viewer (you already replied)❌ skipped
Empty (no comments)❌ skipped
Unresolved, non-outdated, last comment by someone else✅ returned

Reference

Attempt cap (max_attempts)

attempts is a dict[str, int] mapping action.key to tries. Each item (conflict, check:<name>, thread:<id>) gets at most max_attempts=3 tries. When every remaining item has been tried that many times, next_action returns wait with a detail like gave up on: check:unit, thread:T1. The cap stops endless retry loops on a failure the agent can’t fix.

PR in another repo

read_pr_state takes owner / name from the PR’s own url, not from gh‘s {owner}/{repo} placeholders (which resolve to the current directory’s repo). So you can pass a PR URL from a different repo than your working directory.

PRState fields
FieldTypeMeaning
numberintPR number
urlstrPR URL
statestrOPEN, MERGED, or CLOSED
mergeablestrMERGEABLE, CONFLICTING, or UNKNOWN
headstrHead branch
basestrBase branch
failinglist[str]Names of failing checks
threadslist[Thread]Open review threads waiting on you
Thread and Action fields

Thread: id (GraphQL thread id), path, line (int | None), author (last commenter), body (last comment).

Action: kind (stop, merge_base, fix_check, answer_thread, wait), key (attempt-counter key, default ""), detail (human-readable detail, default "").

GraphQL query used
graphql
query($owner:String!,$name:String!,$number:Int!){
  viewer{login}
  repository(owner:$owner,name:$name){
    pullRequest(number:$number){
      reviewThreads(first:100){
        nodes{id isResolved isOutdated path line
          comments(last:1){ nodes{ author{login} body } } } } } } }

Related