Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions AGENTS.md
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,8 @@ heads. Use a [stack](#stacked-pull-requests) when B needs code from A to work.
Compare the failure with the PR run to see whether it came from the PR
itself or its interaction with earlier changes. GitHub rebuilds affected
candidates on the surviving base. A new push or an explicit queue
removal expires the old request. After resolving the cause, post a fresh
removal expires the old request. The merge gate's own removal of a
pull request it is holding does not: it returns when the hold clears. After resolving the cause, post a fresh
command for the current head. [Queue timing and verification](docs/verification.md#how-releases-are-published)

## Stacked pull requests
Expand Down Expand Up @@ -269,7 +270,9 @@ you call it done.
- **Codex answers `@codex security review` with a review of the head
commit**, posted as "Codex Review". The security row of its summary can
stay at an earlier commit, and a finished security review with no findings
leaves its findings fields empty, not false.
leaves its findings fields empty, not false. Its security review starts
when a pull request opens or a draft is marked ready, not on a push, so
the merge gate makes a requested pull request a draft and ready again.
- **Tests that share a binary share a process.** A test that depends on a
file descriptor being closed can be broken by a sibling test that spawns a
child at the same moment.
Expand Down
54 changes: 46 additions & 8 deletions desktop/ci/auto-queue.sh
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,10 @@
# - no unresolved Codex review thread is marked P0 or P1, or carries a
# security review marker.
# Otherwise auto-merge is turned off. The security review runs by itself only
# when a pull request opens, so a requested head gets one asked for,
# once per head commit, in a comment from the maintainer's token. A completed security entry for the current head is required.
# when a pull request opens or a draft is marked ready, so a requested head
# gets one once per head commit: a comment from the maintainer's token records
# the request, and the pull request is made a draft and ready again. A
# completed security entry for the current head is required.
# Anything this script cannot read counts
# as "not reviewed": a failed lookup or a change in the reviewer's comment
# format stops automatic merges; it never lets one through.
Expand All @@ -44,6 +46,16 @@ reviewer='chatgpt-codex-connector'
# this shell, not in a command substitution, so the commit it judged is the
# commit the merge request is bound to.
decision=''
marker='<!-- ox-auto-queue -->'
# When this script last took the pull request out of the queue itself, as
# GitHub dated that removal. Kept in the status comment between runs.
own_removal=''
# Prints when the pull request was last removed from the merge queue, or nothing.
last_removal() {
gh api graphql -f id="${node}" -f query='query($id:ID!){node(id:$id){... on PullRequest{
timelineItems(last:1,itemTypes:[REMOVED_FROM_MERGE_QUEUE_EVENT]){nodes{... on RemovedFromMergeQueueEvent{createdAt}}}}}}' |
jq -er '.data.node.timelineItems.nodes | if type == "array" then (last.createdAt // "") else error("missing timeline") end'
}
head=''
head_ref=''
node=''
Expand Down Expand Up @@ -84,12 +96,19 @@ decide() {
decision="hold: awaiting /queue ${head} from the maintainer"; return
fi
intent=yes
if ! removed_at=$(gh api graphql -f id="${node}" -f query='query($id:ID!){node(id:$id){... on PullRequest{
timelineItems(last:1,itemTypes:[REMOVED_FROM_MERGE_QUEUE_EVENT]){nodes{... on RemovedFromMergeQueueEvent{createdAt}}}}}}' |
jq -er '.data.node.timelineItems.nodes | if type == "array" then (last.createdAt // "") else error("missing timeline") end'); then
if ! removed_at=$(last_removal); then
decision="hold: could not read the queue history"; return
fi
if [ -n "${removed_at}" ] && [[ "${consent}" < "${removed_at}" || "${consent}" == "${removed_at}" ]]; then
# A removal by GitHub (failed checks) or by the maintainer ends the
# request. This script's own removal of a held pull request does not: the
# hold is what it was waiting on, and on 2026-12-03 that removal left #388
# asking for a second /queue once its reviews had arrived. GitHub records
# both kinds as "manual" by the same account, so the script notes its own.
own_removal=$(jq -r --arg who "${maintainer}" --arg marker "${marker}" \
'[.[] | select(.login == $who and (.body | startswith($marker)))] | last // empty | .body' <<< "${comments}" |
sed -n 's/^<!-- ox-auto-queue-removed \([0-9TZ:-]*\) -->$/\1/p' | tail -n 1)
if [ -n "${removed_at}" ] && [ "${removed_at}" != "${own_removal}" ] &&
[[ "${consent}" < "${removed_at}" || "${consent}" == "${removed_at}" ]]; then
decision="hold: previous queue request expired after removal; send /queue ${head} to retry"; return
fi
# Only the default branch has required checks and the merge queue. A pull
Expand Down Expand Up @@ -198,8 +217,13 @@ if [[ "${decision}" == hold:* ]]; then
node=$(jq -er '.node_id' <<< "${pr}")
entry=$(queue_entry)
if [ "${entry}" != none ]; then
gh api graphql -f id="${node}" -f query='mutation($id:ID!){dequeuePullRequest(input:{id:$id}){clientMutationId}}' >/dev/null ||
# Only a removal this script made counts as its own: if GitHub got
# there first the mutation fails and the removal stays GitHub's.
if gh api graphql -f id="${node}" -f query='mutation($id:ID!){dequeuePullRequest(input:{id:$id}){clientMutationId}}' >/dev/null; then
own_removal=$(last_removal) || own_removal=''
else
echo "::warning::Checking whether GitHub already removed the queue entry" >&2
fi
fi
# A missing field, failed read or concurrent merge cannot confirm a hold.
pr=$(gh api "repos/${repo}/pulls/${number}")
Expand All @@ -212,7 +236,6 @@ fi

# One comment that says where the pull request stands, written when it first
# has something to say and edited only when that changes.
marker='<!-- ox-auto-queue -->'
status=''
if [ -n "${removed}" ]; then
status="**${removed}.** The run that tested it on top of \`main\` is in this pull request's checks: read the failed job there. After investigating the failed check, send a fresh \`/queue ${head}\` to retry this head."
Expand All @@ -230,6 +253,10 @@ fi
if [ -n "${status}" ] && [ -n "${comments}" ]; then
status="${marker}
${status}"
if [ -n "${own_removal}" ]; then
status="${status}
<!-- ox-auto-queue-removed ${own_removal} -->"
fi
previous="$(jq -r --arg who "${maintainer}" --arg marker "${marker}" \
'[.[] | select(.login == $who and (.body | startswith($marker)))] | last // empty | "\(.id)\t\(.body | @base64)"' <<< "${comments}")"
if [ -z "${previous}" ]; then
Expand All @@ -245,6 +272,17 @@ if [ "${request}" = yes ]; then
gh pr comment "${url}" --body "@codex security review

Asked for by the maintainer’s queue request, for commit ${head}."
# Codex answers that comment with a code review and leaves its security
# entry at an older commit. On 2026-12-03 three requested heads waited an
# hour on it. Marking a draft ready is what starts both reviews of the
# head: every pull request admitted that day carried that trigger, and
# #388 got both within two minutes of it. The comment above stops a later
# run from doing this twice for one head.
gh pr ready "${url}" --undo
if ! gh pr ready "${url}" && ! { sleep 5; gh pr ready "${url}"; }; then
echo "::error::#${number} was made a draft to start its security review and could not be marked ready again"
exit 1
fi
fi
# A lookup that failed is a fault someone should see, not a quiet hold.
case "${decision}" in
Expand Down
54 changes: 51 additions & 3 deletions desktop/ci/test_auto_queue.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,13 @@
case "$*" in
*"pr checks"*) echo '[{"name":"OX merge check","bucket":"pass","link":"https://github.com/o/r/actions/runs/123/job/456"}]'; exit 0 ;;
*enqueuePullRequest*) echo "$*" >> "$dir/calls"; exit 0 ;;
*dequeuePullRequest*) echo "$*" >> "$dir/calls"; [ "$fail" = dequeue ] || rm -f "$dir/queued"; exit 0 ;;
*dequeuePullRequest*) echo "$*" >> "$dir/calls"; [ "$fail" = dequeue ] || rm -f "$dir/queued"
[ "$fail" = github-first ] && exit 1
if [ -f "$dir/after-dequeue.json" ]; then cp "$dir/after-dequeue.json" "$dir/timeline.json"; fi; exit 0 ;;
*mergeQueueEntry*) if [ -f "$dir/queued" ]; then echo '{"data":{"node":{"id":"PR_38","mergeQueueEntry":{"id":"entry"}}}}'; else echo '{"data":{"node":{"id":"PR_38","mergeQueueEntry":null}}}'; fi; exit 0 ;;
*"/actions/workflows/ci.yml/runs"*) cat "$dir/runs.json"; exit 0 ;;
*REMOVED_FROM_MERGE_QUEUE_EVENT*) cat "$dir/timeline.json"; exit 0 ;;
*"pr ready"*) echo "$*" >> "$dir/calls"; [ "$fail" = ready ] && [ "$2 $4" != "ready --undo" ] && exit 1; exit 0 ;;
*"pr merge"*|*"pr comment"*|*"--method"*) echo "$*" >> "$dir/calls"; exit 0 ;;
*graphql*) file=threads.json ;;
*/comments*) file=comments.json ;;
Expand Down Expand Up @@ -57,7 +60,7 @@ def thread(body, resolved=False, author="chatgpt-codex-connector", kind="Bot"):
class AutoQueue(unittest.TestCase):
def decide(self, *, pull=None, comments=None, threads=None, more_threads=False, dry_run=False, fail=None,
event=None, reason=None, queued=False, consent=True, run_head=HEAD, latest_run_id=123,
removed_at=None, earlier_run=None):
removed_at=None, earlier_run=None, dequeued_at=None):
base = {"node_id": "PR_38", "auto_merge": None, "merged": False, "state": "open", "draft": False, "user": {"login": "maintainer"},
"head": {"sha": HEAD, "ref": "topic", "repo": {"full_name": "o/r"}}, "labels": [{"name": "full-ci"}],
"base": {"ref": "main", "repo": {"default_branch": "main"}}}
Expand All @@ -82,6 +85,9 @@ def decide(self, *, pull=None, comments=None, threads=None, more_threads=False,
(tmp / "runs.json").write_text(json.dumps({"workflow_runs": [*runs, latest_run]}))
nodes = [{"createdAt": removed_at}] if removed_at else []
(tmp / "timeline.json").write_text(json.dumps({"data": {"node": {"timelineItems": {"nodes": nodes}}}}))
if dequeued_at:
(tmp / "after-dequeue.json").write_text(json.dumps(
{"data": {"node": {"timelineItems": {"nodes": [{"createdAt": dequeued_at}]}}}}))
if queued:
(tmp / "queued").touch()
if fail:
Expand All @@ -99,7 +105,9 @@ def decide(self, *, pull=None, comments=None, threads=None, more_threads=False,
if reason:
env["AUTO_QUEUE_REASON"] = reason
run = subprocess.run(["bash", str(SCRIPT), "38"], env=env, capture_output=True, text=True)
self.assertEqual(run.returncode, 1 if fail or (pull or {}).get("auto_merge") else 0, run.stderr)
# GitHub removing the entry first is not a fault: the hold is confirmed and the run passes.
failed = (fail and fail != "github-first") or (pull or {}).get("auto_merge")
self.assertEqual(run.returncode, 1 if failed else 0, run.stderr)
calls = (tmp / "calls").read_text() if (tmp / "calls").exists() else ""
return run.stdout.strip(), calls

Expand Down Expand Up @@ -192,15 +200,27 @@ def test_a_head_commit_without_a_security_review_gets_one_requested_once(self):
self.assertIn("pr comment", calls)
self.assertIn(f"@codex security review\n\nAsked for by the maintainer’s queue request, for commit {HEAD}.", calls)
self.assertIn("--disable-auto", calls)
# The comment records the request; draft and ready again is what starts the review.
url = "https://github.com/o/r/pull/38"
self.assertLess(calls.index("pr comment"), calls.index(f"pr ready {url} --undo"))
self.assertIn(f"pr ready {url} --undo\npr ready {url}\n", calls)
asked = {"user": {"login": "maintainer", "type": "User"},
"body": f"@codex security review\n\nAsked for by the maintainer’s queue request, for commit {HEAD}."}
out, calls = self.decide(comments=comments + [asked])
self.assertNotIn("pr comment", calls)
self.assertNotIn("pr ready", calls)
# Someone else's comment is not the maintainer's request.
asked["user"] = {"login": "someone", "type": "User"}
out, calls = self.decide(comments=comments + [asked])
self.assertIn("pr comment", calls)

def test_a_pull_request_left_as_a_draft_fails_the_run(self):
comments = [{"user": BOT_USER, "body": summary(HEAD, "completed", security=None)}]
out, calls = self.decide(comments=comments, fail="ready")
url = "https://github.com/o/r/pull/38"
self.assertEqual(calls.count(f"pr ready {url}\n"), 2) # One retry, then a red run someone sees.
self.assertIn("could not be marked ready again", out)

def test_an_ordinary_review_cannot_stand_in_for_security(self):
# Codex answers the request with a review of the head commit and may
# leave its security entry at an earlier commit (seen on #39).
Expand Down Expand Up @@ -327,6 +347,34 @@ def request(body, **extra):
"body": f"/queue {HEAD}{signed}"}])
self.assertNotIn("enqueuePullRequest", calls)

def test_this_scripts_own_removal_does_not_end_the_request(self):
# Seen on #388: queued by hand while the security review was missing, removed by this
# script, then asked for a second /queue once the reviews were in. GitHub recorded that
# removal as reason "manual" by the maintainer's account, the same as a removal by hand.
waiting = [{"user": BOT_USER, "body": summary(HEAD, "completed", security=None)}]
out, calls = self.decide(comments=waiting, queued=True, dequeued_at="2026-11-27T12:03:00Z")
self.assertIn("dequeuePullRequest", calls)
self.assertIn("<!-- ox-auto-queue-removed 2026-11-27T12:03:00Z -->", calls)

def status(removed):
return {"user": {"login": "maintainer", "type": "User"},
"body": f"<!-- ox-auto-queue -->\n**Not in line to merge yet:** x.\n<!-- ox-auto-queue-removed {removed} -->"}
reviewed = {"user": BOT_USER, "body": summary(HEAD, "completed")}
_, calls = self.decide(comments=[reviewed, status("2026-11-27T12:03:00Z")], removed_at="2026-11-27T12:03:00Z")
self.assertIn("enqueuePullRequest", calls)
# A later removal, by GitHub for failed checks or by the maintainer, still ends it.
_, calls = self.decide(comments=[reviewed, status("2026-11-27T12:03:00Z")], removed_at="2026-11-27T12:05:00Z")
self.assertNotIn("enqueuePullRequest", calls)
# Someone else's comment cannot claim a removal as the script's own.
forged = dict(status("2026-11-27T12:03:00Z"), user={"login": "someone", "type": "User"})
_, calls = self.decide(comments=[reviewed, forged], removed_at="2026-11-27T12:03:00Z")
self.assertNotIn("enqueuePullRequest", calls)

def test_a_removal_github_made_first_is_not_recorded_as_this_scripts(self):
waiting = [{"user": BOT_USER, "body": summary(HEAD, "completed", security=None)}]
out, calls = self.decide(comments=waiting, queued=True, fail="github-first", dequeued_at="2026-11-27T12:03:00Z")
self.assertNotIn("ox-auto-queue-removed", calls)

def test_a_held_pr_is_removed_from_the_queue_and_reread(self):
for fail in (None, "dequeue"):
out, calls = self.decide(pull={"draft": True}, queued=True, fail=fail)
Expand Down
Loading