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
30 changes: 20 additions & 10 deletions .github/actions/pm-dispatch-queue/action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,10 @@ inputs:
description: "Optional existing issue number in the target repo to attach to instead of creating an issue. Takes precedence over issue."
required: false
default: ""
dedupe_key:
description: "Optional stable request key. When supplied, transient POST failures may be retried safely without creating duplicate tasks."
required: false
default: ""
outputs:
task_id:
description: "pm-dispatch queue task id."
Expand Down Expand Up @@ -71,6 +75,7 @@ runs:
INPUT_KIND: ${{ inputs.kind }}
INPUT_ISSUE: ${{ inputs.issue }}
INPUT_ISSUE_NUMBER: ${{ inputs.issue_number }}
INPUT_DEDUPE_KEY: ${{ inputs.dedupe_key }}
run: |
set -euo pipefail
# Resolve the prompt text. Resolution order: (1) prompt-file,
Expand Down Expand Up @@ -141,17 +146,22 @@ runs:
if [ -n "$INPUT_ISSUE_NUMBER" ]; then
payload="$(printf '%s' "$payload" | jq --argjson issue_number "$INPUT_ISSUE_NUMBER" '. + {issue_number: $issue_number}')"
fi
response="$(curl -sS -w $'\n%{http_code}' -X POST "https://dispatch.security.cab/v1/tasks" \
-H "Authorization: Bearer $oidc_token" \
-H "Content-Type: application/json" \
-d "$payload")"
http_code="${response##*$'\n'}"
body="${response%$'\n'*}"
if [ "$http_code" -lt 200 ] || [ "$http_code" -ge 300 ]; then
echo "::error::pm-dispatch-queue: pm-dispatch POST /v1/tasks failed (HTTP $http_code):" >&2
echo "$body" >&2
exit 1
if [ -n "$INPUT_DEDUPE_KEY" ]; then
payload="$(printf '%s' "$payload" | jq --arg dedupe_key "$INPUT_DEDUPE_KEY" '. + {dedupe_key: $dedupe_key}')"
fi
# A retry is safe only when the caller supplied an invocation-stable
# dedupe key; issue_number alone represents a distinct dispatch each
# time and therefore is not sufficient protection against duplicates.
# Issue creation is not idempotent server-side (the issue is created
# before the task is deduplicated), so a dedupe key alone is not
# enough: retries also require an existing issue_number to attach
# to, or issue=false so no issue is created.
retryable=false
if [ -n "$INPUT_DEDUPE_KEY" ] && { [ -n "$INPUT_ISSUE_NUMBER" ] || [ "$INPUT_ISSUE" != "true" ]; }; then
retryable=true
fi
body="$(OIDC_TOKEN="$oidc_token" PAYLOAD="$payload" RETRYABLE="$retryable" \
bash "${GITHUB_ACTION_PATH}/post-task.sh")" || exit 1
echo "$body"
task_id="$(printf '%s' "$body" | jq -r '.task_id // empty')"
issue_number="$(printf '%s' "$body" | jq -r '.issue_number // empty')"
Expand Down
83 changes: 83 additions & 0 deletions .github/actions/pm-dispatch-queue/post-task.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
#!/usr/bin/env bash
# post-task.sh — POST one repo-task packet to pm-dispatch /v1/tasks (#8950).
#
# Used by the pm-dispatch-queue composite action. Prints the response body on
# stdout and exits 0 on a 2xx; prints the failure to stderr and exits 1
# otherwise.
#
# A transient edge/origin failure (curl transport error, HTTP 000/502/503/504)
# is retried with a fixed backoff when RETRYABLE=true. The caller sets
# RETRYABLE=true only when the payload carries a caller-supplied, invocation-
# stable dedupe_key. Without it, replaying a POST could enqueue a duplicate.
#
# Environment:
# OIDC_TOKEN required bearer token (audience pm-dispatch)
# PAYLOAD required JSON request body
# RETRYABLE optional "true" to retry transient failures (default false)
# PM_DISPATCH_URL optional default https://dispatch.security.cab/v1/tasks
# PM_DISPATCH_MAX_ATTEMPTS optional default 3 (only used when RETRYABLE=true)
# PM_DISPATCH_RETRY_DELAY optional seconds between attempts, default 10
# PM_DISPATCH_MAX_TIME optional per-attempt curl --max-time seconds, default 60
set -euo pipefail

url="${PM_DISPATCH_URL:-https://dispatch.security.cab/v1/tasks}"
: "${OIDC_TOKEN:?post-task: OIDC_TOKEN is required}"
: "${PAYLOAD:?post-task: PAYLOAD is required}"

max_attempts=1
if [ "${RETRYABLE:-false}" = "true" ]; then
if ! printf '%s' "$PAYLOAD" | jq -e '(.dedupe_key | type == "string") and (.dedupe_key | length > 0)' >/dev/null; then
echo "::error::pm-dispatch-queue: retries require a non-empty caller-supplied dedupe_key in the payload" >&2
exit 1
fi
# Issue creation is NOT idempotent server-side: handleTasks creates the
# tracking issue before it deduplicates the queue task, so replaying a
# request whose response was lost would create a duplicate issue. Retries
# are therefore restricted to requests that attach to an existing issue
# (issue_number) or create no issue at all (issue != true).
if ! printf '%s' "$PAYLOAD" | jq -e '(.issue != true) or (.issue_number | type == "number")' >/dev/null; then
echo "::error::pm-dispatch-queue: retries require an existing issue_number or issue=false; issue creation is not idempotent" >&2
exit 1
fi
max_attempts="${PM_DISPATCH_MAX_ATTEMPTS:-3}"
fi
delay="${PM_DISPATCH_RETRY_DELAY:-10}"
max_time="${PM_DISPATCH_MAX_TIME:-60}"

attempt=1
while :; do
curl_status=0
response="$(curl -sS --connect-timeout 15 --max-time "$max_time" -w $'\n%{http_code}' -X POST "$url" \
-H "Authorization: Bearer $OIDC_TOKEN" \
-H "Content-Type: application/json" \
-d "$PAYLOAD")" || curl_status=$?
if [ "$curl_status" -eq 0 ]; then
http_code="${response##*$'\n'}"
body="${response%$'\n'*}"
else
http_code=000
body="curl exited with status $curl_status"
fi

if [ "$http_code" -ge 200 ] && [ "$http_code" -lt 300 ]; then
echo "$body"
exit 0
fi

case "$http_code" in
000 | 502 | 503 | 504) transient=true ;;
*) transient=false ;;
esac

if [ "$transient" = "true" ] && [ "$attempt" -lt "$max_attempts" ]; then
echo "::warning::pm-dispatch-queue: pm-dispatch POST /v1/tasks transient failure (HTTP $http_code, attempt $attempt/$max_attempts); retrying in ${delay}s" >&2
echo "$body" >&2
sleep "$delay"
attempt=$((attempt + 1))
continue
fi

echo "::error::pm-dispatch-queue: pm-dispatch POST /v1/tasks failed (HTTP $http_code, attempt $attempt/$max_attempts):" >&2
echo "$body" >&2
exit 1
done
141 changes: 141 additions & 0 deletions .github/scripts/pm-dispatch-queue-post.test.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
#!/usr/bin/env bash
# Contract tests for .github/actions/pm-dispatch-queue/post-task.sh (#8950):
# transient pm-dispatch failures (HTTP 502/503/504, transport errors) are
# retried only when the request is safe to repeat; everything else fails on
# the first attempt. A fake curl on PATH replays a scripted response sequence.
set -euo pipefail

SCRIPT="$(cd "$(dirname "${BASH_SOURCE[0]}")/../actions/pm-dispatch-queue" && pwd)/post-task.sh"
ACTION_YML="$(dirname "$SCRIPT")/action.yml"
WATCHER_YML="$(cd "$(dirname "${BASH_SOURCE[0]}")/../workflows" && pwd)/main-broken.yml"
TMP="$(mktemp -d)"
trap 'rm -rf "$TMP"' EXIT
mkdir -p "$TMP/bin"

cat >"$TMP/bin/curl" <<'EOF'
#!/usr/bin/env bash
# Fake curl: each call consumes the next line of $FAKE_CURL_SEQ ("<code>|<body>"
# or "exit:<n>") and records the call count.
n=$(($(cat "$FAKE_CURL_STATE" 2>/dev/null || echo 0) + 1))
echo "$n" >"$FAKE_CURL_STATE"
line="$(sed -n "${n}p" "$FAKE_CURL_SEQ")"
[[ -n $line ]] || line="500|sequence exhausted"
case "$line" in
exit:*)
echo "curl: (28) Operation timed out" >&2
exit "${line#exit:}"
;;
esac
printf '%s\n%s' "${line#*|}" "${line%%|*}"
EOF
chmod +x "$TMP/bin/curl"

# run_post RETRYABLE SEQ_LINE... -> sets RC, attempts, out, err
run_post() {
local retryable="$1"
shift
printf '%s\n' "$@" >"$TMP/seq"
rm -f "$TMP/state"
RC=0
PATH="$TMP/bin:$PATH" FAKE_CURL_SEQ="$TMP/seq" FAKE_CURL_STATE="$TMP/state" \
OIDC_TOKEN=tok PAYLOAD='{"repo":"r","dedupe_key":"test-run-step"}' RETRYABLE="$retryable" \
PM_DISPATCH_RETRY_DELAY=0 PM_DISPATCH_MAX_ATTEMPTS=3 \
bash "$SCRIPT" >"$TMP/out" 2>"$TMP/err" || RC=$?
attempts="$(cat "$TMP/state" 2>/dev/null || echo 0)"
}

fail() {
echo "FAIL: $1" >&2
echo "--- stdout" >&2
cat "$TMP/out" >&2
echo "--- stderr" >&2
cat "$TMP/err" >&2
exit 1
}

expect() { # name rc attempts
[[ $RC -eq $2 ]] || fail "$1: exit $RC, expected $2"
[[ $attempts -eq $3 ]] || fail "$1: $attempts attempts, expected $3"
echo "pass: $1"
}

run_post true '201|{"task_id":7}'
expect "success first try" 0 1
grep -Fq '{"task_id":7}' "$TMP/out" || fail "success body not printed"

run_post true '502|error code: 502' '201|{"task_id":8}'
expect "retryable 502 then success" 0 2
grep -Fq '{"task_id":8}' "$TMP/out" || fail "retry body not printed"
grep -Fq 'transient failure (HTTP 502, attempt 1/3)' "$TMP/err" || fail "no retry warning"

run_post true '503|unavailable' '504|timeout' '201|{"task_id":9}'
expect "retryable 503 and 504 then success" 0 3

run_post true 'exit:28' '201|{"task_id":10}'
expect "retryable transport error then success" 0 2

run_post true '502|a' '502|b' '502|c' '201|{"task_id":11}'
expect "retryable 502 exhausts attempts" 1 3
grep -Fq 'failed (HTTP 502, attempt 3/3)' "$TMP/err" || fail "no final failure message"
[[ ! -s "$TMP/out" ]] || fail "failure must not print a body on stdout"

run_post false '502|error code: 502' '201|{"task_id":12}'
expect "non-retryable 502 fails immediately" 1 1

rm -f "$TMP/state"
RC=0
PATH="$TMP/bin:$PATH" FAKE_CURL_SEQ="$TMP/seq" FAKE_CURL_STATE="$TMP/state" \
OIDC_TOKEN=tok PAYLOAD='{"repo":"r"}' RETRYABLE=true \
PM_DISPATCH_RETRY_DELAY=0 PM_DISPATCH_MAX_ATTEMPTS=3 \
bash "$SCRIPT" >"$TMP/out" 2>"$TMP/err" || RC=$?
[[ $RC -eq 1 ]] || fail "retry without dedupe_key should fail"
[[ ! -e "$TMP/state" ]] || fail "missing dedupe_key must not make a request"
grep -Fq 'retries require a non-empty caller-supplied dedupe_key' "$TMP/err" || fail "missing dedupe_key error not explained"
echo "pass: retries require a caller-supplied dedupe key"

# Issue-creation payloads must never be retried: the server creates the
# tracking issue before deduplicating the task, so a lost response followed
# by a retry would create a duplicate issue (Codex P2 on PR #8952).
rm -f "$TMP/state"
printf '%s\n' '502|error code: 502' '201|{"task_id":90}' >"$TMP/seq"
RC=0
PATH="$TMP/bin:$PATH" FAKE_CURL_SEQ="$TMP/seq" FAKE_CURL_STATE="$TMP/state" \
OIDC_TOKEN=test-token RETRYABLE=true PAYLOAD='{"repo":"r","dedupe_key":"test-run-step","issue":true}' \
PM_DISPATCH_RETRY_DELAY=0 PM_DISPATCH_MAX_ATTEMPTS=3 \
bash "$SCRIPT" >"$TMP/out" 2>"$TMP/err" || RC=$?
[[ $RC -eq 1 ]] || fail "issue-creation retry should fail"
[[ ! -e "$TMP/state" ]] || fail "issue-creation payload must not make a request when retries are requested"
grep -Fq 'retries require an existing issue_number or issue=false' "$TMP/err" || fail "issue-creation refusal not explained"
echo "pass: retries refused for issue-creation payloads"

# Attaching to an existing issue IS retryable: no issue is created.
rm -f "$TMP/state"
printf '%s\n' '502|error code: 502' '201|{"task_id":91}' >"$TMP/seq"
RC=0
PATH="$TMP/bin:$PATH" FAKE_CURL_SEQ="$TMP/seq" FAKE_CURL_STATE="$TMP/state" \
OIDC_TOKEN=test-token RETRYABLE=true PAYLOAD='{"repo":"r","dedupe_key":"test-run-step","issue":true,"issue_number":8950}' \
PM_DISPATCH_RETRY_DELAY=0 PM_DISPATCH_MAX_ATTEMPTS=3 \
bash "$SCRIPT" >"$TMP/out" 2>"$TMP/err" || RC=$?
attempts="$(cat "$TMP/state" 2>/dev/null || echo 0)"
[[ $RC -eq 0 ]] || fail "existing-issue retry should succeed"
[[ $attempts -eq 2 ]] || fail "existing-issue retry: $attempts attempts, expected 2"
grep -Fq '{"task_id":91}' "$TMP/out" || fail "existing-issue retry body not printed"
echo "pass: retries allowed when attaching to an existing issue"

run_post true '400|bad request' '201|{"task_id":13}'
expect "retryable 400 is never retried" 1 1

run_post true '404|issue not found' '201|{"task_id":14}'
expect "retryable 404 is never retried" 1 1

run_post true '500|boom' '201|{"task_id":15}'
expect "retryable 500 is never retried" 1 1

grep -Fq 'INPUT_DEDUPE_KEY: ${{ inputs.dedupe_key }}' "$ACTION_YML" || fail "action does not wire the dedupe_key input"
grep -Fq "jq --arg dedupe_key \"\$INPUT_DEDUPE_KEY\" '. + {dedupe_key: \$dedupe_key}'" "$ACTION_YML" || fail "action does not include the dedupe_key in its POST payload"
grep -Fq 'dedupe_key: main-broken-${{ github.run_id }}-${{ github.run_attempt }}-${{ github.job }}-dispatch-append' "$WATCHER_YML" || fail "watcher append call does not supply a stable dedupe_key"
grep -Fq '[ -n "$INPUT_ISSUE_NUMBER" ] || [ "$INPUT_ISSUE" != "true" ]' "$ACTION_YML" || fail "action does not restrict retries to existing-issue or no-issue requests"
echo "pass: action restricts retries to existing-issue or no-issue requests"
echo "pass: watcher append supplies the stable key that makes retries idempotent"

echo "pm-dispatch-queue-post.test: all contract tests passed"
1 change: 1 addition & 0 deletions .github/workflows/main-broken.yml
Original file line number Diff line number Diff line change
Expand Up @@ -441,6 +441,7 @@ jobs:
title: ${{ steps.identify.outputs.title }}
prompt: ${{ steps.identify.outputs.prompt }}
issue_number: ${{ steps.identify.outputs.existing_number }}
dedupe_key: main-broken-${{ github.run_id }}-${{ github.run_attempt }}-${{ github.job }}-dispatch-append

- name: Append a recurrence comment to the existing issue
if: steps.identify.outputs.action == 'append' && (steps.scout.outputs.route == '' || steps.scout.outputs.route == 'fix')
Expand Down
2 changes: 2 additions & 0 deletions .github/workflows/workflow-lint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ jobs:
|| echo "PyYAML unavailable; check-workflows.sh uses its structural fallback"
- name: Contract tests for the workflow validator (#5743)
run: bash .github/scripts/check-workflows.test.sh
- name: Contract tests for pm-dispatch retry safety (#8950)
run: bash .github/scripts/pm-dispatch-queue-post.test.sh
- name: Validate workflows and composite actions
run: bash .github/scripts/check-workflows.sh
- name: Contract tests for the ci-policy agreement checker (#7089)
Expand Down