Skip to content
Open
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
120 changes: 120 additions & 0 deletions scripts/tooling/hapi-pr-status-codex-state.test.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
#!/usr/bin/env bash
# hapi-pr-status — the fork Codex dimension must read `codex-review-state:v1`.
#
# The ChatGPT Codex connector stopped emitting the legacy "Reviewed commit: `sha`"
# prose and now posts a machine-readable summary comment instead, edited in place
# as the review runs. While this script matched only the old prose, dimension 3
# reported UNKNOWN for *every* fork PR — and because `cold-review-clean` refuses
# to invent a tip-match, the fork-stage gate became un-passable, pushing agents
# toward habitual `--skip-fork-stage`. A gate that can only ever say "unknown" is
# worse than none, so both directions are asserted here, offline, against stubs.
set -euo pipefail
DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
S="$DIR/hapi-pr-status.sh"
PASS=0; FAIL=0
ok(){ PASS=$((PASS+1)); }; bad(){ FAIL=$((FAIL+1)); echo "FAIL: $1" >&2; }

W="$(mktemp -d)"; trap 'rm -rf "$W"' EXIT
TIP="21de8a3ef6a22e2257ac3380e9e24d5de7a3bc59"
OTHER="0000000000000000000000000000000000000abc"

# A `gh` stub standing in for the real CLI: enough surface for the fork path
# (version gate, labels, checks, graphql threads, reviews, comments).
mkdir -p "$W/bin"
cat > "$W/bin/gh" <<'STUB'
#!/usr/bin/env bash
set -euo pipefail
case "$*" in
--version*) echo "gh version 2.80.0 (2026-01-01)" ;;
*--json\ labels*) echo "$GH_LABELS" ;;
*pr\ checks*) echo "$GH_CHECKS" ;;
*--json\ headRefOid*) echo "$GH_HEAD_SHA" ;;
*graphql*) echo '{"data":{"repository":{"pullRequest":{"reviewThreads":{"nodes":[],"pageInfo":{"hasNextPage":false,"endCursor":null}}}}}}' ;;
*pulls/*/reviews*) echo '[[]]' ;;
*issues/*/comments*) echo "$GH_COMMENTS" ;;
*) echo "null" ;;
esac
STUB
chmod +x "$W/bin/gh"

export GH_LABELS='[]'
export GH_CHECKS='[{"name":"test","bucket":"pass"}]'
export GH_HEAD_SHA="$TIP"

# Build the summary comment the connector actually posts. `created_at` is
# deliberately older than `updated_at`: the comment is edited in place, so a
# selector keying on creation time would rank a finished review as stale.
state_comment() {
local sha="$1" status="$2" findings="$3" extra="${4:-}"
jq -nc --arg sha "$sha" --arg status "$status" --argjson findings "$findings" --arg extra "$extra" '
[[{
user: {login: "chatgpt-codex-connector"},
created_at: "2026-11-10T02:04:22Z",
updated_at: "2026-11-10T02:07:55Z",
body: ("<!-- codex-review-state:v1 " + ({
code_review: {
commit_sha: $sha,
has_any_findings: $findings,
has_unresolved_findings: $findings,
status: $status
},
security_review: {commit_sha: null, status: null}
} | tostring) + " -->\n\nCodex Review Summary\n" + $extra)
}]]'
}

run() { (PATH="$W/bin:$PATH" bash "$S" 324 --repo heavygee/hapi 2>&1); }

# 1. The case that was silently broken: review completed on the tip, no findings.
GH_COMMENTS="$(state_comment "$TIP" completed false)" ; export GH_COMMENTS
if out="$(run)"; then ok; else bad "tip-matched clean v1 state must exit 0:
$out"; fi
printf '%s' "$out" | grep -q "codex-review-state:v1" && ok || bad "v1 verdict not reported: $out"
printf '%s' "$out" | grep -qi "No .*review/comment with a reviewed commit" \
&& bad "v1 state must not read as a missing review: $out" || ok

# 2. Positive control for the other direction — findings present must still fail,
# or test 1 only proves the script stopped looking.
GH_COMMENTS="$(state_comment "$TIP" completed true)" ; export GH_COMMENTS
if out="$(run)"; then bad "completed-with-findings must exit non-zero: $out"; else ok; fi
printf '%s' "$out" | grep -q "FINDINGS PRESENT" && ok || bad "findings verdict not reported: $out"

# 2b. Explicit state must not be overridden by prose elsewhere in the same comment.
# The v1 summary also renders a security-review row, so "No findings" there
# would otherwise match the legacy clean regex and flip a real code-review
# failure to CLEAN. Reported on heavygee/hapi#326 by the fork bot.
GH_COMMENTS="$(state_comment "$TIP" completed true 'Security review: No findings')" ; export GH_COMMENTS
if out="$(run)"; then bad "security-review prose must not override code-review findings: $out"; else ok; fi
printf '%s' "$out" | grep -q "FINDINGS PRESENT" && ok || bad "findings verdict lost to prose match: $out"

# 3. An in-flight review is not a verdict. This is the dangerous shape: mid-run
# the JSON carries has_any_findings=null, which must never read as clean.
GH_COMMENTS="$(state_comment "$TIP" running null)" ; export GH_COMMENTS
if out="$(run)"; then bad "running review must not pass: $out"; else ok; fi
printf '%s' "$out" | grep -qi "not completed" && ok || bad "in-flight state not diagnosed: $out"

# 4. Tip-ahead must still be caught via the v1 commit_sha, not just the old prose.
GH_COMMENTS="$(state_comment "$OTHER" completed false)" ; export GH_COMMENTS
if out="$(run)"; then bad "stale-tip review must not pass: $out"; else ok; fi
printf '%s' "$out" | grep -qi "Tip moved past" && ok || bad "tip-ahead not diagnosed from v1 sha: $out"

# 5. cold-review-clean must NOT rescue an in-flight review — the label is an
# operator override for addressed findings, not a way to skip a running wave.
GH_COMMENTS="$(state_comment "$TIP" running null)" ; export GH_COMMENTS
export GH_LABELS='[{"name":"cold-review-clean"}]'
if out="$(run)"; then bad "label must not override a running review: $out"; else ok; fi
export GH_LABELS='[]'

# 6. Legacy prose must keep working — this fix adds a format, it does not
# replace one, and older PRs still carry the old shape.
GH_COMMENTS="$(jq -nc --arg sha "$TIP" '
[[{user: {login: "chatgpt-codex-connector"},
created_at: "2026-10-01T00:00:00Z",
updated_at: "2026-10-01T00:00:00Z",
body: ("### 💡 Codex Review: Didn'"'"'t find any major issues.\n\nReviewed commit: `" + $sha + "`")}]]')"
export GH_COMMENTS
if out="$(run)"; then ok; else bad "legacy Reviewed-commit prose regressed:
$out"; fi

echo "hapi-pr-status-codex-state: $PASS passed, $FAIL failed"
[[ $FAIL -eq 0 ]]
62 changes: 56 additions & 6 deletions scripts/tooling/hapi-pr-status.sh
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,12 @@
# posting formal REVIEWS (`### 💡 Codex Review`) and/or ISSUE COMMENTS via the
# ChatGPT subscription-side App. Clean signal: tip-matched review with no-findings
# body / 👍, OR operator `cold-review-clean` **only after a tip-matched wave**.
# Newer waves carry a machine-readable `codex-review-state:v1` block instead of
# the "Reviewed commit:" prose, in a summary comment EDITED IN PLACE as the
# review runs. Read tip from `code_review.commit_sha`; clean iff
# `status == "completed"` AND `has_any_findings == false`. A `running` review is
# not a verdict. Matching only the old prose made this dimension report UNKNOWN
# for every fork PR once the connector switched formats (2026-11, #324).
# - `cold-review-clean` does NOT skip unresolved threads, and does NOT excuse a tip
# that has moved past the latest Codex "Reviewed commit" (agents must wait for /
# clear the next wave). Usage-limit comments are not a clean verdict.
Expand Down Expand Up @@ -218,14 +224,15 @@ if [[ "$OWNER" == "heavygee" ]]; then
LATEST_EVENT=$(jq -n --slurpfile reviews "$REVIEWS_TMP" --slurpfile comments "$COMMENTS_TMP" '
def is_codex: (.user.login == "chatgpt-codex-connector[bot]"
or .user.login == "chatgpt-codex-connector");
def has_reviewed_commit: ((.body // "") | test("Reviewed commit"));
def has_reviewed_commit: ((.body // "")
| test("Reviewed commit") or test("codex-review-state:v1"));
def pages: if type == "array" and length > 0 and (.[0]|type) == "array" then add else . end;
($reviews[0] | pages // []) as $r
| ($comments[0] | pages // []) as $c
| ([ $r[]? | select(is_codex and has_reviewed_commit)
| {kind:"review", at:.submitted_at, body:(.body // "")} ]
+ [ $c[]? | select(is_codex and has_reviewed_commit)
| {kind:"comment", at:.created_at, body:(.body // "")} ])
| {kind:"comment", at:(.updated_at // .created_at), body:(.body // "")} ])
| sort_by(.at) | reverse | .[0] // null
' 2>/dev/null || echo "null")
# Quota notices can arrive as either a plain issue comment OR a formal
Expand Down Expand Up @@ -254,19 +261,42 @@ if [[ "$OWNER" == "heavygee" ]]; then
SUBMITTED=""
BODY=""
KIND=""
# Codex's newer summary comment carries machine-readable state instead of the
# legacy "Reviewed commit: `sha`" prose, and is EDITED IN PLACE as the review
# progresses (running → completed). Parse it when present; the prose parse
# below stays as the fallback for older waves.
# <!-- codex-review-state:v1 {"code_review":{"commit_sha":…,
# "has_any_findings":…,"status":"running|completed"},…} -->
STATE_STATUS=""
STATE_FINDINGS=""
if [[ "$LATEST_EVENT" != "null" && -n "$LATEST_EVENT" ]]; then
KIND=$(echo "$LATEST_EVENT" | jq -r '.kind')
SUBMITTED=$(echo "$LATEST_EVENT" | jq -r '.at')
BODY=$(echo "$LATEST_EVENT" | jq -r '.body // empty')
REVIEWED_SHA=$(printf '%s' "$BODY" | grep -oE 'Reviewed commit:\*\* `[0-9a-f]{7,40}`|Reviewed commit: `[0-9a-f]{7,40}`' \
| head -1 | grep -oE '[0-9a-f]{7,40}' | head -1)
STATE_JSON=$(printf '%s' "$BODY" \
| grep -o 'codex-review-state:v1 {.*}' | head -1 \
| sed 's/^codex-review-state:v1 //' \
| jq -c '.code_review // empty' 2>/dev/null || true)
if [[ -n "$STATE_JSON" ]]; then
REVIEWED_SHA=$(printf '%s' "$STATE_JSON" | jq -r '.commit_sha // empty')
STATE_STATUS=$(printf '%s' "$STATE_JSON" | jq -r '.status // empty')
# NOT `// empty` — jq's alternative operator swallows `false`, which
# is precisely the value that means "reviewed, nothing found".
STATE_FINDINGS=$(printf '%s' "$STATE_JSON" \
| jq -r 'if .has_any_findings == null then "" else (.has_any_findings | tostring) end')
fi
if [[ -z "$REVIEWED_SHA" ]]; then
REVIEWED_SHA=$(printf '%s' "$BODY" | grep -oE 'Reviewed commit:\*\* `[0-9a-f]{7,40}`|Reviewed commit: `[0-9a-f]{7,40}`' \
| head -1 | grep -oE '[0-9a-f]{7,40}' | head -1)
fi
echo " Source: ${BOT_DESC} ${KIND}"
echo " Last run: ${SUBMITTED}"
[[ -n "$REVIEWED_SHA" ]] && echo " Reviewed commit: ${REVIEWED_SHA}"
[[ -n "$STATE_STATUS" ]] && echo " Codex review state: ${STATE_STATUS} (findings=${STATE_FINDINGS:-unknown})"
echo "$BODY" | head -c 300 | sed 's/^/ /'
echo ""
else
echo " ? No ${BOT_DESC} review/comment with Reviewed commit found"
echo " ? No ${BOT_DESC} review/comment with a reviewed commit found"
BODY=""
fi

Expand Down Expand Up @@ -336,7 +366,27 @@ if [[ "$OWNER" == "heavygee" ]]; then
echo " → UNKNOWN (no Reviewed-commit Codex event; cold-review-clean does not invent tip-match)"
BOT_OK=false
PASS=1
elif printf '%s' "$BODY" | sed 's/\\n/ /g' | tr '\n' ' ' | grep -qE "$CLEAN_REGEX"; then
elif [[ -n "$STATE_STATUS" && "$STATE_STATUS" != "completed" ]]; then
# An in-flight review is not a verdict. Without this branch the summary
# comment's "no findings yet" shape would read as clean mid-run.
echo " ✗ Codex review is '${STATE_STATUS}', not completed"
echo " → FAIL (wait for the review to finish — cold-review-clean does not skip an in-flight wave)"
BOT_OK=false
PASS=1
elif [[ "$STATE_STATUS" == "completed" && "$STATE_FINDINGS" == "false" ]]; then
Comment thread
heavygee marked this conversation as resolved.
Resolved
if $TIP_MATCHED; then
echo " → PASS (tip-matched codex-review-state:v1, completed with no findings)"
else
echo " → FAIL (clean review state but not tip-matched)"
BOT_OK=false
PASS=1
fi
elif [[ -z "$STATE_STATUS" ]] && printf '%s' "$BODY" | sed 's/\\n/ /g' | tr '\n' ' ' | grep -qE "$CLEAN_REGEX"; then
# Prose matching is the LEGACY path only. When the event carries explicit
# machine-readable state, that state is the verdict and must not be second-
# guessed by a regex over the whole summary: the v1 comment also renders a
# security-review row, so a "No findings" there would otherwise override
# `has_any_findings: true` from the code review and report a false CLEAN.
if $TIP_MATCHED; then
echo " → PASS (tip-matched clean body)"
else
Expand Down