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
98 changes: 98 additions & 0 deletions .github/actions/preserve-required-gate/assess-required-gate.sh
Original file line number Diff line number Diff line change
Expand Up @@ -67,11 +67,46 @@ is_github_timestamp() {
[[ "$1" =~ ^[0-9]{4}-[0-9]{2}-[0-9]{2}T[0-9]{2}:[0-9]{2}:[0-9]{2}Z$ ]]
}

recover_validation_job_from_attempt() {
local attempt attempt_snapshot cache_key expected_branch expected_head_repository
attempt_recovered_job_json=''
[[ "${EXPECTED_WORKFLOW_PATH}" == ".github/workflows/ci.yml" &&
"${EXPECTED_WORKFLOW_NAME}" == "CI — Validation" &&
"${REQUIRED_GATE_NAME}" == "Validation Gate" ]] || return 1
attempt="$(jq -er '.run_attempt | select(type == "number" and . > 0 and . == floor)' <<<"${job_json}")" || return 1
cache_key="${workflow_run_id}:${attempt}:${job_id}:${check_run_id}"
if [[ -n "${attempt_snapshot_cache[${cache_key}]:-}" ]]; then
attempt_recovered_job_json="$(jq -cer '.job' <<<"${attempt_snapshot_cache[${cache_key}]}")" || return 1
return 0
fi
if (( attempt_recovery_reads >= attempt_recovery_read_limit )); then
echo "Exact-attempt historical sparse-candidate recovery exhausted its ${attempt_recovery_read_limit}-snapshot budget; refusing to assess." >&2
return 3
fi
attempt_recovery_reads=$((attempt_recovery_reads + 1))
expected_branch="$(jq -er '.head_branch | select(type == "string" and length > 0)' <<<"${workflow_run_json}")" || return 1
expected_head_repository="$(jq -er '.head_repository.full_name | select(type == "string" and length > 0)' <<<"${workflow_run_json}")" || return 1
attempt_snapshot="$(bash "$(dirname "${BASH_SOURCE[0]}")/validate-exact-attempt-gate.sh" \
"${workflow_run_id}" "${attempt}" "${job_id}" "${check_run_id}" \
"${REQUIRED_GATE_NAME}" "${expected_workflow_id}" "${EXPECTED_WORKFLOW_NAME}" \
"${EXPECTED_WORKFLOW_PATH}" "${expected_display_title}" "${PR_NUMBER}" \
"${BASE_SHA}" "${HEAD_SHA}" "${expected_branch}" "${GITHUB_REPOSITORY}" \
"${expected_head_repository}" "${check_status}" "${conclusion}" \
"${details_url}" "${started_at}" "${completed_at}" false)" || return 1
attempt_snapshot_cache["${cache_key}"]="${attempt_snapshot}"
attempt_recovered_job_json="$(jq -cer '.job' <<<"${attempt_snapshot}")" || return 1
return 0
}

api_error_file="$(mktemp)"
trap 'rm -f "$api_error_file"' EXIT
expected_workflow_id=""
declare -A workflow_run_cache=()
declare -A job_cache=()
declare -A attempt_snapshot_cache=()
attempt_recovery_reads=0
attempt_recovery_read_limit=3
attempt_recovered_job_json=''

# Recognize only the observed pre-execution Validation cancellation shape. A
# cancelled workflow may still contain genuine failures; its conclusion alone
Expand Down Expand Up @@ -418,6 +453,69 @@ while IFS=$'\t' read -r app_slug check_name details_url check_run_id check_statu
candidate_ambiguous=true
continue
fi
if [[ "${EXPECTED_WORKFLOW_PATH}" == ".github/workflows/ci.yml" &&
"${EXPECTED_WORKFLOW_NAME}" == "CI — Validation" &&
"${REQUIRED_GATE_NAME}" == "Validation Gate" &&
"${check_status}" == "completed" &&
( "${preserve_step_conclusion}" == "unknown" || -z "${preserve_step_conclusion}" ) ]]; then
case "${conclusion}" in
cancelled|skipped|stale) ;;
*)
preserve_step_state="$(jq -r '
if (.steps // null) == null then "absent"
elif (.steps | type) != "array" or any(.steps[]; type != "object" or (.name | type) != "string") then "invalid"
else
[.steps[] | select(.name == "Preserve successful required gate on metadata-only edit")] as $matches
| if ($matches | length) == 0 then "absent"
elif ($matches | length) == 1 then "present"
else "invalid" end
end
' <<<"${job_json}" 2>>"${api_error_file}")" || {
candidate_ambiguous=true
continue
}
if [[ "${preserve_step_state}" == "invalid" ]]; then
candidate_ambiguous=true
continue
fi
if [[ "${preserve_step_state}" == "absent" ]]; then
if recover_validation_job_from_attempt; then
job_json="${attempt_recovered_job_json}"
job_cache["${job_id}"]="${job_json}"
preserve_step_state="$(jq -r '
if (.steps | type) != "array" or any(.steps[]; type != "object" or (.name | type) != "string") then "invalid"
else
[.steps[] | select(.name == "Preserve successful required gate on metadata-only edit")] as $matches
| if ($matches | length) == 0 then "absent"
elif ($matches | length) == 1 then "present"
else "invalid" end
end
' <<<"${job_json}" 2>>"${api_error_file}")" || {
candidate_ambiguous=true
continue
}
if [[ "${preserve_step_state}" == "invalid" ]]; then
candidate_ambiguous=true
continue
fi
preserve_step_conclusion="$(jq -r '
[.steps[]?
| select(.name == "Preserve successful required gate on metadata-only edit")
| (.conclusion // "")
]
| if length == 1 then .[0] else "unknown" end
' <<<"${job_json}" 2>>"${api_error_file}")" || {
candidate_ambiguous=true
continue
}
else
recovery_status=$?
if [[ "${recovery_status}" -eq 3 ]]; then exit 1; fi
fi
fi
;;
esac
fi
if [[ "${preserve_step_conclusion}" == "unknown" ||
-z "${preserve_step_conclusion}" ]]; then
if [[ "${check_status}" == "completed" ]]; then
Expand Down
38 changes: 32 additions & 6 deletions .github/actions/preserve-required-gate/resolve-deferred-gate.sh
Original file line number Diff line number Diff line change
Expand Up @@ -231,12 +231,13 @@ if [[ "${resolution_phase}" == reconcile ]]; then
selected_job="$(jq -r .source_job_id <<<"${selected}")"
selected_check="$(jq -r .source_check_run_id <<<"${selected}")"
selected_attempt="$(jq -r .source_run_attempt <<<"${selected}")"
selected_source_title="${workflow_name} pr-${pr_number} base-${base_sha} head-${head_sha}"
selected_source_native() {
local run job check
local run job check preserve_step_state recovered
run="$(gh api --method GET "/repos/${GITHUB_REPOSITORY}/actions/runs/${selected_run}/attempts/${selected_attempt}")" || return 1
jq -e --argjson id "${selected_run}" --argjson attempt "${selected_attempt}" --argjson workflow "${workflow_id}" \
--arg repo "${GITHUB_REPOSITORY}" --arg head_repo "${head_repository}" --arg head "${head_sha}" \
--arg branch "${head_branch}" --arg title "${source_title}" --arg name "${workflow_name}" \
--arg branch "${head_branch}" --arg title "${selected_source_title}" --arg name "${workflow_name}" \
--arg path "${workflow_file}" --argjson pr "${pr_number}" '
.id == $id and .run_attempt == $attempt and .workflow_id == $workflow and
.repository.full_name == $repo and .head_repository.full_name == $head_repo and
Expand All @@ -250,14 +251,39 @@ if [[ "${resolution_phase}" == reconcile ]]; then
--arg name "${gate_name}" --arg head "${head_sha}" \
--arg url "https://api.github.com/repos/${GITHUB_REPOSITORY}/check-runs/${selected_check}" '
.id == $id and .run_id == $run and .run_attempt == $attempt and .head_sha == $head and
.name == $name and .status == "completed" and .conclusion == "success" and .check_run_url == $url and
([.steps[]? | select(.name == "Preserve successful required gate on metadata-only edit" and
.conclusion == "skipped")] | length == 1)' <<<"${job}" >/dev/null || return 1
.name == $name and .status == "completed" and .conclusion == "success" and .check_run_url == $url' \
<<<"${job}" >/dev/null || return 1
preserve_step_state="$(jq -er '
if .steps == null then "absent"
elif (.steps | type) != "array" or any(.steps[]; type != "object" or (.name | type) != "string") then
error("malformed selected job steps")
else
[.steps[] | select(.name == "Preserve successful required gate on metadata-only edit")] as $matches
| if ($matches | length) == 0 then "absent"
elif ($matches | length) > 1 then "duplicate"
elif $matches[0].status == "completed" and $matches[0].conclusion == "skipped" then "skipped"
else "conflict" end
end
' <<<"${job}")" || return 1
if [[ "${preserve_step_state}" == absent ]]; then
[[ "${workflow_file}" == ".github/workflows/ci.yml" &&
"${workflow_name}" == "CI — Validation" && "${gate_name}" == "Validation Gate" ]] || return 1
recovered="$(bash "$(dirname "${BASH_SOURCE[0]}")/validate-exact-attempt-gate.sh" \
"${selected_run}" "${selected_attempt}" "${selected_job}" "${selected_check}" \
"${gate_name}" "${workflow_id}" "${workflow_name}" "${workflow_file}" \
"${selected_source_title}" "${pr_number}" "${base_sha}" "${head_sha}" \
"${head_branch}" "${GITHUB_REPOSITORY}" "${head_repository}" \
completed success "" "" "" true)" || return 1
printf '%s\n' "${recovered}"
return 0
fi
[[ "${preserve_step_state}" == skipped ]] || return 1
check="$(gh api --method GET "/repos/${GITHUB_REPOSITORY}/check-runs/${selected_check}")" || return 1
jq -e --argjson id "${selected_check}" --arg head "${head_sha}" --arg name "${gate_name}" '
.id == $id and .app.id == 15368 and .app.slug == "github-actions" and .head_sha == $head and
.name == $name and .status == "completed" and .conclusion == "success"' <<<"${check}" >/dev/null || return 1
jq -cn --argjson run "${run}" --argjson job "${job}" --argjson check "${check}" '{run: $run, job: $job, check: $check}'
jq -cnS --argjson run "${run}" --argjson job "${job}" --argjson check "${check}" \
'{run: $run, jobs: null, job: $job, check: $check}'
}
selected_native="$(selected_source_native)" || fail_closed "Selected current-base source lacks independent native proof."
terminal_state=success
Expand Down
138 changes: 138 additions & 0 deletions .github/actions/preserve-required-gate/validate-exact-attempt-gate.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
#!/usr/bin/env bash
set -euo pipefail

if [[ "$#" -ne 21 ]]; then
echo "Malformed or unbound exact-attempt evidence: expected 21 identity arguments." >&2
exit 2
fi
run_id="$1"
attempt="$2"
job_id="$3"
check_id="$4"
gate_name="$5"
workflow_id="$6"
workflow_name="$7"
workflow_path="$8"
display_title="$9"
pr_number="${10}"
base_sha="${11}"
head_sha="${12}"
head_branch="${13}"
repository="${14}"
head_repository="${15}"
expected_status="${16}"
expected_conclusion="${17}"
expected_details_url="${18}"
expected_started_at="${19}"
expected_completed_at="${20}"
require_skipped="${21}"

fail_evidence() {
echo "Malformed or unbound exact-attempt evidence: $1" >&2
exit 2
}
fail_api() {
echo "GitHub API error while reading exact-attempt $1." >&2
exit 1
}
for identity in "$run_id" "$attempt" "$job_id" "$check_id" "$workflow_id" "$pr_number"; do
[[ "$identity" =~ ^[1-9][0-9]*$ ]] || fail_evidence "non-positive native identity"
done
for sha in "$base_sha" "$head_sha"; do
[[ "$sha" =~ ^[0-9A-Fa-f]{40}$ ]] || fail_evidence "invalid PR tuple SHA"
done
[[ "$expected_status" == completed && "$require_skipped" =~ ^(true|false)$ ]] ||
fail_evidence "unsupported selected gate state"
[[ -n "$gate_name" && -n "$workflow_name" && -n "$workflow_path" && -n "$display_title" &&
-n "$head_branch" && -n "$repository" && -n "$head_repository" ]] || fail_evidence "incomplete expected source tuple"

if ! attempt_run="$(gh api --method GET "/repos/${repository}/actions/runs/${run_id}/attempts/${attempt}" 2>/dev/null)"; then
fail_api "workflow run"
fi
jq -e --argjson id "$run_id" --argjson attempt "$attempt" --argjson workflow "$workflow_id" \
--arg repo "$repository" --arg head_repo "$head_repository" --arg name "$workflow_name" \
--arg path "$workflow_path" --arg title "$display_title" --arg branch "$head_branch" \
--arg head "$head_sha" --arg base "$base_sha" --argjson pr "$pr_number" '
.id == $id and .run_attempt == $attempt and .workflow_id == $workflow and
(.name == $name or .name == $title) and
(.path == $path or (.path | startswith($path + "@") and length > ($path | length) + 1)) and
.display_title == $title and .repository.full_name == $repo and
.head_repository.full_name == $head_repo and
.head_sha == $head and .head_branch == $branch and .event == "pull_request" and
.status == "completed" and (.pull_requests | type) == "array" and
((.pull_requests | length) == 0 or
(any(.pull_requests[]; .number == $pr) and
all(.pull_requests[] | select(.number == $pr);
((.base.sha // $base) == $base and (.head.sha // $head) == $head))) )
' <<<"$attempt_run" >/dev/null || fail_evidence "workflow attempt identity or PR tuple differs"

if ! attempt_jobs="$(gh api --method GET "/repos/${repository}/actions/runs/${run_id}/attempts/${attempt}/jobs" \
-f per_page=100 2>/dev/null)"; then
fail_api "complete workflow job snapshot"
fi
attempt_job="$(jq -cer --argjson run "$run_id" --argjson attempt "$attempt" --argjson job "$job_id" \
--argjson check "$check_id" --arg repo "$repository" --arg head "$head_sha" \
--arg name "$workflow_name" --arg title "$display_title" --arg branch "$head_branch" --arg gate "$gate_name" \
--arg status "$expected_status" --arg conclusion "$expected_conclusion" \
--arg details "$expected_details_url" --arg started "$expected_started_at" \
--arg completed "$expected_completed_at" --argjson require_skipped "$([[ "$require_skipped" == true ]] && echo true || echo false)" '
if type != "object" or (.jobs | type) != "array" then error("malformed attempt jobs") else . end
| .total_count as $total | .jobs as $jobs
| if ($total | type) != "number" or $total < 1 or $total > 100 or $total != ($total | floor) or
($jobs | length) != $total or
any($jobs[]; type != "object" or (.id | type) != "number" or .id < 1 or .id != (.id | floor)) or
([$jobs[].id] | unique | length) != $total or
any($jobs[]; .run_id != $run or .run_attempt != $attempt or .head_sha != $head or .head_branch != $branch or
(.workflow_name != $name and .workflow_name != $title) or
((.check_run_url // "") | startswith("https://api.github.com/repos/" + $repo + "/check-runs/") | not) or
(((.check_run_url // "") | ltrimstr("https://api.github.com/repos/" + $repo + "/check-runs/")) |
test("^[1-9][0-9]*$") | not)) then
error("incomplete or unbound attempt jobs")
else
[$jobs[] | select(.name == $gate)] as $gate_jobs
| [$jobs[] | select(.id == $job)] as $matches
| if ($gate_jobs | length) != 1 or ($matches | length) != 1 or $gate_jobs[0].id != $job then
error("attempt gate identity is not unique")
else $matches[0] end
| . as $selected
| ($selected.html_url // "") as $html
| ("https://github.com/" + $repo + "/actions/runs/" + ($run | tostring) + "/job/") as $prefix
| if .name != $gate or .run_id != $run or .run_attempt != $attempt or .head_sha != $head or
(.workflow_name != $name and .workflow_name != $title) or
.check_run_url != ("https://api.github.com/repos/" + $repo + "/check-runs/" + ($check | tostring)) or
(($html | startswith($prefix)) | not) or
(($html | ltrimstr($prefix) | (try capture("^(?<id>[1-9][0-9]*)([?#].*)?$").id catch "")) != ($job | tostring)) or
.status != $status or (.conclusion // "") != $conclusion or
($details != "" and $html != $details) or
($started != "" and (.started_at // "") != $started) or
($completed != "" and (.completed_at // "") != $completed) or
(.steps | type) != "array" then
error("attempt gate identity differs")
else . end
| [.steps[] | select(.name == "Preserve successful required gate on metadata-only edit")] as $preserve
| if ($preserve | length) > 1 or
($require_skipped and (($preserve | length) != 1 or $preserve[0].status != "completed" or $preserve[0].conclusion != "skipped")) then
error("attempt preservation-step evidence differs")
else . end
end
' <<<"$attempt_jobs")" || fail_evidence "complete structured workflow jobs or selected gate differ"

if ! check="$(gh api --method GET "/repos/${repository}/check-runs/${check_id}" 2>/dev/null)"; then
fail_api "selected native CheckRun"
fi
jq -e --argjson id "$check_id" --argjson job "$attempt_job" --arg head "$head_sha" \
--arg name "$gate_name" --arg status "$expected_status" --arg conclusion "$expected_conclusion" \
--arg details "$expected_details_url" --arg started "$expected_started_at" --arg completed "$expected_completed_at" '
.id == $id and .app.id == 15368 and .app.slug == "github-actions" and
.head_sha == $head and .name == $name and .status == $status and
(.conclusion // "") == $conclusion and .details_url == $job.html_url and
(.started_at // "") == ($job.started_at // "") and
(.completed_at // "") == ($job.completed_at // "") and
($details == "" or .details_url == $details) and
($started == "" or (.started_at // "") == $started) and
($completed == "" or (.completed_at // "") == $completed)
' <<<"$check" >/dev/null || fail_evidence "selected native CheckRun differs from its exact-attempt gate job"

jq -cnS --argjson run "$attempt_run" --argjson jobs "$attempt_jobs" \
--argjson job "$attempt_job" --argjson check "$check" \
'{run: $run, jobs: ($jobs.jobs | sort_by(.id)), job: $job, check: $check}'
Loading
Loading