# Report setup progress to Jog

While you move a repository's GitHub Actions jobs to Jog, report each step to Jog, so the person sees where you are on their account's Overview at https://usejog.com/account/ACCOUNT. ACCOUNT is the account the prompt you were given names, and NAME the repository's name in it. AGENT is your own name, one of `claude-code`, `codex`, `opencode`, `pi` or `other`. You need `curl` and `jq`, in a Unix shell.

Reporting only shows the person where you are: it never decides what you do. If any step here fails, tell the person once that you can't report, and carry on with the setup without it.

Every report is one of the fixed values below. Never send words of your own, a file's contents, a log or a secret: Jog refuses anything else, and words each step itself.

## 1. Get a setup token

The token is kept in a file only the person can read, outside the repository: `${XDG_CONFIG_HOME:-$HOME/.config}/jog/setup-ACCOUNT.header`. It reports on ACCOUNT alone, for 24 hours, and reads nothing.

- If that file exists, go to step 2.
- Otherwise ask Jog for one, which the person approves in their browser. This asks, keeps Jog's answer in a file of its own beside the token's (its `deviceCode` collects the token, so it is never printed), prints that file's name and what the person needs, and opens the page:

```sh
set +x; d="${XDG_CONFIG_HOME:-$HOME/.config}/jog"
if ! { mkdir -p "$d" && chmod 700 "$d" && { [ "$(tail -n 1 "$d/.gitignore" 2>/dev/null)" = '*' ] || printf '\n*\n' | tee -a "$d/.gitignore" >/dev/null; } && a=$(mktemp "$d/.setup-device.XXXXXX"); }; then echo "can't write $d"
elif ! jq -nc '{name: "AGENT", access: "setup", org: "ACCOUNT"}' |
  command curl -q -sS --proto '=https' --max-time 30 -o "$a" -X POST https://usejog.com/api/device -H 'content-type: application/json' -d @-; then rm -f "$a"; echo 'no answer'
elif ! jq -se 'length == 1 and (.[0] | .deviceCode as $c | ($c | type == "string" and length > 0) and (.userCode | type == "string") and ((.verificationUriComplete // .verificationUri) | type == "string") and (.expiresIn | type == "number") and ([.userCode, (.verificationUriComplete // .verificationUri)] | all(index($c) == null)))' "$a" >/dev/null 2>&1; then
  jq -r 'select(type == "object") | (.error | strings | select(test("^[a-z_]{1,40}$") and (contains("\n") | not))) as $e | [$e] + (if $e == "logins_busy" or $e == "logins_busy_here" or $e == "busy" then [.retryAfter | numbers] else [] end) | map(tostring) | join(" ")' "$a" 2>/dev/null | grep . || echo 'unexpected answer'; rm -f "$a"
else
  printf 'file: %s\n' "$a"
  jq -r '"code: \(.userCode)", "page: \(.verificationUriComplete // .verificationUri)", "expires in: \(.expiresIn) seconds"' "$a"
  u=$(jq -r '.verificationUriComplete // .verificationUri // empty' "$a")
  case "$u" in https://usejog.com/*) (xdg-open "$u" || open "$u" || start "$u") </dev/null >/dev/null 2>&1 & ;; esac
fi
```

It prints the file it keeps Jog's answer in (`file`), the code the person checks (`code`), the page where they approve it (`page`) and how long the code lasts. Show the person the page and the code, in case the page didn't open. The page is the account's Overview: ask them to check the code it shows is the `code` printed, or to type it there, then approve. Approving a new token ends any earlier one of theirs for ACCOUNT. If it printed one line instead: `no answer` means Jog didn't answer, so run it again; `logins_busy` or `logins_busy_here` and a number, too many asks at once, so run it again after that many seconds; anything else, tell the person you can't report, and carry on without it. Then run this, with its second line set to the `file` it printed. It sends that file's device code, saves the token once it's approved and prints neither. It asks every few seconds for up to about 90 seconds, so give it a timeout of at least two minutes:

```sh
a=; IFS= read -r a <<'EOF'
THE FILE IT PRINTED
EOF
set +x; d="${XDG_CONFIG_HOME:-$HOME/.config}/jog"; f="$d/setup-ACCOUNT.header"
case "${a##*/}" in .setup-device.??????) [ "${a%/*}" = "$d" ] || a= ;; *) a= ;; esac
if [ -z "$a" ] || [ ! -f "$a" ] || [ -h "$a" ] || [ ! -s "$a" ]; then echo 'no code'
elif ! { mkdir -p "$d" && chmod 700 "$d" && { [ "$(tail -n 1 "$d/.gitignore" 2>/dev/null)" = '*' ] || printf '\n*\n' | tee -a "$d/.gitignore" >/dev/null; } && t=$(mktemp "$d/.setup.XXXXXX"); }; then echo "can't write $d"
else
  w=0; b=$(date +%s)
  while :; do
    r=$(jq -c '{deviceCode}' "$a" | command curl -q -sS --proto '=https' --max-time 15 -X POST https://usejog.com/api/device/token \
      -H 'content-type: application/json' -d @-) || r=
    if k=$(printf '%s' "$r" | jq -ser 'select(length == 1) | .[0].key | select(test("^jog_setup_[0-9a-f]{72}$"))' 2>/dev/null) && [ -n "$k" ]; then
      rm -f "$a"
      if ! printf 'Authorization: Bearer %s\n' "$k" >| "$t"; then rm -f "$t"; echo 'not saved'
      elif rm -f "$f" && mv "$t" "$f"; then echo saved
      else printf 'kept %s\n' "$t"; fi
      break
    fi
    e=$(printf '%s' "$r" | jq -sr 'select(length == 1) | .[0].error | strings | select(test("^[a-z_]{1,40}$") and (contains("\n") | not))' 2>/dev/null) || e=
    case "$e" in
      authorization_pending|logins_busy_here|logins_busy|busy) s=$(printf '%s' "$r" | jq -sr '(.[0].retryAfter | numbers | ceil | if . < 1 then 1 elif . > 90 then 91 else . end) // 5') ;;
      access_denied|expired_token) rm -f "$t" "$a"; echo "$e"; break ;;
      *) rm -f "$t"; echo "${e:-unexpected answer}"; break ;;
    esac
    if [ $((w + s)) -gt 90 ] || [ $(($(date +%s) - b + s)) -gt 90 ]; then rm -f "$t"; echo "$e"; break; fi
    sleep "$s"; w=$((w + s))
  done
fi
```

It prints one line:

- `saved`: the file holds the token. Go to step 2.
- `authorization_pending`, `logins_busy_here` or `busy`: it isn't approved yet, or Jog was busy. Run it again with the same `file`, until the seconds the code lasts have passed since you asked.
- `no code`: that isn't a file the first block kept, or an earlier run of this already used or ended its code. Run the first block again; if that fails too, carry on without reporting.
- `kept` and a file: the token is saved in that file, but couldn't take the place of `setup-ACCOUNT.header`. Never print or copy the kept file. Tell the person, and carry on without reporting.
- Anything else: tell the person you can't report, and carry on without it.

Never print, copy or move that file or the first block's `file`, and never put the token or the device code in a command line, a commit, a pull request, a log or the chat. Read it only as below. `command` skips a `curl` alias, and `-q` a `.curlrc`, either of which could print headers or follow redirects. Never add `-v`, `-L` or `--trace`.

## 2. Report each step

Each report is one command, with `REPORT` replaced by one of the reports below, on one line. It numbers the reports itself, in a file beside the token, so a report sent twice changes nothing:

```sh
f="${XDG_CONFIG_HOME:-$HOME/.config}/jog/setup-ACCOUNT.header"; n="${f%.header}.seq"; o="${f%.header}.answer"; rm -f "$o"
s=$(cat "$n" 2>/dev/null); case "$s" in ''|*[!0-9]*) s=0 ;; esac; s=$((s + 1)); printf '%s\n' "$s" >| "$n"
jq -nc --argjson seq "$s" '{seq: $seq, agent: "AGENT"} + REPORT' |
  command curl -q -sS --proto '=https' -X POST -H @"$f" -H 'content-type: application/json' -d @- -o "$o" -w '%{http_code}' https://usejog.com/api/orgs/ACCOUNT/setup/report
echo " $(jq -r '.error // empty' "$o" 2>/dev/null)"
```

Send each when it happens, at most one a second:

- The token is saved: `{step: "connected", state: "done"}`
- You start listing the jobs: `{step: "plan", state: "working"}`
- You show the person which jobs move and ask them to say go, with MOVE jobs moving and STAY staying: `{step: "plan", state: "needs_you", reason: "confirm_plan", counts: {move: MOVE, stay: STAY}}`
- They say go: `{step: "plan", state: "done", counts: {move: MOVE, stay: STAY}}`, then `{step: "pull", state: "working"}`
- You opened pull request NUMBER: `{step: "pull", state: "done", pull: {repo: "NAME", number: NUMBER}}`
- Its checks are done and it waits for the person: `{step: "pull", state: "needs_you", reason: "review_pull_request", pull: {repo: "NAME", number: NUMBER}}`
- You wait for the person for anything else, on the step you're on (`plan` or `pull`): `{step: "STEP", state: "needs_you", reason: "WHY"}`, where WHY is `permission` (a permission only they can give, such as `gh auth refresh -s workflow`), `credits` (the account has no credit) or `question` (anything else you asked them)
- You're working again once they answered: `{step: "STEP", state: "working"}`
- GitHub refuses the push: `{step: "pull", state: "failed", reason: "push_refused"}`
- GitHub refuses the pull request: `{step: "pull", state: "failed", reason: "pull_refused"}`
- No job can move to Jog: `{step: "plan", state: "failed", reason: "cannot_move"}`
- A moved job fails on Jog and you can't fix it: `{step: "pull", state: "failed", reason: "job_failed"}`
- Anything else stops you: `{step: "STEP", state: "failed", reason: "other"}`

Jog sees for itself whether the pull request is open, which jobs ran on Jog and whether it merged: never report those.

It prints the HTTP status, and Jog's error if there is one:

- 200: reported.
- 400: the report isn't one of these. Check it against the list.
- 401 `bad_key`: the token ended: the person disconnected it, approved a newer one, its 24 hours passed, or they no longer reach ACCOUNT. Delete the file and stop reporting; ask for a new token (step 1) only if the person asks you to.
- 403 `key_not_allowed`: the file isn't a setup token, so the session is over for you. Delete it and stop reporting; ask for a new token (step 1) only if the person asks you to.
- 404 `not_found` on a report with `pull`: Jog can't find that repository or pull request for this session. Check NAME and NUMBER, and keep sending the other reports.
- 404 `not_found` on every report: ACCOUNT is wrong, or Jog doesn't take reports now. Stop reporting.
- 409 `checking` or `reach_unknown`: Jog doesn't yet know which repositories the person reaches (a pull request's report needs it). Wait 10 seconds and send the same report again, at most 6 times; if it still answers this, carry on without that report and send the next one when it happens.
- 429 `too_many_reports`: wait a second, then send it again.
- 429 `reports_spent`: the token has sent all its reports. Stop reporting.
- Anything else: carry on without reporting.
