diff --git a/.github/actionlint.yaml b/.github/actionlint.yaml deleted file mode 100644 index 8d5904ae..00000000 --- a/.github/actionlint.yaml +++ /dev/null @@ -1,6 +0,0 @@ -paths: - .github/workflows/sfl-pr-review.lock.yml: - ignore: - # gh-aw v0.86.2 emits this supported runtime extension. Keep the ignore - # scoped to the generated reviewer lock and the exact unknown-key error. - - '^unexpected key "queue" for "concurrency" section\. expected one of "cancel-in-progress", "group"$' diff --git a/.github/workflows/sfl-pr-review-auto.yml b/.github/workflows/sfl-pr-review-auto.yml index fa7065fb..9badb5aa 100644 --- a/.github/workflows/sfl-pr-review-auto.yml +++ b/.github/workflows/sfl-pr-review-auto.yml @@ -1,857 +1,1168 @@ -# HemSoft SFL reviewer platform v2 -name: SFL PR Review Auto Trigger +# Deployed from HemSoft/set-it-free-loop. The deployment command replaces this +# source pin with the immutable source SHA. +# Source: HemSoft/set-it-free-loop/deployment/infrastructure/sfl-pr-review-auto.yml@main +name: SFL Codex Review Observer +run-name: >- + ${{ github.event_name == 'issue_comment' && + startsWith(github.event.comment.body, '@codex review') && + format('SFL Codex review request #{0}', github.event.issue.number) || + format('SFL {0} event', github.event_name) }} on: + push: + branches: [main] issue_comment: types: [created] - pull_request_target: - types: [opened, synchronize, reopened, ready_for_review, edited, review_requested, labeled] pull_request_review: types: [submitted] + pull_request_target: + types: [opened, reopened, edited, synchronize] + +permissions: {} -concurrency: - group: sfl-pr-review-auto-${{ github.event.pull_request.number || github.event.issue.number }}-${{ github.event.pull_request.head.sha || github.event.comment.id }}-${{ github.event_name }}-${{ github.event.action }}-${{ github.event.action == 'labeled' && github.event.label.name == 'sfl-review' && 'explicit' || 'standard' }} - cancel-in-progress: false +env: + SFL_REVIEW_BASE_BRANCH: main jobs: - comment-command: + invalidate-review-request: + name: Invalidate prior gate for registered review request if: >- vars.SFL_ENABLED != 'false' && github.event_name == 'issue_comment' && - github.event.action == 'created' && github.event.issue.pull_request && - github.event.comment.body == '@sfl-app review' && - contains(fromJSON('["OWNER","MEMBER","COLLABORATOR"]'), github.event.comment.author_association) + github.event.sender.login == github.repository_owner && + startsWith(github.event.comment.body, '@codex review') runs-on: ubuntu-latest - timeout-minutes: 10 - outputs: - base-sha: ${{ steps.pull-request.outputs.base-sha }} - expected-review-run-id: ${{ steps.request-review.outputs.review-run-id }} - head-sha: ${{ steps.pull-request.outputs.head-sha }} - pr-number: ${{ github.event.issue.number }} - reviewer-installed: 'true' + timeout-minutes: 5 permissions: + checks: write contents: read + issues: read pull-requests: read + statuses: read steps: - - name: Configure GH_HOST for enterprise compatibility - shell: bash - run: | - GH_HOST="${GITHUB_SERVER_URL#https://}" - GH_HOST="${GH_HOST#http://}" - echo "GH_HOST=${GH_HOST}" >> "$GITHUB_ENV" - - - name: Validate command and seal pull request state - id: pull-request - env: - DEFAULT_BRANCH: ${{ github.event.repository.default_branch }} - GH_TOKEN: ${{ github.token }} - PR_NUMBER: ${{ github.event.issue.number }} - REPOSITORY: ${{ github.repository }} - TRUSTED_REF: ${{ github.event.repository.default_branch }} - run: | - set -euo pipefail - REVIEWER_WORKFLOW=$(gh api \ - --method GET \ - "repos/${REPOSITORY}/contents/.github/workflows/sfl-pr-review.lock.yml" \ - -f "ref=${TRUSTED_REF}" \ - -H "Accept: application/vnd.github.raw+json") - if ! grep -qE '^\s+dispatch_id:\s*$' <<< "$REVIEWER_WORKFLOW"; then - echo "::error::The installed reviewer does not support exact dispatch correlation" - exit 1 - fi - if ! grep -qE '^\s+review_effort:\s*$' <<< "$REVIEWER_WORKFLOW"; then - echo "::error::The installed reviewer does not support selected review effort" - exit 1 - fi - - PR=$(gh api "repos/${REPOSITORY}/pulls/${PR_NUMBER}") - BASE_REF=$(jq -r '.base.ref // ""' <<< "$PR") - BASE_SHA=$(jq -r '.base.sha // ""' <<< "$PR") - DRAFT=$(jq -r 'if .draft == false then false else true end' <<< "$PR") - HEAD_REPOSITORY=$(jq -r '.head.repo.full_name // ""' <<< "$PR") - HEAD_SHA=$(jq -r '.head.sha // ""' <<< "$PR") - REVIEW_EFFORT=$(jq -r ' - [.labels[]?.name] as $labels - | if ($labels | index("sfl-effort:high")) then "high" - elif ($labels | index("sfl-effort:medium")) then "medium" - else "low" - end - ' <<< "$PR") - STATE=$(jq -r '.state // ""' <<< "$PR") - - if [ "$STATE" != "open" ] || [ "$DRAFT" = "true" ]; then - echo "::error::SFL comment review requires an open, ready pull request" - exit 1 - fi - if [ "$HEAD_REPOSITORY" != "$REPOSITORY" ]; then - echo "::error::SFL comment review does not execute untrusted fork code" - exit 1 - fi - if [ "$BASE_REF" != "$DEFAULT_BRANCH" ]; then - echo "::error::SFL comment review only validates the default branch" - exit 1 - fi - if ! [[ "$BASE_SHA" =~ ^[0-9a-f]{40}$ ]] || ! [[ "$HEAD_SHA" =~ ^[0-9a-f]{40}$ ]]; then - echo "::error::GitHub returned an invalid pull request commit" - exit 1 - fi - - echo "base-sha=${BASE_SHA}" >> "$GITHUB_OUTPUT" - echo "head-sha=${HEAD_SHA}" >> "$GITHUB_OUTPUT" - echo "review-effort=${REVIEW_EFFORT}" >> "$GITHUB_OUTPUT" - - - name: Mint SFL GitHub App token - id: app-token - uses: actions/create-github-app-token@bcd2ba49218906704ab6c1aa796996da409d3eb1 # v3.2.0 + - name: Fail the gate while a registered Codex review is pending + uses: actions/github-script@3a2844b7e9c422d3c10d287c895573f7108da1b3 # v9.0.0 with: - client-id: ${{ vars.SFL_APP_CLIENT_ID }} - private-key: ${{ secrets.SFL_APP_PRIVATE_KEY }} - owner: ${{ github.repository_owner }} - repositories: ${{ github.event.repository.name }} - permission-actions: write - permission-contents: read - permission-pull-requests: write - - - name: Request review and acknowledge the exact run - id: request-review - env: - BASE_SHA: ${{ steps.pull-request.outputs.base-sha }} - COMMAND_COMMENT_ID: ${{ github.event.comment.id }} - GH_TOKEN: ${{ steps.app-token.outputs.token }} - HEAD_SHA: ${{ steps.pull-request.outputs.head-sha }} - PR_NUMBER: ${{ github.event.issue.number }} - REPOSITORY: ${{ github.repository }} - REVIEW_EFFORT: ${{ steps.pull-request.outputs.review-effort }} - TRUSTED_REF: ${{ github.event.repository.default_branch }} - run: | - set -euo pipefail - DISPATCH_ID="command-${COMMAND_COMMENT_ID}" - MARKER="" - EXPECTED_TITLE="SFL PR Review #${PR_NUMBER} ${BASE_SHA}:${HEAD_SHA} retry=" - - # BEGIN TESTABLE COMMENT COMMAND LOOKUP - find_command_acknowledgement() { - local marker="$1" - jq -s -r \ - --arg marker "$marker" ' - [add[] - | select((.user.login // "") | test("^sfl-app\\[bot\\]$"; "i")) - | select((.body // "") | contains($marker))] - | sort_by(.id) - | last - | select(. != null) - | @base64 - ' - } - - find_command_run() { - local expected_title="$1" - local dispatch_id="$2" - jq -r \ - --arg dispatch_id "$dispatch_id" \ - --arg title "$expected_title" ' - [.workflow_runs[] - | select(.display_title | startswith($title)) - | select(.display_title | endswith(" dispatch=" + $dispatch_id))] - | sort_by(.id) - | last - | .id // empty - ' - } - - command_acknowledgement_complete() { - jq -e '.body | test("\\[Actions run [1-9][0-9]*\\]\\(")' > /dev/null - } - - command_acknowledgement_run_id() { - jq -r '.body | (capture("\\[Actions run (?[1-9][0-9]*)\\]\\(")? | .id) // empty' - } - - command_run_matches() { - local expected_title="$1" - local dispatch_id="$2" - jq -e \ - --arg dispatch_id "$dispatch_id" \ - --arg title "$expected_title" ' - .path == ".github/workflows/sfl-pr-review.lock.yml" - and .event == "workflow_dispatch" - and ((.display_title // "") | startswith($title)) - and ((.display_title // "") | endswith(" dispatch=" + $dispatch_id)) - ' > /dev/null - } - # END TESTABLE COMMENT COMMAND LOOKUP - - ACKNOWLEDGEMENT=$(gh api \ - "repos/${REPOSITORY}/issues/${PR_NUMBER}/comments?per_page=100" \ - --paginate \ - | find_command_acknowledgement "$MARKER") - ACKNOWLEDGEMENT_ID="" - if [ -n "$ACKNOWLEDGEMENT" ]; then - ACKNOWLEDGEMENT_JSON=$(base64 --decode <<< "$ACKNOWLEDGEMENT") - ACKNOWLEDGEMENT_ID=$(jq -r '.id // empty' <<< "$ACKNOWLEDGEMENT_JSON") - if command_acknowledgement_complete <<< "$ACKNOWLEDGEMENT_JSON"; then - ACKNOWLEDGED_RUN_ID=$(command_acknowledgement_run_id <<< "$ACKNOWLEDGEMENT_JSON") - if ! [[ "$ACKNOWLEDGED_RUN_ID" =~ ^[1-9][0-9]*$ ]]; then - echo "::error::Completed SFL acknowledgement does not identify its exact run" - exit 1 - fi - if ACKNOWLEDGED_RUN=$(gh api \ - "repos/${REPOSITORY}/actions/runs/${ACKNOWLEDGED_RUN_ID}") \ - && command_run_matches \ - "$EXPECTED_TITLE" \ - "$DISPATCH_ID" <<< "$ACKNOWLEDGED_RUN"; then - echo "review-run-id=${ACKNOWLEDGED_RUN_ID}" >> "$GITHUB_OUTPUT" - echo "Command comment ${COMMAND_COMMENT_ID} was already acknowledged for ${HEAD_SHA}" - exit 0 - fi - echo "Ignoring stale SFL acknowledgement run ${ACKNOWLEDGED_RUN_ID}; it does not match ${BASE_SHA}:${HEAD_SHA}" - fi - else - PENDING_BODY=$(printf '%s\nSFL review request accepted for `%s`. Resolving the Actions run…' \ - "$MARKER" "$HEAD_SHA") - ACKNOWLEDGEMENT_ID=$(jq -nc --arg body "$PENDING_BODY" '{body: $body}' \ - | gh api --method POST \ - "repos/${REPOSITORY}/issues/${PR_NUMBER}/comments" \ - --input - \ - | jq -r '.id // empty') - fi - if ! [[ "$ACKNOWLEDGEMENT_ID" =~ ^[1-9][0-9]*$ ]]; then - echo "::error::GitHub did not return the SFL acknowledgement comment ID" - exit 1 - fi - - REVIEW_RUN_ID=$(gh api \ - "repos/${REPOSITORY}/actions/workflows/sfl-pr-review.lock.yml/runs?event=workflow_dispatch&per_page=100" \ - | find_command_run "$EXPECTED_TITLE" "$DISPATCH_ID") - if ! [[ "$REVIEW_RUN_ID" =~ ^[1-9][0-9]*$ ]]; then - AW_CONTEXT=$(jq -cn \ - --arg base_sha "${BASE_SHA}" \ - --arg head_sha "${HEAD_SHA}" \ - --argjson item_number "${PR_NUMBER}" \ - '{item_type: "pull_request", item_number: $item_number, base_sha: $base_sha, head_sha: $head_sha}') - gh api --method POST \ - "repos/${REPOSITORY}/actions/workflows/sfl-pr-review.lock.yml/dispatches" \ - -f "ref=${TRUSTED_REF}" \ - -f "inputs[item_number]=${PR_NUMBER}" \ - -f "inputs[aw_context]=${AW_CONTEXT}" \ - -f "inputs[base_sha]=${BASE_SHA}" \ - -f "inputs[head_sha]=${HEAD_SHA}" \ - -f "inputs[retry_count]=0" \ - -f "inputs[review_effort]=${REVIEW_EFFORT}" \ - -f "inputs[dispatch_id]=${DISPATCH_ID}" - echo "Requested SFL review for PR #${PR_NUMBER} at ${HEAD_SHA} with dispatch ${DISPATCH_ID}" - - for ATTEMPT in $(seq 1 120); do - REVIEW_RUN_ID=$(gh api \ - "repos/${REPOSITORY}/actions/workflows/sfl-pr-review.lock.yml/runs?event=workflow_dispatch&per_page=100" \ - | find_command_run "$EXPECTED_TITLE" "$DISPATCH_ID") - if [[ "$REVIEW_RUN_ID" =~ ^[1-9][0-9]*$ ]]; then - break - fi - sleep 2 - done - fi - if ! [[ "$REVIEW_RUN_ID" =~ ^[1-9][0-9]*$ ]]; then - COMMAND_RUN_URL="${GITHUB_SERVER_URL}/${REPOSITORY}/actions/runs/${GITHUB_RUN_ID}" - FAILURE_BODY=$(printf '%s\nSFL review was requested for `%s`, but its Actions run could not be correlated. [Command run](%s)' \ - "$MARKER" "$HEAD_SHA" "$COMMAND_RUN_URL") - jq -nc --arg body "$FAILURE_BODY" '{body: $body}' \ - | gh api --method PATCH \ - "repos/${REPOSITORY}/issues/comments/${ACKNOWLEDGEMENT_ID}" \ - --input - > /dev/null - echo "::error::Could not resolve the SFL review run requested by comment ${COMMAND_COMMENT_ID}" - exit 1 - fi - echo "review-run-id=${REVIEW_RUN_ID}" >> "$GITHUB_OUTPUT" + github-token: ${{ github.token }} + script: | + const {owner, repo} = context.repo; + const pullNumber = context.payload.issue.number; + const commentId = context.payload.comment.id; + const comment = await github.rest.issues.getComment({ + owner, repo, comment_id: commentId, + }); + if (comment.data.updated_at !== comment.data.created_at) { + core.info("Edited Codex review requests are not accepted"); + return; + } + const pull = await github.rest.pulls.get({ + owner, repo, pull_number: pullNumber, + }); + const defaultBranch = context.payload.repository.default_branch; + const headRepo = pull.data.head.repo && pull.data.head.repo.full_name; + if (pull.data.state !== "open" || + pull.data.base.ref !== defaultBranch || + !headRepo || + headRepo.toLowerCase() !== `${owner}/${repo}`.toLowerCase()) { + core.info("Codex review request does not target a supported open pull request"); + return; + } + const currentHead = pull.data.head.sha; + const currentBase = pull.data.base.sha; + const reviewerChecks = async () => github.paginate( + github.rest.checks.listForRef, + { + owner, + repo, + ref: currentHead, + check_name: "SFL Reviewer Gate Runner", + filter: "all", + per_page: 100, + }, + response => response.data.check_runs, + ); + const reviewContextToken = checks => { + const invalidation = checks.filter( + check => check.app && check.app.id === 15368 && + ((check.external_id || "").startsWith("sfl-codex-review:pull-context:") || + (check.external_id || "").startsWith("sfl-codex-review:base-advance:")), + ).sort((left, right) => right.id - left.id)[0]; + return invalidation ? invalidation.external_id : "none"; + }; + const contextToken = reviewContextToken(await reviewerChecks()); + const marker = ``; + if ((comment.data.body || "").trim() !== `@codex review\n\n${marker}`) { + core.info("Codex review request does not contain the exact current SFL marker"); + return; + } + const targetURL = (comment.data.html_url || "").toLowerCase(); + let registered = false; + for (let attempt = 0; attempt < 12 && !registered; attempt += 1) { + const statuses = await github.paginate( + github.rest.repos.listCommitStatusesForRef, + {owner, repo, ref: currentHead, per_page: 100}, + ); + registered = statuses.some(status => + status.context === "SFL Codex Review Request Registry" && + ((status.creator && status.creator.login) || "").toLowerCase() === owner.toLowerCase() && + (status.target_url || "").toLowerCase() === targetURL, + ); + if (!registered && attempt < 11) { + await new Promise(resolve => setTimeout(resolve, 5000)); + } + } + if (!registered) { + core.info("Codex review request was not registered by gh sfl review"); + return; + } + const confirmedPull = await github.rest.pulls.get({ + owner, repo, pull_number: pullNumber, + }); + const confirmedChecks = await reviewerChecks(); + if (confirmedPull.data.state !== "open" || + confirmedPull.data.head.sha !== currentHead || + confirmedPull.data.base.sha !== currentBase || + reviewContextToken(confirmedChecks) !== contextToken) { + core.info("Pull request context changed before the review request could invalidate the gate"); + return; + } + const terminalRequestIdentity = `:request:${commentId}:at:`; + if (confirmedChecks.some( + check => check.app && check.app.id === 15368 && + check.status === "completed" && + (check.external_id || "").includes(terminalRequestIdentity), + )) { + core.info("This registered Codex review request already has a terminal SFL gate"); + return; + } + const externalId = `sfl-codex-review:request-pending:${commentId}`; + if (confirmedChecks.some( + check => check.app && check.app.id === 15368 && check.external_id === externalId, + )) { + core.info("This registered Codex review request already invalidated the prior gate"); + return; + } + const reason = `Registered Codex review request ${commentId} is pending for pull request #${pullNumber}`; + await github.rest.checks.create({ + owner, + repo, + name: "SFL Reviewer Gate Runner", + head_sha: currentHead, + status: "completed", + conclusion: "failure", + completed_at: new Date().toISOString(), + details_url: comment.data.html_url, + external_id: externalId, + output: {title: "SFL Codex review pending", summary: reason}, + }); + core.setFailed(reason); - RUN_URL="${GITHUB_SERVER_URL}/${REPOSITORY}/actions/runs/${REVIEW_RUN_ID}" - FINAL_BODY=$(printf '%s\nSFL review requested for `%s`: [Actions run %s](%s)' \ - "$MARKER" "$HEAD_SHA" "$REVIEW_RUN_ID" "$RUN_URL") - jq -nc --arg body "$FINAL_BODY" '{body: $body}' \ - | gh api --method PATCH \ - "repos/${REPOSITORY}/issues/comments/${ACKNOWLEDGEMENT_ID}" \ - --input - > /dev/null - dispatch: + observe: + name: Observe authenticated Codex review if: >- - github.event_name != 'issue_comment' && vars.SFL_ENABLED != 'false' && - (github.event.action != 'labeled' - || github.event.label.name == 'sfl-review') + github.event.sender.id == 199175422 && + ((github.event_name == 'issue_comment' && github.event.issue.pull_request) || + github.event_name == 'pull_request_review') + concurrency: + group: sfl-codex-review-serialize-${{ github.repository }}-${{ github.event.issue.number || github.event.pull_request.number }} + cancel-in-progress: false runs-on: ubuntu-latest + timeout-minutes: 5 permissions: + actions: read + checks: write contents: read - outputs: - expected-review-run-id: ${{ steps.dispatch-review.outputs.review-run-id }} - reviewer-installed: ${{ steps.reviewer-contract.outputs.installed }} + issues: read + pull-requests: read + statuses: read steps: - - name: Configure GH_HOST for enterprise compatibility - shell: bash - run: | - GH_HOST="${GITHUB_SERVER_URL#https://}" - GH_HOST="${GH_HOST#http://}" - echo "GH_HOST=${GH_HOST}" >> "$GITHUB_ENV" - - - name: Detect installed reviewer contract - id: reviewer-contract - env: - GH_TOKEN: ${{ github.token }} - REPOSITORY: ${{ github.repository }} - TRUSTED_REF: ${{ github.event.repository.default_branch }} - run: | - set -euo pipefail - error_file=$(mktemp) - if ! base_workflow="$(gh api \ - --method GET \ - "repos/${REPOSITORY}/contents/.github/workflows/sfl-pr-review.lock.yml" \ - -f "ref=${TRUSTED_REF}" \ - -H "Accept: application/vnd.github.raw+json" 2>"$error_file")"; then - error_text=$(cat "$error_file") - rm -f "$error_file" - if grep -q 'HTTP 404' <<< "$error_text"; then - echo "installed=false" >> "$GITHUB_OUTPUT" - echo "Reviewer lock is absent from ${TRUSTED_REF}; treating this as bootstrap deployment" - exit 0 - fi - echo "::error::Could not inspect the default-branch reviewer contract: ${error_text}" >&2 - exit 1 - fi - rm -f "$error_file" - if grep -qE '^\s+dispatch_id:\s*$' <<< "${base_workflow}"; then - echo "installed=true" >> "$GITHUB_OUTPUT" - exit 0 - fi - echo "installed=false" >> "$GITHUB_OUTPUT" - echo "Compatible reviewer lock is not on ${TRUSTED_REF}; treating this as bootstrap deployment" - - - name: Validate review eligibility - if: steps.reviewer-contract.outputs.installed == 'true' - env: - BASE_REF: ${{ github.event.pull_request.base.ref }} - DEFAULT_BRANCH: ${{ github.event.repository.default_branch }} - DRAFT: ${{ github.event.pull_request.draft }} - HEAD_REPOSITORY: ${{ github.event.pull_request.head.repo.full_name }} - REPOSITORY: ${{ github.repository }} - run: | - set -euo pipefail - if [ "$HEAD_REPOSITORY" != "$REPOSITORY" ]; then - echo "::error::SFL required review does not execute untrusted fork code" - exit 1 - fi - if [ "$DRAFT" = "true" ]; then - echo "::error::SFL required review waits until the pull request is ready" - exit 1 - fi - if [ "$BASE_REF" != "$DEFAULT_BRANCH" ]; then - echo "::error::SFL required review only validates the default branch" - exit 1 - fi - - - name: Mint SFL GitHub App token - if: steps.reviewer-contract.outputs.installed == 'true' - id: app-token - uses: actions/create-github-app-token@bcd2ba49218906704ab6c1aa796996da409d3eb1 # v3.2.0 + - name: Validate Codex result and publish immutable-head gate + uses: actions/github-script@3a2844b7e9c422d3c10d287c895573f7108da1b3 # v9.0.0 with: - client-id: ${{ vars.SFL_APP_CLIENT_ID }} - private-key: ${{ secrets.SFL_APP_PRIVATE_KEY }} - owner: ${{ github.repository_owner }} - repositories: ${{ github.event.repository.name }} - permission-actions: write - permission-contents: read - permission-pull-requests: write - - - name: Reset stale SFL review state after a push - if: >- - steps.reviewer-contract.outputs.installed == 'true' - && github.event.action == 'synchronize' - && contains(github.event.pull_request.labels.*.name, 'sfl-pr') - env: - GH_TOKEN: ${{ steps.app-token.outputs.token }} - PR_NUMBER: ${{ github.event.pull_request.number }} - REPOSITORY: ${{ github.repository }} - run: | - set -euo pipefail - LIVE_LABELS=$(gh api "repos/${REPOSITORY}/issues/${PR_NUMBER}/labels?per_page=100" \ - --paginate \ - --jq '.[].name') - if ! grep -Eq '^(sfl-done|sfl-needs-work)$' <<< "$LIVE_LABELS"; then - echo "PR #${PR_NUMBER} has no stale terminal review state to reset" - exit 0 - fi - gh api --method POST "repos/${REPOSITORY}/issues/${PR_NUMBER}/labels" \ - -f "labels[]=sfl-ready-for-review" > /dev/null - gh api --method DELETE \ - "repos/${REPOSITORY}/issues/${PR_NUMBER}/labels/sfl-done" 2>/dev/null || true - gh api --method DELETE \ - "repos/${REPOSITORY}/issues/${PR_NUMBER}/labels/sfl-needs-work" 2>/dev/null || true - echo "PR #${PR_NUMBER} received a new head — reset stale review state" + github-token: ${{ github.token }} + script: | + // BEGIN TESTABLE CODEX OBSERVER + function classifyCodexArtifact(input) { + const botId = 199175422; + const botLogin = "chatgpt-codex-connector[bot]"; + const appId = 1144995; + const appSlug = "chatgpt-codex-connector"; + const appOwner = "openai"; + const artifact = input.artifact || {}; + const user = artifact.user || {}; + const app = artifact.performed_via_github_app; + const body = artifact.body || ""; - - name: Dispatch or resolve full-spectrum review - id: dispatch-review - if: steps.reviewer-contract.outputs.installed == 'true' - env: - BASE_SHA: ${{ github.event.pull_request.base.sha }} - EVENT_ACTION: ${{ github.event.action }} - EVENT_NAME: ${{ github.event_name }} - GH_TOKEN: ${{ steps.app-token.outputs.token }} - HEAD_SHA: ${{ github.event.pull_request.head.sha }} - PR_NUMBER: ${{ github.event.pull_request.number }} - REPOSITORY: ${{ github.repository }} - REQUESTED_REVIEWER: ${{ github.event.requested_reviewer.login }} - REVIEW_AUTHOR: ${{ github.event.review.user.login }} - REVIEW_BODY: ${{ github.event.review.body }} - TRUSTED_REF: ${{ github.event.repository.default_branch }} - run: | - set -euo pipefail - - normalize_login() { - printf '%s' "$1" | sed -E 's/\[bot\]$//I' | tr '[:upper:]' '[:lower:]' - } + if (user.id !== botId || user.login !== botLogin) { + return { action: "ignore", reason: "artifact author is not the Codex connector bot" }; + } - # BEGIN TESTABLE REVIEW EFFORT - resolve_review_effort() { - local labels="$1" - local effort - for effort in high medium low; do - if grep -Fxq "sfl-effort:${effort}" <<< "$labels"; then - printf '%s\n' "$effort" - return - fi - done - printf 'low\n' - } - # END TESTABLE REVIEW EFFORT + if (input.eventName === "issue_comment") { + if (!app || app.id !== appId || app.slug !== appSlug || + !app.owner || app.owner.login !== appOwner) { + return { action: "ignore", reason: "comment lacks the expected Codex GitHub App provenance" }; + } + } else if (input.eventName === "pull_request_review") { + // GitHub's review REST object currently omits performed_via_github_app. + // If GitHub begins returning it, require the same immutable App identity. + if (app && (app.id !== appId || app.slug !== appSlug || + !app.owner || app.owner.login !== appOwner)) { + return { action: "ignore", reason: "review has conflicting GitHub App provenance" }; + } + if (artifact.state !== "COMMENTED") { + return { action: "failure", reason: "Codex review has an unexpected state" }; + } + } else { + return { action: "ignore", reason: "unsupported event" }; + } - consume_review_label() { - local labels="$1" - local error_file error_text - if ! grep -Fxq "sfl-review" <<< "$labels"; then - return 0 - fi - error_file=$(mktemp) - if gh api --method DELETE \ - "repos/${REPOSITORY}/issues/${PR_NUMBER}/labels/sfl-review" \ - > /dev/null 2>"$error_file"; then - rm -f "$error_file" - echo "Consumed sfl-review trigger label from PR #${PR_NUMBER}" - return 0 - fi - error_text=$(cat "$error_file") - rm -f "$error_file" - if grep -q 'HTTP 404' <<< "$error_text"; then - echo "sfl-review trigger label is already absent from PR #${PR_NUMBER}" - return 0 - fi - echo "::error::Could not consume sfl-review trigger label: ${error_text}" >&2 - return 1 - } + const currentHead = (input.currentHead || "").toLowerCase(); + const resolvedSha = (input.resolvedSha || "").toLowerCase(); + const reviewCommit = (input.reviewCommit || "").toLowerCase(); + if (reviewCommit && reviewCommit !== currentHead) { + return { action: "ignore", reason: "Codex artifact is stale for the current pull request head" }; + } + if (!resolvedSha) { + if (input.eventName === "issue_comment") { + return { action: "ignore", reason: "Codex comment cannot be bound to a reviewed commit" }; + } + return { action: "failure", reason: "Codex artifact has a malformed or unresolvable reviewed commit" }; + } + if (resolvedSha !== currentHead) { + return { action: "ignore", reason: "Codex artifact is stale for the current pull request head" }; + } + if (!input.requestMatchesCurrentBase) { + return { action: "ignore", reason: "Codex artifact is not bound to a review request for the current head and base" }; + } + if (input.openPullCount !== 1) { + return { action: "failure", reason: "The current head is shared by multiple open pull requests; use a separate branch before requesting review" }; + } - if [ "$EVENT_NAME" = "pull_request_review" ]; then - REVIEW_RUN_ID="" - if [ "$(normalize_login "$REVIEW_AUTHOR")" = "sfl-app" ]; then - REVIEW_RUN_ID=$(printf '%s' "$REVIEW_BODY" \ - | sed -nE 's#.*actions/runs/([0-9]+).*#\1#p' \ - | tail -n 1) - if ! [[ "$REVIEW_RUN_ID" =~ ^[1-9][0-9]*$ ]]; then - echo "::error::SFL review does not identify its workflow run" - exit 1 - fi - fi - echo "review-run-id=${REVIEW_RUN_ID}" >> "$GITHUB_OUTPUT" - exit 0 - fi + if (input.eventName === "issue_comment" && + /^Codex Review: Didn't find any major issues\./.test(body)) { + return { action: "success", reason: "Codex found no major issues on the current head" }; + } + if (input.eventName === "pull_request_review" && + (input.inlineCount > 0 || /automated review suggestions/i.test(body))) { + return { action: "failure", reason: "Codex reported review findings on the current head" }; + } + return { action: "failure", reason: "authenticated Codex artifact has an unknown result shape" }; + } - if [ "$EVENT_ACTION" = "review_requested" ] \ - && [ "$(normalize_login "$REQUESTED_REVIEWER")" != "sfl-app" ]; then - echo "review-run-id=" >> "$GITHUB_OUTPUT" - exit 0 - fi + function isActiveInvalidationRun(run, pullNumber, owner) { + if (!run || run.status === "completed") return false; + if (run.event === "push") return true; + if (run.event === "issue_comment") { + return ((run.actor && run.actor.login) || "").toLowerCase() === owner.toLowerCase() && + run.display_title === `SFL Codex review request #${pullNumber}`; + } + return run.event === "pull_request_target" && + (run.pull_requests || []).some(candidate => candidate.number === pullNumber); + } - PR_LABELS=$(gh api \ - "repos/${REPOSITORY}/issues/${PR_NUMBER}/labels?per_page=100" \ - --paginate --jq '.[].name') - REVIEW_EFFORT=$(resolve_review_effort "$PR_LABELS") + function requestGateExternalIdPrefix(pullNumber, currentBase, contextToken, requestId, requestTime) { + return `sfl-codex-review:pull:${pullNumber}:base:${currentBase.toLowerCase()}:context:${encodeURIComponent(contextToken)}:request:${requestId}:at:${requestTime}`; + } - # Dedup: never dispatch a second standard review for the same sealed - # base/head. Rapid event flurries (labeled/edited/synchronize - # bursts) and recovery workflow dispatches otherwise race into - # duplicate paid runs; review-side concurrency cancels the loser - # only after agent time is already spent. - # - # This check is deliberately not backed by a shared concurrency - # group: a group holds at most one running plus one pending job, so - # a trigger burst could evict a pending recovery transition — a - # correctness hazard worse than an occasional duplicate. The - # residual check-then-dispatch window is sub-second, and its worst - # case is one duplicate run that sealed provenance rejects. - EXPECTED_TITLE="SFL PR Review #${PR_NUMBER} ${BASE_SHA}:${HEAD_SHA} retry=" - # Bounded lookup: active statuses plus successful completions, one - # page each — never paginate the workflow's full run history on every - # pull request event. Reusing a successful exact-state run also - # closes the queue-delay window where an edited event starts after - # the original reviewer has already completed. - # BEGIN TESTABLE DISPATCH DEDUP - reusable_review_statuses() { - printf '%s\n' queued in_progress success - } + function requestGateExternalId(pullNumber, currentBase, contextToken, requestId, requestTime, artifactIdentity) { + return `${requestGateExternalIdPrefix(pullNumber, currentBase, contextToken, requestId, requestTime)}:artifact:${artifactIdentity}`; + } + // END TESTABLE CODEX OBSERVER - find_reusable_review_run() { - local expected_title="$1" - local review_label_present="${2:-false}" - if [ "$review_label_present" = "true" ]; then - cat > /dev/null - return 0 - fi - jq --arg title "${expected_title}" ' - [.workflow_runs[] | select(.display_title | startswith($title))] - | sort_by(.id) - | last - | .id // empty - ' - } - # END TESTABLE DISPATCH DEDUP - REUSABLE_RUN_ID="" - REVIEW_LABEL_PRESENT="false" - if grep -Fxq "sfl-review" <<< "$PR_LABELS"; then - REVIEW_LABEL_PRESENT="true" - fi - # Whichever concurrent event first observes a live explicit request - # must dispatch with the effort labels selected at request time, even - # if another review is already active or successfully completed. A - # stale queued labeled event whose trigger was consumed by another - # event uses ordinary deduplication instead. - for STATUS in $(reusable_review_statuses); do - MATCHING_RUN_ID=$(gh api \ - "repos/${REPOSITORY}/actions/workflows/sfl-pr-review.lock.yml/runs?status=${STATUS}&per_page=100" \ - | find_reusable_review_run \ - "${EXPECTED_TITLE}" "$REVIEW_LABEL_PRESENT") - if [[ "$MATCHING_RUN_ID" =~ ^[1-9][0-9]*$ ]]; then - REUSABLE_RUN_ID="$MATCHING_RUN_ID" - break - fi - done - if [ -n "$REUSABLE_RUN_ID" ]; then - consume_review_label "$PR_LABELS" - echo "Current-state review already active or successfully completed for ${BASE_SHA}:${HEAD_SHA} — skipping duplicate dispatch" - echo "review-run-id=${REUSABLE_RUN_ID}" >> "$GITHUB_OUTPUT" - exit 0 - fi + const {owner, repo} = context.repo; + const eventName = context.eventName; + const pullNumber = eventName === "issue_comment" + ? context.payload.issue.number + : context.payload.pull_request.number; + const artifactId = eventName === "issue_comment" + ? context.payload.comment.id + : context.payload.review.id; + let artifact; + let inlineCount = 0; + let reviewCommit = ""; + if (eventName === "issue_comment") { + const response = await github.rest.issues.getComment({ + owner, repo, comment_id: artifactId, + }); + artifact = response.data; + } else { + const response = await github.rest.pulls.getReview({ + owner, repo, pull_number: pullNumber, review_id: artifactId, + }); + artifact = response.data; + reviewCommit = artifact.commit_id || ""; + const comments = await github.paginate( + "GET /repos/{owner}/{repo}/pulls/{pull_number}/reviews/{review_id}/comments", + {owner, repo, pull_number: pullNumber, review_id: artifactId, per_page: 100}, + ); + inlineCount = comments.length; + } - AW_CONTEXT=$(jq -cn \ - --arg base_sha "${BASE_SHA}" \ - --arg head_sha "${HEAD_SHA}" \ - --argjson item_number "${PR_NUMBER}" \ - '{item_type: "pull_request", item_number: $item_number, base_sha: $base_sha, head_sha: $head_sha}') + const pull = await github.rest.pulls.get({ + owner, repo, pull_number: pullNumber, + }); + if (pull.data.state !== "open") { + core.info("The pull request is no longer open; no SFL gate was published"); + return; + } + const defaultBranch = context.payload.repository && context.payload.repository.default_branch; + if (!defaultBranch) throw new Error("Could not determine the repository default branch"); + if (pull.data.base.ref !== defaultBranch) { + core.info(`Pull requests targeting ${pull.data.base.ref} are unsupported; SFL reviews require the default branch ${defaultBranch}`); + return; + } + const headRepo = pull.data.head.repo && pull.data.head.repo.full_name; + if (!headRepo || headRepo.toLowerCase() !== `${owner}/${repo}`.toLowerCase()) { + core.info("Fork pull requests are unsupported; no SFL gate was published"); + return; + } + const currentBase = pull.data.base.sha; + const currentHead = pull.data.head.sha; + const configuredBaseBranch = process.env.SFL_REVIEW_BASE_BRANCH || ""; + if (!configuredBaseBranch) throw new Error("The deployed SFL review base branch is empty"); + if (defaultBranch !== configuredBaseBranch) { + const reason = `Repository default branch ${defaultBranch} differs from deployed SFL review base ${configuredBaseBranch}; redeploy SFL before accepting review results`; + await github.rest.checks.create({ + owner, + repo, + name: "SFL Reviewer Gate Runner", + head_sha: currentHead, + status: "completed", + conclusion: "failure", + completed_at: new Date().toISOString(), + details_url: artifact.html_url || pull.data.html_url, + external_id: `sfl-codex-review:default-branch-mismatch:${encodeURIComponent(configuredBaseBranch)}:${encodeURIComponent(defaultBranch)}`, + output: {title: "SFL Codex review blocked", summary: reason}, + }); + core.setFailed(reason); + return; + } + const match = (artifact.body || "").match( + /\*\*Reviewed commit:\*\*\s+`([0-9a-f]{7,40})`/i, + ); + let resolvedSha = ""; + if (match) { + try { + const commit = await github.rest.repos.getCommit({ + owner, repo, ref: match[1], + }); + resolvedSha = commit.data.sha; + } catch (error) { + if (error.status !== 404 && error.status !== 422) throw error; + } + } + const artifactBindsCurrentHead = eventName === "issue_comment" + ? Boolean(match) && resolvedSha.toLowerCase() === currentHead.toLowerCase() + : reviewCommit.toLowerCase() === currentHead.toLowerCase() && + (!match || resolvedSha.toLowerCase() === currentHead.toLowerCase()); + const artifactIdentity = `${eventName === "issue_comment" ? "c" : "r"}${artifactId}`; + const artifactSuffix = `:artifact:${artifactIdentity}`; + const artifactChecks = await github.paginate( + github.rest.checks.listForRef, + { + owner, + repo, + ref: currentHead, + check_name: "SFL Reviewer Gate Runner", + filter: "all", + per_page: 100, + }, + response => response.data.check_runs, + ); + const artifactAlreadyConsumed = artifactChecks.some( + check => check.app && check.app.id === 15368 && + (check.external_id || "").endsWith(artifactSuffix), + ); + if (artifactAlreadyConsumed) { + core.info("This exact Codex artifact already completed a review request"); + return; + } + const artifactTime = Date.parse(artifact.created_at || artifact.submitted_at || ""); + const isOwnerRequest = comment => + ((comment.user && comment.user.login) || "").toLowerCase() === owner.toLowerCase(); + const headMarker = `/.exec(comment.body || ""); + return isOwnerRequest(comment) && contextMatch && contextMatch[1] && + (comment.body || "") === `@codex review\n\n${baseMarker}context=${contextMatch[1]} -->` && + Number.isFinite(requestTime) && requestTime <= artifactTime && + Number.isFinite(updatedTime) && updatedTime === requestTime; + }).sort((left, right) => + left.requestTime - right.requestTime || left.comment.id - right.comment.id); + latestPotentialRequest = potentialRequests[potentialRequests.length - 1]; + const visibleRegistrations = registeredRequestIdsFromStatuses(requestStatuses); + latestPotentialRequestRegistered = Boolean(latestPotentialRequest) && + visibleRegistrations.has(latestPotentialRequest.comment.id); + if (latestPotentialRequestRegistered) break; + if (attempt < 11) await new Promise(resolve => setTimeout(resolve, 5000)); + } + if (!latestPotentialRequestRegistered) { + core.info("No registered Codex review request materialized for this artifact"); + return; + } + const registeredRequestIds = registeredRequestIdsFromStatuses(requestStatuses); + // BEGIN TESTABLE REQUEST ELIGIBILITY + const requestContextFromBody = body => { + const contextMatch = /;context=(.*?) -->/.exec(body || ""); + return contextMatch && contextMatch[1] ? contextMatch[1] : ""; + }; + const requestOrder = (left, right) => + left.requestTime - right.requestTime || left.comment.id - right.comment.id; + const requestCandidates = (comments, beforeTime, registrations = registeredRequestIds) => comments.map( + comment => ({ + comment, + requestTime: Date.parse(comment.created_at || ""), + updatedTime: Date.parse(comment.updated_at || ""), + }), + ).filter( + ({comment, requestTime, updatedTime}) => isOwnerRequest(comment) && + registrations.has(comment.id) && + (comment.body || "").includes(baseMarker) && + (comment.body || "") === `@codex review\n\n${baseMarker}context=${requestContextFromBody(comment.body)} -->` && + Number.isFinite(requestTime) && requestTime <= beforeTime && + Number.isFinite(updatedTime) && updatedTime === requestTime, + ).sort(requestOrder); + const hasTerminalGateBefore = (request, contextToken, nextRequestTime, checkRuns) => { + const prefix = requestGateExternalIdPrefix( + pullNumber, + currentBase, + contextToken, + request.comment.id, + request.requestTime, + ); + return checkRuns.some(check => { + const completedTime = Date.parse(check.completed_at || ""); + return check.app && check.app.id === 15368 && + check.status === "completed" && + (check.external_id === prefix || + (check.external_id || "").startsWith(`${prefix}:artifact:`)) && + Number.isFinite(completedTime) && completedTime < nextRequestTime; + }); + }; + const eligibleReviewRequests = ( + comments, checkRuns, beforeTime, registrations = registeredRequestIds, + ) => { + const previousByContext = new Map(); + const eligible = []; + for (const candidate of requestCandidates(comments, beforeTime, registrations)) { + const requestContext = requestContextFromBody(candidate.comment.body); + if (!requestContext) continue; + const previous = previousByContext.get(requestContext); + if (!previous || + hasTerminalGateBefore(previous, requestContext, candidate.requestTime, checkRuns)) { + eligible.push({...candidate, contextToken: requestContext}); + previousByContext.set(requestContext, candidate); + } + } + return eligible; + }; + // END TESTABLE REQUEST ELIGIBILITY + const candidateRequests = Number.isFinite(artifactTime) + ? eligibleReviewRequests(triggerComments, artifactChecks, artifactTime) + .sort((left, right) => -requestOrder(left, right)) + : []; + const pendingRequest = candidateRequests[0]; + if (pendingRequest) { + const pendingRequestPrefix = requestGateExternalIdPrefix( + pullNumber, + currentBase, + pendingRequest.contextToken, + pendingRequest.comment.id, + pendingRequest.requestTime, + ); + const pendingRequestAlreadyTerminal = artifactChecks.some( + check => check.app && check.app.id === 15368 && + check.status === "completed" && + (check.external_id === pendingRequestPrefix || + (check.external_id || "").startsWith(`${pendingRequestPrefix}:artifact:`)), + ); + if (pendingRequestAlreadyTerminal) { + core.info("This Codex review request already has a terminal SFL gate"); + return; + } + } + const activeInvalidationRuns = async () => { + const activeStatuses = ["requested", "queued", "in_progress", "waiting", "pending"]; + const events = ["pull_request_target", "push", "issue_comment"]; + const pages = await Promise.all(activeStatuses.map(status => github.paginate( + github.rest.actions.listWorkflowRuns, + { + owner, + repo, + workflow_id: "sfl-pr-review-auto.yml", + status, + per_page: 100, + }, + response => response.data.workflow_runs, + ))); + return pages.flat().filter( + run => run.id !== context.runId && events.includes(run.event) && + isActiveInvalidationRun(run, pullNumber, owner), + ); + }; + const failForPendingInvalidation = async pendingRun => { + if (!artifactBindsCurrentHead) { + core.info("The Codex artifact does not bind to the current head; it cannot complete the pending request"); + return; + } + const reason = `${pendingRun.event} invalidation run ${pendingRun.id} is still active; the Codex result cannot supersede its invalidation`; + let externalId = `sfl-codex-review:pending-invalidation:${pendingRun.id}:artifact:${artifactIdentity}`; + if (pendingRequest) { + const contextMatch = /;context=(.*?) -->/.exec(pendingRequest.comment.body || ""); + if (contextMatch && contextMatch[1]) { + externalId = requestGateExternalId( + pullNumber, + currentBase, + contextMatch[1], + pendingRequest.comment.id, + pendingRequest.requestTime, + artifactIdentity, + ); + } + } + await github.rest.checks.create({ + owner, + repo, + name: "SFL Reviewer Gate Runner", + head_sha: currentHead, + status: "completed", + conclusion: "failure", + completed_at: new Date().toISOString(), + details_url: artifact.html_url || pull.data.html_url, + external_id: externalId, + output: {title: "SFL Codex review blocked", summary: reason}, + }); + core.setFailed(reason); + }; + const pullLifecycleToken = async () => { + const events = await github.paginate( + "GET /repos/{owner}/{repo}/issues/{issue_number}/events", + {owner, repo, issue_number: pullNumber, per_page: 100}, + ); + return events.filter(event => event.event === "closed" || event.event === "reopened") + .reduce((latest, event) => Math.max(latest, event.id || 0), 0); + }; + const initialLifecycleToken = await pullLifecycleToken(); + let pendingInvalidationRuns = []; + let observedInvalidationRun = false; + for (let attempt = 0; attempt < 12; attempt += 1) { + pendingInvalidationRuns = await activeInvalidationRuns(); + if (pendingInvalidationRuns.length > 0) { + observedInvalidationRun = true; + core.info(`Waiting for ${pendingInvalidationRuns[0].event} invalidation run ${pendingInvalidationRuns[0].id} before publishing the Codex result`); + } else if (observedInvalidationRun || attempt >= 5) { + break; + } else { + core.info("Allowing an invalidation run time to materialize before publishing the Codex result"); + } + await new Promise(resolve => setTimeout(resolve, 5000)); + } + if (pendingInvalidationRuns.length > 0) { + await failForPendingInvalidation(pendingInvalidationRuns[0]); + return; + } + const reviewContextToken = checkRuns => { + const invalidation = checkRuns.filter( + check => check.app && check.app.id === 15368 && + ((check.external_id || "").startsWith("sfl-codex-review:pull-context:") || + (check.external_id || "").startsWith("sfl-codex-review:base-advance:")), + ).sort((left, right) => right.id - left.id)[0]; + return invalidation ? invalidation.external_id : "none"; + }; + const initialChecks = await github.paginate( + github.rest.checks.listForRef, + { + owner, + repo, + ref: currentHead, + check_name: "SFL Reviewer Gate Runner", + filter: "all", + per_page: 100, + }, + response => response.data.check_runs, + ); + const contextToken = reviewContextToken(initialChecks); + const marker = ``; + const conflictingBaseRequest = triggerComments.some( + comment => isOwnerRequest(comment) && + (comment.body || "").startsWith("@codex review") && + (comment.body || "").includes(headMarker) && + !(comment.body || "").includes(baseMarker), + ); + const matchingRequests = candidateRequests.filter( + ({comment}) => (comment.body || "").includes(marker), + ); + const request = matchingRequests[0]; + const requestMatchesCurrentBase = Boolean(request) && !conflictingBaseRequest; + const openPullsForCurrentHead = async () => { + const candidates = await github.paginate(github.rest.pulls.list, { + owner, repo, state: "open", per_page: 100, + }); + return candidates.filter( + candidate => candidate.head && candidate.head.sha === currentHead, + ); + }; + const openPulls = await openPullsForCurrentHead(); + const openPullCount = openPulls.length; - dispatch_args=( - --method POST - "repos/${REPOSITORY}/actions/workflows/sfl-pr-review.lock.yml/dispatches" - -f "ref=${TRUSTED_REF}" - -f "inputs[item_number]=${PR_NUMBER}" - -f "inputs[aw_context]=${AW_CONTEXT}" - ) + let result = classifyCodexArtifact({ + eventName, + currentHead, + resolvedSha, + reviewCommit, + requestMatchesCurrentBase, + openPullCount, + inlineCount, + artifact, + }); + if (request && request.requestTime === artifactTime) { + result = { + action: "failure", + reason: "Codex artifact and registered request have ambiguous same-second ordering; retry the review request", + }; + } + core.info(result.reason); + if (result.action === "ignore") return; - base_workflow="$(gh api \ - --method GET \ - "repos/${REPOSITORY}/contents/.github/workflows/sfl-pr-review.lock.yml" \ - -f "ref=${TRUSTED_REF}" \ - -H "Accept: application/vnd.github.raw+json")" - if grep -qE '^\s+head_sha:\s*$' <<< "${base_workflow}"; then - dispatch_args+=(-f "inputs[head_sha]=${HEAD_SHA}") - fi - if grep -qE '^\s+base_sha:\s*$' <<< "${base_workflow}"; then - dispatch_args+=(-f "inputs[base_sha]=${BASE_SHA}") - fi - if grep -qE '^\s+retry_count:\s*$' <<< "${base_workflow}"; then - dispatch_args+=(-f "inputs[retry_count]=0") - fi - if grep -qE '^\s+review_effort:\s*$' <<< "${base_workflow}"; then - dispatch_args+=(-f "inputs[review_effort]=${REVIEW_EFFORT}") - fi - DISPATCH_ID="auto-${GITHUB_RUN_ID}" - if ! grep -qE '^\s+dispatch_id:\s*$' <<< "${base_workflow}"; then - echo "::error::The installed reviewer does not support exact dispatch correlation" - exit 1 - fi - dispatch_args+=(-f "inputs[dispatch_id]=${DISPATCH_ID}") + const publishPull = await github.rest.pulls.get({ + owner, repo, pull_number: pullNumber, + }); + const publishOpenPulls = await openPullsForCurrentHead(); + if (publishPull.data.state !== "open" || + publishPull.data.head.sha !== currentHead || + publishPull.data.base.sha !== currentBase || + publishOpenPulls.length !== 1 || + publishOpenPulls[0].number !== pullNumber) { + core.info("The pull request context changed before the Codex result could be published"); + return; + } - BEFORE_RUN_ID=$(gh api \ - "repos/${REPOSITORY}/actions/workflows/sfl-pr-review.lock.yml/runs?event=workflow_dispatch&per_page=100" \ - | jq -r \ - --arg title "$EXPECTED_TITLE" ' - [.workflow_runs[] - | select(.display_title | startswith($title))] - | sort_by(.id) - | last - | .id // 0 - ') - gh api "${dispatch_args[@]}" - consume_review_label "$PR_LABELS" - for ATTEMPT in $(seq 1 15); do - REVIEW_RUN_ID=$(gh api \ - "repos/${REPOSITORY}/actions/workflows/sfl-pr-review.lock.yml/runs?event=workflow_dispatch&per_page=100" \ - | jq -r \ - --arg title "$EXPECTED_TITLE" \ - --arg dispatch_id "$DISPATCH_ID" \ - --argjson before_run_id "$BEFORE_RUN_ID" ' - [.workflow_runs[] - | select(.display_title | startswith($title)) - | select(.display_title | endswith(" dispatch=" + $dispatch_id)) - | select(.id > $before_run_id)] - | sort_by(.id) - | last - | .id // empty - ') - if [[ "$REVIEW_RUN_ID" =~ ^[1-9][0-9]*$ ]]; then - echo "review-run-id=${REVIEW_RUN_ID}" >> "$GITHUB_OUTPUT" - exit 0 - fi - sleep 2 - done - echo "::error::Could not resolve the dispatched SFL review run" - exit 1 + const title = result.action === "success" + ? "SFL Codex review passed" + : "SFL Codex review failed"; + const checks = await github.paginate( + github.rest.checks.listForRef, + { + owner, + repo, + ref: currentHead, + check_name: "SFL Reviewer Gate Runner", + filter: "all", + per_page: 100, + }, + response => response.data.check_runs, + ); + const requestTime = request && request.requestTime; + const hasNewerMatchingRequest = (comments, checkRuns, statuses) => + eligibleReviewRequests( + comments, + checkRuns, + Number.POSITIVE_INFINITY, + registeredRequestIdsFromStatuses(statuses), + ).some( + candidate => candidate.contextToken === contextToken && + (candidate.requestTime > requestTime || + (candidate.requestTime === requestTime && + candidate.comment.id > request.comment.id)), + ); + if (reviewContextToken(checks) !== contextToken) { + core.info("The matching Codex request is not bound to the latest pull context"); + return; + } + const finalInvalidationRuns = await activeInvalidationRuns(); + if (finalInvalidationRuns.length > 0) { + await failForPendingInvalidation(finalInvalidationRuns[0]); + return; + } + const latestTriggerComments = await github.paginate(github.rest.issues.listComments, { + owner, repo, issue_number: pullNumber, per_page: 100, + }); + const latestRequestChecks = await github.paginate( + github.rest.checks.listForRef, + { + owner, + repo, + ref: currentHead, + check_name: "SFL Reviewer Gate Runner", + filter: "all", + per_page: 100, + }, + response => response.data.check_runs, + ); + const latestRequestStatuses = await github.paginate( + github.rest.repos.listCommitStatusesForRef, + {owner, repo, ref: currentHead, per_page: 100}, + ); + if (hasNewerMatchingRequest( + latestTriggerComments, latestRequestChecks, latestRequestStatuses, + )) { + core.info("A newer Codex review request superseded this artifact before publication"); + return; + } + const externalIdPrefix = requestGateExternalIdPrefix( + pullNumber, currentBase, contextToken, request.comment.id, requestTime, + ); + const externalId = requestGateExternalId( + pullNumber, currentBase, contextToken, request.comment.id, requestTime, artifactIdentity, + ); + const existing = latestRequestChecks.find( + check => check.app && check.app.id === 15368 && + check.status === "completed" && + (check.external_id === externalIdPrefix || + (check.external_id || "").startsWith(`${externalIdPrefix}:artifact:`)), + ); + if (existing) { + core.info("This Codex review request already has a terminal SFL gate"); + return; + } + const detailsURL = artifact.html_url || pull.data.html_url; + if (result.action !== "success") { + const publishedCompletedAt = new Date().toISOString(); + await github.rest.checks.create({ + owner, + repo, + name: "SFL Reviewer Gate Runner", + head_sha: currentHead, + status: "completed", + conclusion: "failure", + completed_at: publishedCompletedAt, + details_url: detailsURL, + external_id: externalId, + output: {title, summary: result.reason}, + }); + core.setFailed(result.reason); + return; + } + const published = await github.rest.checks.create({ + owner, + repo, + name: "SFL Reviewer Gate Runner", + head_sha: currentHead, + status: "in_progress", + started_at: new Date().toISOString(), + details_url: detailsURL, + external_id: externalId, + output: {title: "SFL Codex review validating", summary: result.reason}, + }); + const publicationState = async () => { + const [ + currentPull, + openPulls, + comments, + checkRuns, + statuses, + lifecycleToken, + invalidationRuns, + ] = await Promise.all([ + github.rest.pulls.get({owner, repo, pull_number: pullNumber}), + openPullsForCurrentHead(), + github.paginate(github.rest.issues.listComments, { + owner, repo, issue_number: pullNumber, per_page: 100, + }), + github.paginate( + github.rest.checks.listForRef, + { + owner, + repo, + ref: currentHead, + check_name: "SFL Reviewer Gate Runner", + filter: "all", + per_page: 100, + }, + response => response.data.check_runs, + ), + github.paginate( + github.rest.repos.listCommitStatusesForRef, + {owner, repo, ref: currentHead, per_page: 100}, + ), + pullLifecycleToken(), + activeInvalidationRuns(), + ]); + return { + pull: currentPull.data, + openPulls, + contextToken: reviewContextToken(checkRuns), + supersededRequest: hasNewerMatchingRequest(comments, checkRuns, statuses), + lifecycleChanged: lifecycleToken !== initialLifecycleToken, + invalidationRuns, + }; + }; + const publicationChanged = state => state.pull.state !== "open" || + state.pull.head.sha !== currentHead || + state.pull.base.sha !== currentBase || + state.openPulls.length !== 1 || + state.openPulls[0].number !== pullNumber || + state.supersededRequest || + state.lifecycleChanged || + state.invalidationRuns.length > 0 || + state.contextToken !== contextToken; + const publicationChangeReason = state => state.supersededRequest + ? "A newer Codex review request superseded this result while it was being published" + : state.lifecycleChanged + ? "The pull request was closed or reopened while the Codex result was being published" + : state.invalidationRuns.length > 0 + ? "An SFL invalidation started while the Codex result was being published" + : "The pull request context changed while the Codex result was being published"; + const failPublishedSuccess = async changedReason => { + await github.rest.checks.update({ + owner, + repo, + check_run_id: published.data.id, + status: "completed", + conclusion: "failure", + completed_at: new Date().toISOString(), + details_url: detailsURL, + external_id: externalId, + output: {title: "SFL Codex review invalidated", summary: changedReason}, + }); + core.setFailed(changedReason); + }; + const confirmedState = await publicationState(); + if (publicationChanged(confirmedState)) { + await failPublishedSuccess(publicationChangeReason(confirmedState)); + return; + } + await github.rest.checks.update({ + owner, + repo, + check_run_id: published.data.id, + status: "completed", + conclusion: "success", + completed_at: new Date().toISOString(), + details_url: detailsURL, + external_id: externalId, + output: {title, summary: result.reason}, + }); + const postSuccessState = await publicationState(); + if (publicationChanged(postSuccessState)) { + await failPublishedSuccess(publicationChangeReason(postSuccessState)); + } - approval: - name: SFL Reviewer Gate Runner - needs: [comment-command, dispatch] + invalidate-pull-context: + name: Invalidate Codex review for changed pull context if: >- - always() && - (github.event.action != 'labeled' || - github.event.label.name == 'sfl-review') && - (github.event_name != 'issue_comment' || - (github.event.action == 'created' && - github.event.issue.pull_request && - github.event.comment.body == '@sfl-app review' && - contains(fromJSON('["OWNER","MEMBER","COLLABORATOR"]'), github.event.comment.author_association))) + github.event_name == 'pull_request_target' && + (github.event.action == 'opened' || + github.event.action == 'reopened' || + github.event.action == 'synchronize' || + github.event.changes.base) runs-on: ubuntu-latest - timeout-minutes: 120 + timeout-minutes: 5 + concurrency: + group: sfl-codex-review-pull-context-${{ github.repository }}-${{ github.event.pull_request.number }} + cancel-in-progress: true permissions: actions: read checks: write contents: read pull-requests: read steps: - - name: Configure GH_HOST for enterprise compatibility - shell: bash - run: | - GH_HOST="${GITHUB_SERVER_URL#https://}" - GH_HOST="${GH_HOST#http://}" - echo "GH_HOST=${GH_HOST}" >> "$GITHUB_ENV" - - - name: Resolve immutable approval context - id: approval-context - env: - BASE_SHA: ${{ github.event.pull_request.base.sha || needs.comment-command.outputs.base-sha }} - EVENT_NAME: ${{ github.event_name }} - GH_TOKEN: ${{ github.token }} - HEAD_SHA: ${{ github.event.pull_request.head.sha || needs.comment-command.outputs.head-sha }} - PR_NUMBER: ${{ github.event.pull_request.number || needs.comment-command.outputs.pr-number || github.event.issue.number }} - REPOSITORY: ${{ github.repository }} - run: | - set -euo pipefail - if [ "$EVENT_NAME" = "issue_comment" ] \ - && { [ -z "$BASE_SHA" ] || [ -z "$HEAD_SHA" ]; }; then - PR=$(gh api "repos/${REPOSITORY}/pulls/${PR_NUMBER}") - BASE_SHA=$(jq -r '.base.sha // ""' <<< "$PR") - HEAD_SHA=$(jq -r '.head.sha // ""' <<< "$PR") - fi - if ! [[ "$PR_NUMBER" =~ ^[1-9][0-9]*$ ]] \ - || ! [[ "$BASE_SHA" =~ ^[0-9a-f]{40}$ ]] \ - || ! [[ "$HEAD_SHA" =~ ^[0-9a-f]{40}$ ]]; then - echo "::error::Could not resolve immutable SFL approval context" - exit 1 - fi - echo "base-sha=${BASE_SHA}" >> "$GITHUB_OUTPUT" - echo "head-sha=${HEAD_SHA}" >> "$GITHUB_OUTPUT" - echo "pr-number=${PR_NUMBER}" >> "$GITHUB_OUTPUT" - - - name: Initialize head approval check - id: approval-check - env: - BASE_SHA: ${{ steps.approval-context.outputs.base-sha }} - GH_TOKEN: ${{ github.token }} - HEAD_SHA: ${{ steps.approval-context.outputs.head-sha }} - PR_NUMBER: ${{ steps.approval-context.outputs.pr-number }} - REPOSITORY: ${{ github.repository }} - run: | - set -euo pipefail - payload=$(jq -nc \ - --arg details_url "${GITHUB_SERVER_URL}/${GITHUB_REPOSITORY}/actions/runs/${GITHUB_RUN_ID}" \ - --arg external_id "sfl-review-approval:${PR_NUMBER}:${BASE_SHA}:${HEAD_SHA}:${GITHUB_RUN_ID}" \ - --arg head_sha "$HEAD_SHA" \ - --arg name "SFL Reviewer Approval" \ - '{ - name: $name, - head_sha: $head_sha, - status: "in_progress", - details_url: $details_url, - external_id: $external_id, - output: { - title: "SFL reviewer gate is running", - summary: "Validating the exact SFL reviewer run for the immutable pull request head." - } - }') - response=$(printf '%s' "$payload" | gh api \ - --method POST \ - "repos/${REPOSITORY}/check-runs" \ - --header "Accept: application/vnd.github+json" \ - --input -) - check_run_id=$(jq -r '.id // empty' <<< "$response") - if ! [[ "$check_run_id" =~ ^[1-9][0-9]*$ ]]; then - echo "::error::GitHub did not return the immutable-head approval check ID" - exit 1 - fi - echo "check-run-id=${check_run_id}" >> "$GITHUB_OUTPUT" - - - name: Wait for exact SFL review run - id: wait-review - continue-on-error: true - env: - BASE_SHA: ${{ steps.approval-context.outputs.base-sha }} - COMMENT_COMMAND_RESULT: ${{ needs.comment-command.result }} - DEFAULT_BRANCH: ${{ github.event.repository.default_branch }} - DISPATCH_RESULT: ${{ needs.dispatch.result }} - EVENT_NAME: ${{ github.event_name }} - EXPECTED_REVIEW_RUN_ID: ${{ needs.dispatch.outputs.expected-review-run-id || needs.comment-command.outputs.expected-review-run-id }} - GH_TOKEN: ${{ github.token }} - HEAD_SHA: ${{ steps.approval-context.outputs.head-sha }} - PR_NUMBER: ${{ steps.approval-context.outputs.pr-number }} - REPOSITORY: ${{ github.repository }} - REVIEWER_INSTALLED: ${{ needs.dispatch.outputs.reviewer-installed || needs.comment-command.outputs.reviewer-installed }} - SFL_ENABLED: ${{ vars.SFL_ENABLED }} - run: | - set -euo pipefail - if [ "$SFL_ENABLED" = "false" ]; then - echo "::error::SFL reviewer maintenance mode is active; required reviewer gate fails closed" - exit 1 - fi - if [ "$EVENT_NAME" = "issue_comment" ] \ - && [ "$COMMENT_COMMAND_RESULT" != "success" ]; then - echo "::error::SFL comment command job concluded ${COMMENT_COMMAND_RESULT}; refusing to bootstrap" - exit 1 - fi - if [ "$EVENT_NAME" != "issue_comment" ] \ - && [ "$DISPATCH_RESULT" != "success" ]; then - echo "::error::SFL dispatch job concluded ${DISPATCH_RESULT}; refusing to bootstrap" - exit 1 - fi - if [ "$REVIEWER_INSTALLED" != "true" ]; then - echo "Reviewer bootstrap deployment detected; gate activates after the compatible lock reaches the default branch" - exit 0 - fi - EXPECTED_TITLE="SFL PR Review #${PR_NUMBER} ${BASE_SHA}:${HEAD_SHA} retry=" - - # BEGIN TESTABLE AUTO GATE RUN VALIDATION - validate_review_run() { - local repository="$1" - local default_branch="$2" - local expected_title="$3" - jq -e \ - --arg repository "$repository" \ - --arg default_branch "$default_branch" \ - --arg expected_title "$expected_title" ' - .path == ".github/workflows/sfl-pr-review.lock.yml" - and .event == "workflow_dispatch" - and .head_repository.full_name == $repository - and .head_branch == $default_branch - and ((.display_title // .name // "") | - startswith($expected_title)) - ' >/dev/null - } - # END TESTABLE AUTO GATE RUN VALIDATION - - if [ -z "$EXPECTED_REVIEW_RUN_ID" ]; then - if [ "$EVENT_NAME" = "issue_comment" ]; then - echo "::error::Could not resolve the exact comment-dispatched SFL review run" - exit 1 - fi - EXPECTED_REVIEW_RUN_ID=$(gh api \ - "repos/${REPOSITORY}/actions/workflows/sfl-pr-review.lock.yml/runs?event=workflow_dispatch&per_page=100" \ - --paginate --jq '.workflow_runs[]' \ - | jq -s -r \ - --arg title "$EXPECTED_TITLE" ' - [.[] | select(.display_title | startswith($title))] - | sort_by(.id) - | last - | .id // empty - ') - fi - if ! [[ "$EXPECTED_REVIEW_RUN_ID" =~ ^[1-9][0-9]*$ ]]; then - echo "::error::Could not resolve the exact SFL review run" - exit 1 - fi - - # 440 attempts at 15 seconds leaves roughly 110 minutes for the - # review, preserving headroom under the 120-minute wrapper ceiling. - for ATTEMPT in $(seq 1 440); do - if ! REVIEW_RUN=$(gh api \ - "repos/${REPOSITORY}/actions/runs/${EXPECTED_REVIEW_RUN_ID}"); then - echo "::warning::Actions run lookup failed (attempt ${ATTEMPT}/440)" - sleep 15 - continue - fi - if ! printf '%s' "$REVIEW_RUN" \ - | validate_review_run \ - "$REPOSITORY" \ - "$DEFAULT_BRANCH" \ - "$EXPECTED_TITLE"; then - echo "::error::Rejected untrusted SFL review run ${EXPECTED_REVIEW_RUN_ID}" - exit 1 - fi + - name: Fail the immutable-head gate until the new diff is reviewed + uses: actions/github-script@3a2844b7e9c422d3c10d287c895573f7108da1b3 # v9.0.0 + with: + github-token: ${{ github.token }} + script: | + // BEGIN TESTABLE INVALIDATION ORDER + function supersedesInvalidation(externalId, eventTime, runId) { + const match = /^sfl-codex-review:(?:pull-context|base-advance):at:(\d+):(\d+)/.exec(externalId || ""); + if (!match) return false; + const candidateTime = Number(match[1]); + const candidateRunId = Number(match[2]); + if (!Number.isSafeInteger(candidateTime) || !Number.isSafeInteger(candidateRunId)) return false; + return candidateTime > eventTime || + (candidateTime === eventTime && candidateRunId > runId); + } + // END TESTABLE INVALIDATION ORDER - STATUS=$(echo "$REVIEW_RUN" | jq -r '.status // ""') - CONCLUSION=$(echo "$REVIEW_RUN" | jq -r '.conclusion // ""') - if [ "$STATUS" != "completed" ]; then - sleep 15 - continue - fi - if [ "$CONCLUSION" = "success" ]; then - echo "Exact SFL review run approved PR #${PR_NUMBER}" - exit 0 - fi - echo "::error::Exact SFL review run concluded ${CONCLUSION:-missing}" - exit 1 - done + const {owner, repo} = context.repo; + const pull = context.payload.pull_request; + const workflowRun = await github.rest.actions.getWorkflowRun({ + owner, repo, run_id: context.runId, + }); + const contextChangeTime = Date.parse(workflowRun.data.created_at || ""); + if (!Number.isFinite(contextChangeTime)) { + throw new Error("Could not determine the pull-context change event time"); + } + const invalidationId = `sfl-codex-review:pull-context:at:${contextChangeTime}:${context.runId}`; + const reviewerChecks = await github.paginate( + github.rest.checks.listForRef, + { + owner, + repo, + ref: pull.head.sha, + check_name: "SFL Reviewer Gate Runner", + filter: "all", + per_page: 100, + }, + response => response.data.check_runs, + ); + const existingInvalidation = reviewerChecks.some( + check => check.app && check.app.id === 15368 && check.external_id === invalidationId, + ); + if (existingInvalidation) { + core.info(`Pull request #${pull.number} already has this exact invalidation; skipping rerun`); + return; + } + const supersedingContext = reviewerChecks.find( + check => check.app && check.app.id === 15368 && + supersedesInvalidation(check.external_id, contextChangeTime, context.runId), + ); + if (supersedingContext) { + core.info(`Pull request #${pull.number} has a newer invalidation context; skipping stale invalidation`); + return; + } + const newerSuccess = reviewerChecks.some( + check => check.app && check.app.id === 15368 && + check.conclusion === "success" && + check.external_id && check.external_id.startsWith( + `sfl-codex-review:pull:${pull.number}:base:${pull.base.sha.toLowerCase()}:`, + ) && + check.external_id.includes(`:context:${encodeURIComponent(invalidationId)}:`), + ); + if (newerSuccess) { + core.info(`Pull request #${pull.number} already has a successful Codex gate completed after this context change`); + return; + } + const livePull = await github.rest.pulls.get({ + owner, repo, pull_number: pull.number, + }); + if (livePull.data.state !== "open" || + livePull.data.head.sha !== pull.head.sha || + livePull.data.base.sha !== pull.base.sha) { + core.info(`Pull request #${pull.number} changed again after this context event; skipping stale invalidation`); + return; + } + const reason = context.payload.action === "edited" + ? `Pull request base changed to ${pull.base.ref} at ${pull.base.sha}; request a new Codex review with gh sfl review --retry.` + : context.payload.action === "synchronize" + ? `Pull request head changed to ${pull.head.sha}; request a Codex review for this exact head and base.` + : `Pull request #${pull.number} opened or reopened for ${pull.base.ref} at ${pull.base.sha}; request a Codex review for this exact pull request context.`; + const finalChecks = await github.paginate( + github.rest.checks.listForRef, + { + owner, + repo, + ref: pull.head.sha, + check_name: "SFL Reviewer Gate Runner", + filter: "all", + per_page: 100, + }, + response => response.data.check_runs, + ); + const finalContextExists = finalChecks.some( + check => check.app && check.app.id === 15368 && + (check.external_id === invalidationId || + supersedesInvalidation(check.external_id, contextChangeTime, context.runId)), + ); + const finalSuccess = finalChecks.some( + check => check.app && check.app.id === 15368 && + check.conclusion === "success" && + check.external_id && check.external_id.startsWith( + `sfl-codex-review:pull:${pull.number}:base:${pull.base.sha.toLowerCase()}:`, + ) && + check.external_id.includes(`:context:${encodeURIComponent(invalidationId)}:`), + ); + if (finalContextExists || finalSuccess) { + core.info(`Pull request #${pull.number} received a newer context or successful Codex gate while invalidation was running`); + return; + } + await github.rest.checks.create({ + owner, + repo, + name: "SFL Reviewer Gate Runner", + head_sha: pull.head.sha, + status: "completed", + conclusion: "failure", + completed_at: new Date().toISOString(), + details_url: pull.html_url, + external_id: invalidationId, + output: {title: "SFL Codex review invalidated", summary: reason}, + }); + core.setFailed(reason); - echo "::error::Timed out waiting for exact SFL review run" - exit 1 + invalidate-base-advance: + name: Invalidate Codex reviews after base advance + if: github.event_name == 'push' + runs-on: ubuntu-latest + timeout-minutes: 30 + concurrency: + group: sfl-codex-review-base-advance-${{ github.repository }}-${{ github.ref }} + cancel-in-progress: true + permissions: + actions: read + checks: write + contents: read + pull-requests: read + steps: + - name: Fail gates for pull requests whose base branch advanced + uses: actions/github-script@3a2844b7e9c422d3c10d287c895573f7108da1b3 # v9.0.0 + with: + github-token: ${{ github.token }} + script: | + // BEGIN TESTABLE INVALIDATION ORDER + function supersedesInvalidation(externalId, eventTime, runId) { + const match = /^sfl-codex-review:(?:pull-context|base-advance):at:(\d+):(\d+)/.exec(externalId || ""); + if (!match) return false; + const candidateTime = Number(match[1]); + const candidateRunId = Number(match[2]); + if (!Number.isSafeInteger(candidateTime) || !Number.isSafeInteger(candidateRunId)) return false; + return candidateTime > eventTime || + (candidateTime === eventTime && candidateRunId > runId); + } + // END TESTABLE INVALIDATION ORDER - - name: Finalize head approval check - if: always() && steps.approval-check.outputs.check-run-id != '' - env: - APPROVAL_CHECK_ID: ${{ steps.approval-check.outputs.check-run-id }} - GH_TOKEN: ${{ github.token }} - HEAD_SHA: ${{ steps.approval-context.outputs.head-sha }} - PR_NUMBER: ${{ steps.approval-context.outputs.pr-number }} - REPOSITORY: ${{ github.repository }} - WAIT_OUTCOME: ${{ steps.wait-review.outcome }} - run: | - set -euo pipefail - conclusion=failure - title="SFL Reviewer Approval failed" - summary="The exact SFL reviewer run did not approve PR #${PR_NUMBER} at ${HEAD_SHA}." - if [ "$WAIT_OUTCOME" = "success" ]; then - conclusion=success - title="SFL Reviewer Approval passed" - summary="The exact SFL reviewer run approved PR #${PR_NUMBER} at ${HEAD_SHA}." - fi - payload=$(jq -nc \ - --arg completed_at "$(date -u +%Y-%m-%dT%H:%M:%SZ)" \ - --arg conclusion "$conclusion" \ - --arg summary "$summary" \ - --arg title "$title" \ - '{ - status: "completed", - conclusion: $conclusion, - completed_at: $completed_at, - output: {title: $title, summary: $summary} - }') - printf '%s' "$payload" | gh api \ - --method PATCH \ - "repos/${REPOSITORY}/check-runs/${APPROVAL_CHECK_ID}" \ - --header "Accept: application/vnd.github+json" \ - --input - > /dev/null - if [ "$conclusion" != "success" ]; then - exit 1 - fi + const {owner, repo} = context.repo; + const configuredBaseBranch = process.env.SFL_REVIEW_BASE_BRANCH || ""; + let baseRef = context.ref.replace(/^refs\/heads\//, ""); + let renamedBase = false; + if (context.payload.deleted) { + const repository = await github.rest.repos.get({owner, repo}); + const defaultBranch = repository.data.default_branch || ""; + if (!defaultBranch) throw new Error("Could not determine the repository default branch after base deletion"); + baseRef = defaultBranch; + renamedBase = defaultBranch !== configuredBaseBranch; + } + const workflowRun = await github.rest.actions.getWorkflowRun({ + owner, repo, run_id: context.runId, + }); + const baseAdvanceTime = Date.parse(workflowRun.data.created_at || ""); + if (!Number.isFinite(baseAdvanceTime)) { + throw new Error("Could not determine the base-advance event time"); + } + const openPulls = await github.paginate(github.rest.pulls.list, { + owner, repo, state: "open", base: baseRef, per_page: 100, + }); + const invalidatePull = async pull => { + if (pull.base.ref !== baseRef) return; + const invalidationId = `sfl-codex-review:base-advance:at:${baseAdvanceTime}:${context.runId}:${pull.number}`; + const reviewerChecks = await github.paginate( + github.rest.checks.listForRef, + { + owner, + repo, + ref: pull.head.sha, + check_name: "SFL Reviewer Gate Runner", + filter: "all", + per_page: 100, + }, + response => response.data.check_runs, + ); + const existingInvalidation = reviewerChecks.some( + check => check.app && check.app.id === 15368 && check.external_id === invalidationId, + ); + if (existingInvalidation) { + core.info(`Pull request #${pull.number} already has this exact invalidation; skipping rerun`); + return; + } + const supersedingContext = reviewerChecks.find( + check => check.app && check.app.id === 15368 && + supersedesInvalidation(check.external_id, baseAdvanceTime, context.runId), + ); + if (supersedingContext) { + core.info(`Pull request #${pull.number} has a newer invalidation context; skipping stale invalidation`); + return; + } + const newerSuccess = reviewerChecks.some( + check => check.app && check.app.id === 15368 && + check.conclusion === "success" && + check.external_id && check.external_id.startsWith( + `sfl-codex-review:pull:${pull.number}:base:${pull.base.sha.toLowerCase()}:`, + ) && + check.external_id.includes(`:context:${encodeURIComponent(invalidationId)}:`), + ); + if (newerSuccess) { + core.info(`Pull request #${pull.number} already has a successful Codex gate completed after this base advance`); + return; + } + const reason = renamedBase + ? `Repository default branch changed from deployed SFL review base ${configuredBaseBranch} to ${baseRef}; redeploy SFL before requesting a new Codex review.` + : `Pull request base ${baseRef} advanced to ${pull.base.sha}; request a new Codex review with gh sfl review --retry.`; + const finalChecks = await github.paginate( + github.rest.checks.listForRef, + { + owner, + repo, + ref: pull.head.sha, + check_name: "SFL Reviewer Gate Runner", + filter: "all", + per_page: 100, + }, + response => response.data.check_runs, + ); + const finalContextExists = finalChecks.some( + check => check.app && check.app.id === 15368 && + (check.external_id === invalidationId || + supersedesInvalidation(check.external_id, baseAdvanceTime, context.runId)), + ); + const finalSuccess = finalChecks.some( + check => check.app && check.app.id === 15368 && + check.conclusion === "success" && + check.external_id && check.external_id.startsWith( + `sfl-codex-review:pull:${pull.number}:base:${pull.base.sha.toLowerCase()}:`, + ) && + check.external_id.includes(`:context:${encodeURIComponent(invalidationId)}:`), + ); + if (finalContextExists || finalSuccess) { + core.info(`Pull request #${pull.number} received a newer context or successful Codex gate while invalidation was running`); + return; + } + await github.rest.checks.create({ + owner, + repo, + name: "SFL Reviewer Gate Runner", + head_sha: pull.head.sha, + status: "completed", + conclusion: "failure", + completed_at: new Date().toISOString(), + details_url: pull.html_url, + external_id: invalidationId, + output: {title: "SFL Codex review invalidated", summary: reason}, + }); + }; + const failures = []; + const batchSize = 10; + for (let offset = 0; offset < openPulls.length; offset += batchSize) { + const batch = openPulls.slice(offset, offset + batchSize); + const results = await Promise.allSettled(batch.map(invalidatePull)); + results.forEach((result, index) => { + if (result.status === "rejected") { + failures.push(`PR #${batch[index].number}: ${result.reason}`); + } + }); + } + if (failures.length > 0) { + throw new Error(`Base-advance invalidation failed after attempting every pull request:\n${failures.join("\n")}`); + } diff --git a/.github/workflows/sfl-pr-review-recovery.yml b/.github/workflows/sfl-pr-review-recovery.yml deleted file mode 100644 index f39ff86d..00000000 --- a/.github/workflows/sfl-pr-review-recovery.yml +++ /dev/null @@ -1,577 +0,0 @@ -# HemSoft SFL reviewer platform v2 -name: SFL PR Review Recovery - -on: - workflow_run: - workflows: ["SFL PR Review"] - types: [completed] - -permissions: - contents: read - -jobs: - recover: - if: >- - vars.SFL_ENABLED != 'false' && - (github.event.workflow_run.conclusion == 'failure' || - github.event.workflow_run.conclusion == 'timed_out' || - github.event.workflow_run.conclusion == 'cancelled') && - github.event.workflow_run.path == '.github/workflows/sfl-pr-review.lock.yml' && - github.event.workflow_run.event == 'workflow_dispatch' && - github.event.workflow_run.head_repository.full_name == github.repository && - github.event.workflow_run.head_branch == github.event.repository.default_branch - runs-on: ubuntu-slim - permissions: - checks: write - contents: read - steps: - - name: Configure GH_HOST for enterprise compatibility - shell: bash - run: | - GH_HOST="${GITHUB_SERVER_URL#https://}" - GH_HOST="${GH_HOST#http://}" - echo "GH_HOST=${GH_HOST}" >> "$GITHUB_ENV" - - - name: Finalize interrupted review evidence - if: always() - uses: actions/github-script@3a2844b7e9c422d3c10d287c895573f7108da1b3 # v9.0.0 - env: - FAILED_RUN_CONCLUSION: ${{ github.event.workflow_run.conclusion }} - FAILED_RUN_ID: ${{ github.event.workflow_run.id }} - FAILED_RUN_TITLE: ${{ github.event.workflow_run.display_title }} - with: - github-token: ${{ github.token }} - script: | - const runId = Number(process.env.FAILED_RUN_ID); - const conclusion = process.env.FAILED_RUN_CONCLUSION; - const title = process.env.FAILED_RUN_TITLE || ''; - const match = title.match( - /^SFL PR Review #([1-9][0-9]*) ([0-9a-f]{40}):([0-9a-f]{40}) retry=[01] dispatch=[A-Za-z0-9._-]{1,64}$/ - ); - if (!Number.isSafeInteger(runId) || runId <= 0 || !match) { - core.setFailed('Cannot resolve immutable review evidence from the interrupted run'); - return; - } - - const pullNumber = Number(match[1]); - const baseSha = match[2]; - const headSha = match[3]; - const externalId = - `sfl-review:${pullNumber}:${baseSha}:${headSha}:${runId}`; - const checkRuns = await github.paginate( - github.rest.checks.listForRef, - { - owner: context.repo.owner, - repo: context.repo.repo, - ref: headSha, - check_name: 'SFL Review Evidence', - filter: 'all', - per_page: 100, - } - ); - const matches = checkRuns.filter(check => - check.external_id === externalId && check.app?.id === 15368 - ); - if (matches.length === 0) { - core.info(`Run ${runId} did not initialize an SFL Review Evidence check`); - return; - } - if (matches.length !== 1) { - core.setFailed( - `Run ${runId} has ${matches.length} matching SFL Review Evidence checks` - ); - return; - } - - const evidence = matches[0]; - if (evidence.status === 'completed') { - core.info(`SFL Review Evidence check ${evidence.id} is already complete`); - return; - } - await github.rest.checks.update({ - owner: context.repo.owner, - repo: context.repo.repo, - check_run_id: evidence.id, - name: 'SFL Review Evidence', - external_id: externalId, - status: 'completed', - conclusion: 'failure', - completed_at: new Date().toISOString(), - output: { - title: 'SFL review did not complete', - summary: - `Reviewer run ${runId} concluded ${conclusion} before ` + - 'review evidence could be finalized.', - text: 'replacement_retry_eligible=false', - }, - }); - core.info(`Finalized interrupted SFL Review Evidence check ${evidence.id}`); - - - name: Mint SFL GitHub App token - if: github.event.workflow_run.conclusion != 'cancelled' - id: app-token - uses: actions/create-github-app-token@bcd2ba49218906704ab6c1aa796996da409d3eb1 # v3.2.0 - with: - client-id: ${{ vars.SFL_APP_CLIENT_ID }} - private-key: ${{ secrets.SFL_APP_PRIVATE_KEY }} - owner: ${{ github.repository_owner }} - repositories: ${{ github.event.repository.name }} - permission-actions: write - permission-contents: read - permission-pull-requests: read - - - name: Retry missing review output once - if: github.event.workflow_run.conclusion != 'cancelled' - env: - DEFAULT_BRANCH: ${{ github.event.repository.default_branch }} - FAILED_RUN_CREATED_AT: ${{ github.event.workflow_run.created_at }} - FAILED_RUN_ID: ${{ github.event.workflow_run.id }} - GH_TOKEN: ${{ steps.app-token.outputs.token }} - REPOSITORY: ${{ github.repository }} - SFL_APP_SLUG: ${{ steps.app-token.outputs.app-slug }} - run: | - set -euo pipefail - - # BEGIN TESTABLE REVIEW RECOVERY POLICY - reconcile_review_count() { - local manifest_review_count="$1" - local published_review_count="$2" - - if [ "$manifest_review_count" -gt "$published_review_count" ]; then - echo "$manifest_review_count" - else - echo "$published_review_count" - fi - } - - decide_review_recovery() { - local terminal_signal_count="$1" - local review_count="$2" - local retry_count="$3" - local expected_head="$4" - local live_head="$5" - local expected_base="$6" - local live_base="$7" - local eligible="$8" - - if [ "$review_count" -eq 1 ]; then - echo "complete" - return 0 - fi - if [ "$review_count" -ne 0 ]; then - echo "invalid" - return 1 - fi - if [ "$terminal_signal_count" -ne 0 ]; then - echo "reported" - return 0 - fi - if [ "$eligible" != "true" ] \ - || [ "$live_head" != "$expected_head" ] \ - || [ "$live_base" != "$expected_base" ]; then - echo "stale" - return 0 - fi - if [ "$retry_count" -eq 0 ]; then - echo "retry" - return 0 - fi - echo "exhausted" - return 1 - } - - newer_run_suppresses_retry() { - local run_status="$1" - local provenance_loaded="$2" - local provenance_match="$3" - local title_match="$4" - local run_conclusion="${5:-}" - - # A newer run whose provenance matches this exact PR/base/head - # owns the current-state review, regardless of requested effort. - if [ "$provenance_match" = "true" ]; then - echo "suppress" - return 0 - fi - # A maintainer-cancelled newer run is an intentional terminal - # signal for this sealed PR/base/head, even when cancellation - # happened before its provenance artifact was uploaded. - if [ "$run_conclusion" = "cancelled" ] \ - && [ "$title_match" = "true" ]; then - echo "suppress" - return 0 - fi - # An active newer run whose provenance has not materialized yet - # suppresses only when its sealed display title identifies this - # exact PR/base/head. A completed run needs matching provenance: - # otherwise it may have failed before uploading the artifact and - # still needs this older run's recovery attempt. - if [ "$run_status" != "completed" ] \ - && [ "$provenance_loaded" != "true" ] \ - && [ "$title_match" = "true" ]; then - echo "suppress" - return 0 - fi - echo "allow" - return 0 - } - # END TESTABLE REVIEW RECOVERY POLICY - - ARTIFACT_DIR=$(mktemp -d) - trap 'rm -rf "$ARTIFACT_DIR"' EXIT - mkdir -p \ - "$ARTIFACT_DIR/agent" \ - "$ARTIFACT_DIR/provenance" \ - "$ARTIFACT_DIR/safe-outputs" - - PROVENANCE_LOADED="false" - for PROVENANCE_ATTEMPT in 1 2 3; do - rm -rf "$ARTIFACT_DIR/provenance" - mkdir -p "$ARTIFACT_DIR/provenance" - if gh run download "$FAILED_RUN_ID" \ - --repo "$REPOSITORY" \ - --name sfl-review-run-provenance \ - --dir "$ARTIFACT_DIR/provenance"; then - PROVENANCE_LOADED="true" - break - fi - if [ "$PROVENANCE_ATTEMPT" -lt 3 ]; then - sleep $((PROVENANCE_ATTEMPT * 5)) - fi - done - if [ "$PROVENANCE_LOADED" != "true" ]; then - echo "::error::Could not load exact review provenance for run ${FAILED_RUN_ID}" - exit 1 - fi - - AGENT_OUTPUT="$ARTIFACT_DIR/agent/agent_output.json" - PROVENANCE="$ARTIFACT_DIR/provenance/sfl-review-run-provenance.json" - SAFE_OUTPUT_ITEMS="$ARTIFACT_DIR/safe-outputs/safe-output-items.jsonl" - for AGENT_ATTEMPT in 1 2 3; do - rm -rf "$ARTIFACT_DIR/agent" - mkdir -p "$ARTIFACT_DIR/agent" - if gh run download "$FAILED_RUN_ID" \ - --repo "$REPOSITORY" \ - --name agent \ - --dir "$ARTIFACT_DIR/agent"; then - break - fi - if [ "$AGENT_ATTEMPT" -lt 3 ]; then - sleep $((AGENT_ATTEMPT * 5)) - fi - done - if ! gh run download "$FAILED_RUN_ID" \ - --repo "$REPOSITORY" \ - --name safe-outputs-items \ - --dir "$ARTIFACT_DIR/safe-outputs"; then - echo "No durable safe-output artifact exists for run ${FAILED_RUN_ID}; reconciling published reviews" - : > "$SAFE_OUTPUT_ITEMS" - fi - if [ ! -f "$PROVENANCE" ]; then - echo "::error::Review recovery provenance is incomplete" - exit 1 - fi - if ! jq -e \ - --arg repository "$REPOSITORY" \ - --arg workflow_run_id "$FAILED_RUN_ID" \ - ' - .repository == $repository and - .workflow_run_id == $workflow_run_id and - (.pr_number | type) == "number" and - (.pr_number | floor) == .pr_number and - .pr_number > 0 and - (.base_sha | test("^[0-9a-f]{40}$")) and - (.head_sha | test("^[0-9a-f]{40}$")) and - (.review_effort | test("^(low|medium|high)$")) and - (.retry_count == 0 or .retry_count == 1) - ' "$PROVENANCE" >/dev/null; then - echo "::error::Review recovery provenance is invalid or mismatched" - exit 1 - fi - - PR_NUMBER=$(jq -r '.pr_number' "$PROVENANCE") - BASE_SHA=$(jq -r '.base_sha' "$PROVENANCE") - HEAD_SHA=$(jq -r '.head_sha' "$PROVENANCE") - REVIEW_EFFORT=$(jq -r '.review_effort' "$PROVENANCE") - RETRY_COUNT=$(jq -r '.retry_count' "$PROVENANCE") - if ! MANIFEST_REVIEW_COUNT=$(jq -s \ - '[ - .[] | - select(.type == "submit_pull_request_review") - ] | length' \ - "$SAFE_OUTPUT_ITEMS"); then - echo "::error::Review safe-output manifest is malformed" - exit 1 - fi - - if [ -z "$SFL_APP_SLUG" ]; then - echo "::error::Could not identify the trusted SFL GitHub App" - exit 1 - fi - APP_LOGIN="${SFL_APP_SLUG}[bot]" - PUBLISHED_REVIEWS=$(gh api \ - "repos/${REPOSITORY}/pulls/${PR_NUMBER}/reviews?per_page=100" \ - --paginate \ - --slurp) - if ! PUBLISHED_REVIEW_COUNT=$(jq -r \ - --arg app_login "$APP_LOGIN" \ - --arg head_sha "$HEAD_SHA" \ - --arg workflow_run_id "$FAILED_RUN_ID" \ - ' - [ - .[] | - .[] | - select( - ((.user.login // "") | ascii_downcase) == - ($app_login | ascii_downcase) and - .commit_id == $head_sha and - ( - (.body // "") | - contains("/actions/runs/" + $workflow_run_id) - ) - ) - ] | length - ' <<< "$PUBLISHED_REVIEWS"); then - echo "::error::Could not reconcile published reviews for run ${FAILED_RUN_ID}" - exit 1 - fi - REVIEW_COUNT=$(reconcile_review_count \ - "$MANIFEST_REVIEW_COUNT" \ - "$PUBLISHED_REVIEW_COUNT") - - if [ "$REVIEW_COUNT" -eq 1 ]; then - echo "Run ${FAILED_RUN_ID} produced its formal review; no missing-output retry is allowed" - exit 0 - fi - if [ "$REVIEW_COUNT" -gt 1 ]; then - echo "::error::Run ${FAILED_RUN_ID} produced multiple formal reviews" - exit 1 - fi - - # gh-aw omits metadata-only signals from the durable manifest. A - # retry is safe only after proving the agent emitted no terminal - # missing-data/tool/incomplete-report signal. - if [ ! -f "$AGENT_OUTPUT" ]; then - echo "::error::Review agent output is unavailable; refusing an unproven retry" - exit 1 - fi - if ! TERMINAL_SIGNAL_COUNT=$(jq -r \ - ' - ( - if type == "array" then - . - elif type == "object" and (.items | type) == "array" then - .items - else - error("agent output must be an array or contain an items array") - end - ) as $items | - [ - $items[] | - select( - .type == "missing_data" or - .type == "missing_tool" or - .type == "report_incomplete" - ) - ] | length - ' "$AGENT_OUTPUT"); then - echo "::error::Review agent output is malformed; refusing an unproven retry" - exit 1 - fi - - PULL_REQUEST=$(gh api "repos/${REPOSITORY}/pulls/${PR_NUMBER}") - LIVE_HEAD=$(jq -r '.head.sha // empty' <<< "$PULL_REQUEST") - LIVE_BASE=$(jq -r '.base.sha // empty' <<< "$PULL_REQUEST") - ELIGIBLE=$(jq -r \ - --arg default_branch "$DEFAULT_BRANCH" \ - --arg repository "$REPOSITORY" \ - ' - ( - .state == "open" and - .draft == false and - .base.ref == $default_branch and - .head.repo.full_name == $repository - ) | tostring - ' <<< "$PULL_REQUEST") - - DECISION_STATUS=0 - DECISION=$(decide_review_recovery \ - "$TERMINAL_SIGNAL_COUNT" \ - "$REVIEW_COUNT" \ - "$RETRY_COUNT" \ - "$HEAD_SHA" \ - "$LIVE_HEAD" \ - "$BASE_SHA" \ - "$LIVE_BASE" \ - "$ELIGIBLE") || DECISION_STATUS=$? - case "$DECISION" in - complete) - exit 0 - ;; - reported) - echo "Run ${FAILED_RUN_ID} produced an explicit non-review safe output; preserving its failed gate without retry" - exit 0 - ;; - stale) - echo "PR #${PR_NUMBER} is no longer eligible at ${HEAD_SHA}; suppressing stale retry" - exit 0 - ;; - exhausted) - echo "::error::PR #${PR_NUMBER} produced no formal review after its one trusted retry" - exit "$DECISION_STATUS" - ;; - invalid) - echo "::error::PR #${PR_NUMBER} produced an invalid formal-review count" - exit "$DECISION_STATUS" - ;; - retry) - ;; - *) - echo "::error::Unknown review recovery decision: ${DECISION}" - exit 1 - ;; - esac - - WORKFLOW_RUNS=$(gh api \ - "repos/${REPOSITORY}/actions/workflows/sfl-pr-review.lock.yml/runs?event=workflow_dispatch&per_page=100" \ - --paginate \ - --slurp) - EXPECTED_REVIEW_TITLE_PREFIX="SFL PR Review #${PR_NUMBER} ${BASE_SHA}:${HEAD_SHA} retry=" - NEWER_RUNS=$(jq -r \ - --arg created_at "$FAILED_RUN_CREATED_AT" \ - ' - .[] | - .workflow_runs[] | - select(.created_at > $created_at) | - [.id, .status, (.display_title // ""), (.conclusion // "")] | - @tsv - ' <<< "$WORKFLOW_RUNS") - while IFS=$'\t' read -r NEWER_RUN_ID NEWER_RUN_STATUS NEWER_RUN_TITLE NEWER_RUN_CONCLUSION; do - [ -n "$NEWER_RUN_ID" ] || continue - NEWER_PROVENANCE_DIR="$ARTIFACT_DIR/newer-${NEWER_RUN_ID}" - # The sealed title is computed first: an unrelated newer run - # provably targets a different PR/base/head, so it earns a - # single provenance attempt instead of the full in-flight - # polling window — 12 attempts (~330s) per unrelated run could - # outlast the recovery grace and hand the failed PR - # a premature sfl-human-required escalation. - NEWER_TITLE_MATCH="false" - case "$NEWER_RUN_TITLE" in - "$EXPECTED_REVIEW_TITLE_PREFIX"*) NEWER_TITLE_MATCH="true" ;; - esac - NEWER_PROVENANCE_ATTEMPTS=1 - if [ "$NEWER_RUN_STATUS" != "completed" ] \ - && [ "$NEWER_TITLE_MATCH" = "true" ]; then - NEWER_PROVENANCE_ATTEMPTS=12 - fi - NEWER_PROVENANCE_LOADED="false" - NEWER_PROVENANCE_MATCH="false" - for NEWER_PROVENANCE_ATTEMPT in $(seq 1 "$NEWER_PROVENANCE_ATTEMPTS"); do - rm -rf "$NEWER_PROVENANCE_DIR" - mkdir -p "$NEWER_PROVENANCE_DIR" - if gh run download "$NEWER_RUN_ID" \ - --repo "$REPOSITORY" \ - --name sfl-review-run-provenance \ - --dir "$NEWER_PROVENANCE_DIR" \ - >/dev/null 2>&1; then - NEWER_PROVENANCE_LOADED="true" - if jq -e \ - --argjson pr_number "$PR_NUMBER" \ - --arg base_sha "$BASE_SHA" \ - --arg head_sha "$HEAD_SHA" \ - ' - .pr_number == $pr_number and - .base_sha == $base_sha and - .head_sha == $head_sha - ' "$NEWER_PROVENANCE_DIR/sfl-review-run-provenance.json" \ - >/dev/null; then - NEWER_PROVENANCE_MATCH="true" - fi - break - fi - if [ "$NEWER_PROVENANCE_ATTEMPT" -lt "$NEWER_PROVENANCE_ATTEMPTS" ]; then - sleep $((NEWER_PROVENANCE_ATTEMPT * 5)) - fi - done - if [ "$(newer_run_suppresses_retry \ - "$NEWER_RUN_STATUS" \ - "$NEWER_PROVENANCE_LOADED" \ - "$NEWER_PROVENANCE_MATCH" \ - "$NEWER_TITLE_MATCH" \ - "$NEWER_RUN_CONCLUSION")" != "suppress" ]; then - continue - fi - if [ "$NEWER_PROVENANCE_MATCH" = "true" ]; then - echo "A newer review run already owns PR #${PR_NUMBER} at ${HEAD_SHA}; suppressing duplicate retry" - else - echo "A newer matching review for PR #${PR_NUMBER} at ${BASE_SHA}:${HEAD_SHA} suppresses duplicate retry" - fi - exit 0 - done <<< "$NEWER_RUNS" - - AW_CONTEXT=$(jq -cn \ - --arg base_sha "$BASE_SHA" \ - --arg head_sha "$HEAD_SHA" \ - --argjson item_number "$PR_NUMBER" \ - '{item_type: "pull_request", item_number: $item_number, base_sha: $base_sha, head_sha: $head_sha}') - dispatch_args=( - --method POST - "repos/${REPOSITORY}/actions/workflows/sfl-pr-review.lock.yml/dispatches" - -f "ref=${DEFAULT_BRANCH}" - -f "inputs[item_number]=${PR_NUMBER}" - -f "inputs[base_sha]=${BASE_SHA}" - -f "inputs[head_sha]=${HEAD_SHA}" - -f "inputs[retry_count]=1" - -f "inputs[aw_context]=${AW_CONTEXT}" - ) - base_workflow="$(gh api \ - --method GET \ - "repos/${REPOSITORY}/contents/.github/workflows/sfl-pr-review.lock.yml" \ - -f "ref=${DEFAULT_BRANCH}" \ - -H "Accept: application/vnd.github.raw+json")" - if grep -qE '^\s+review_effort:\s*$' <<< "${base_workflow}"; then - dispatch_args+=(-f "inputs[review_effort]=${REVIEW_EFFORT}") - fi - if grep -qE '^\s+dispatch_id:\s*$' <<< "${base_workflow}"; then - dispatch_args+=(-f "inputs[dispatch_id]=recovery-${GITHUB_RUN_ID}") - fi - - # Last-moment interlock: the artifact/provenance checks above can be - # slow. Revalidate both immutable PR state and newer-run ownership - # immediately before dispatch so a stale retry cannot cancel a newer - # current-state review through the shared reviewer concurrency group. - FINAL_PULL_REQUEST=$(gh api "repos/${REPOSITORY}/pulls/${PR_NUMBER}") - if ! jq -e \ - --arg base_sha "$BASE_SHA" \ - --arg default_branch "$DEFAULT_BRANCH" \ - --arg head_sha "$HEAD_SHA" \ - --arg repository "$REPOSITORY" \ - ' - .state == "open" and - .draft == false and - .base.ref == $default_branch and - .base.sha == $base_sha and - .head.repo.full_name == $repository and - .head.sha == $head_sha - ' <<< "$FINAL_PULL_REQUEST" >/dev/null; then - echo "PR #${PR_NUMBER} changed before recovery dispatch; suppressing stale retry" - exit 0 - fi - FINAL_NEWER_RUN_ID=$(gh api \ - "repos/${REPOSITORY}/actions/workflows/sfl-pr-review.lock.yml/runs?event=workflow_dispatch&per_page=100" \ - | jq -r \ - --arg expected_title "$EXPECTED_REVIEW_TITLE_PREFIX" \ - --argjson failed_run_id "$FAILED_RUN_ID" ' - [.workflow_runs[] - | select(.id > $failed_run_id) - | select((.display_title // "") | startswith($expected_title))] - | sort_by(.id) - | last - | .id // empty - ') - if [[ "$FINAL_NEWER_RUN_ID" =~ ^[1-9][0-9]*$ ]]; then - echo "Newer review run ${FINAL_NEWER_RUN_ID} acquired PR #${PR_NUMBER} before recovery dispatch; suppressing duplicate retry" - exit 0 - fi - gh api "${dispatch_args[@]}" - echo "Dispatched one trusted missing-output retry for PR #${PR_NUMBER} at ${HEAD_SHA}" diff --git a/.github/workflows/sfl-pr-review.lock.yml b/.github/workflows/sfl-pr-review.lock.yml deleted file mode 100644 index fb194590..00000000 --- a/.github/workflows/sfl-pr-review.lock.yml +++ /dev/null @@ -1,2887 +0,0 @@ -# gh-aw-metadata: {"schema_version":"v4","frontmatter_hash":"44a8354ce686870863972a34fbacac2e66b996539d28d7595072f3b6d9b15962","body_hash":"a4cc487dbbb169790a702891462cd2612eb23f7231d3bed018e4a81bb5e8854b","compiler_version":"v0.86.2","strict":true,"engine_base_url_customized":true,"agent_id":"copilot","agent_model":"moonshotai/kimi-k3","engine_versions":{"copilot":"1.0.79"}} -# gh-aw-manifest: {"version":1,"secrets":["COPILOT_GITHUB_TOKEN","GH_AW_GITHUB_MCP_SERVER_TOKEN","GH_AW_GITHUB_TOKEN","GITHUB_TOKEN","OPENROUTER_API_KEY","SFL_APP_PRIVATE_KEY"],"actions":[{"repo":"actions/cache/restore","sha":"55cc8345863c7cc4c66a329aec7e433d2d1c52a9","version":"v6.1.0"},{"repo":"actions/cache/save","sha":"55cc8345863c7cc4c66a329aec7e433d2d1c52a9","version":"v6.1.0"},{"repo":"actions/checkout","sha":"3d3c42e5aac5ba805825da76410c181273ba90b1","version":"v7.0.1"},{"repo":"actions/create-github-app-token","sha":"bcd2ba49218906704ab6c1aa796996da409d3eb1","version":"bcd2ba49218906704ab6c1aa796996da409d3eb1"},{"repo":"actions/download-artifact","sha":"3e5f45b2cfb9172054b4087a40e8e0b5a5461e7c","version":"v8.0.1"},{"repo":"actions/github-script","sha":"3a2844b7e9c422d3c10d287c895573f7108da1b3","version":"v9.0.0"},{"repo":"actions/setup-node","sha":"820762786026740c76f36085b0efc47a31fe5020","version":"v7.0.0"},{"repo":"actions/upload-artifact","sha":"043fb46d1a93c77aae656e7c1c64a875d1fc6a0a","version":"043fb46d1a93c77aae656e7c1c64a875d1fc6a0a"},{"repo":"github/gh-aw-actions/setup","sha":"6aab9e5b5c91c615506061f09bedd81a23babe3c","version":"v0.86.2"}],"containers":[{"image":"ghcr.io/github/gh-aw-firewall/agent:0.27.44","digest":"sha256:0d727725c737b58c7bdf51f640cffb928385ec46517e0917c7f1a02f1bada8b4","pinned_image":"ghcr.io/github/gh-aw-firewall/agent:0.27.44@sha256:0d727725c737b58c7bdf51f640cffb928385ec46517e0917c7f1a02f1bada8b4"},{"image":"ghcr.io/github/gh-aw-firewall/api-proxy:0.27.44","digest":"sha256:b50fbadba138f6e9aba94aca09711335c489bb3b15861220cb66f6092e042dc7","pinned_image":"ghcr.io/github/gh-aw-firewall/api-proxy:0.27.44@sha256:b50fbadba138f6e9aba94aca09711335c489bb3b15861220cb66f6092e042dc7"},{"image":"ghcr.io/github/gh-aw-firewall/squid:0.27.44","digest":"sha256:83e48bbe12c634be8c228a576832fe45f66c529ac3659db92bddbcf2eeb6d627","pinned_image":"ghcr.io/github/gh-aw-firewall/squid:0.27.44@sha256:83e48bbe12c634be8c228a576832fe45f66c529ac3659db92bddbcf2eeb6d627"},{"image":"ghcr.io/github/gh-aw-mcpg:v0.4.9","digest":"sha256:e5a1569aeaf41820fa7bdee3e94468cae448133cdbf00119ad24f5b74db1ab9f","pinned_image":"ghcr.io/github/gh-aw-mcpg:v0.4.9@sha256:e5a1569aeaf41820fa7bdee3e94468cae448133cdbf00119ad24f5b74db1ab9f"},{"image":"ghcr.io/github/gh-aw-node","digest":"sha256:0d9f1fb5fd6610c0ac1f5194a38e45a8a1e81f8a390d5142d8e4e6f26a4b3196","pinned_image":"ghcr.io/github/gh-aw-node@sha256:0d9f1fb5fd6610c0ac1f5194a38e45a8a1e81f8a390d5142d8e4e6f26a4b3196"},{"image":"ghcr.io/github/github-mcp-server:v1.9.0","digest":"sha256:881b53d6f75f69bdbc1b5b10fc2f1361717c19054143b3a8529fb5c32061a50e","pinned_image":"ghcr.io/github/github-mcp-server:v1.9.0@sha256:881b53d6f75f69bdbc1b5b10fc2f1361717c19054143b3a8529fb5c32061a50e"}]} -# This file was automatically generated by gh-aw (v0.86.2). DO NOT EDIT. To debug this workflow, load the skill at https://github.com/github/gh-aw/blob/main/debug.md -# -# ___ _ _ -# / _ \ | | (_) -# | |_| | __ _ ___ _ __ | |_ _ ___ -# | _ |/ _` |/ _ \ '_ \| __| |/ __| -# | | | | (_| | __/ | | | |_| | (__ -# \_| |_/\__, |\___|_| |_|\__|_|\___| -# __/ | -# _ _ |___/ -# | | | | / _| | -# | | | | ___ _ __ _ __| |_| | _____ ____ -# | |/\| |/ _ \ '__| |/ /| _| |/ _ \ \ /\ / / ___| -# \ /\ / (_) | | | | ( | | | | (_) \ V V /\__ \ -# \/ \/ \___/|_| |_|\_\|_| |_|\___/ \_/\_/ |___/ -# -# -# To update this file, edit HemSoft/set-it-free-loop/deployment/workflows/sfl-pr-review.md@main and run: -# gh aw compile -# Not all edits will cause changes to this file. -# -# For more information: https://github.github.com/gh-aw/introduction/overview/ -# -# Full-spectrum PR review dispatched automatically by the SFL PR review wrapper -# or manually through workflow_dispatch. Performs 3 evidence-based review -# passes (Security, Accuracy & Reliability, Quality & Maintainability) and -# posts inline review comments on specific lines with an overall summary. -# -# Source: HemSoft/set-it-free-loop/deployment/workflows/sfl-pr-review.md@main -# -# Frontmatter env variables: -# - SFL_REVIEW_EFFORT: (main workflow) -# -# Secrets used: -# - COPILOT_GITHUB_TOKEN -# - GH_AW_GITHUB_MCP_SERVER_TOKEN -# - GH_AW_GITHUB_TOKEN -# - GITHUB_TOKEN -# - OPENROUTER_API_KEY -# - SFL_APP_PRIVATE_KEY -# -# Custom actions used: -# - actions/cache/restore@55cc8345863c7cc4c66a329aec7e433d2d1c52a9 # v6.1.0 -# - actions/cache/save@55cc8345863c7cc4c66a329aec7e433d2d1c52a9 # v6.1.0 -# - actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 -# - actions/create-github-app-token@bcd2ba49218906704ab6c1aa796996da409d3eb1 -# - actions/create-github-app-token@bcd2ba49218906704ab6c1aa796996da409d3eb1 # v3.2.0 -# - actions/download-artifact@3e5f45b2cfb9172054b4087a40e8e0b5a5461e7c # v8.0.1 -# - actions/github-script@3a2844b7e9c422d3c10d287c895573f7108da1b3 # v9.0.0 -# - actions/github-script@3a2844b7e9c422d3c10d287c895573f7108da1b3 # v9.0.0 (source v9) -# - actions/setup-node@820762786026740c76f36085b0efc47a31fe5020 # v7.0.0 -# - actions/upload-artifact@043fb46d1a93c77aae656e7c1c64a875d1fc6a0a -# - actions/upload-artifact@043fb46d1a93c77aae656e7c1c64a875d1fc6a0a # v7.0.1 -# - github/gh-aw-actions/setup@6aab9e5b5c91c615506061f09bedd81a23babe3c # v0.86.2 -# -# Container images used: -# - ghcr.io/github/gh-aw-firewall/agent:0.27.44@sha256:0d727725c737b58c7bdf51f640cffb928385ec46517e0917c7f1a02f1bada8b4 -# - ghcr.io/github/gh-aw-firewall/api-proxy:0.27.44@sha256:b50fbadba138f6e9aba94aca09711335c489bb3b15861220cb66f6092e042dc7 -# - ghcr.io/github/gh-aw-firewall/squid:0.27.44@sha256:83e48bbe12c634be8c228a576832fe45f66c529ac3659db92bddbcf2eeb6d627 -# - ghcr.io/github/gh-aw-mcpg:v0.4.9@sha256:e5a1569aeaf41820fa7bdee3e94468cae448133cdbf00119ad24f5b74db1ab9f -# - ghcr.io/github/gh-aw-node@sha256:0d9f1fb5fd6610c0ac1f5194a38e45a8a1e81f8a390d5142d8e4e6f26a4b3196 -# - ghcr.io/github/github-mcp-server:v1.9.0@sha256:881b53d6f75f69bdbc1b5b10fc2f1361717c19054143b3a8529fb5c32061a50e - -name: "SFL PR Review" -on: - workflow_dispatch: - inputs: - aw_context: - description: gh-aw pull request context JSON - required: true - type: string - base_sha: - description: Expected pull request base commit - required: true - type: string - dispatch_id: - default: manual - description: Unique wrapper or recovery dispatch identifier - required: false - type: string - head_sha: - description: Expected pull request head commit - required: true - type: string - item_number: - description: Pull request number to review - required: true - type: string - retry_count: - default: "0" - description: Missing-review-output retry counter - required: false - type: string - review_effort: - default: low - description: Audit effort marker retained in provenance and recovery - options: - - low - - medium - - high - required: false - type: choice - -permissions: {} - -concurrency: - cancel-in-progress: true - group: sfl-pr-review-${{ inputs.item_number }} - -run-name: "SFL PR Review #${{ inputs.item_number }} ${{ inputs.base_sha }}:${{ inputs.head_sha }} retry=${{ inputs.retry_count }} dispatch=${{ inputs.dispatch_id }}" - -env: - SFL_REVIEW_EFFORT: ${{ inputs.review_effort || 'low' }} - -jobs: - activation: - runs-on: ubuntu-slim - permissions: - actions: read - contents: read - env: - GH_AW_MAX_DAILY_AI_CREDITS: ${{ vars.GH_AW_DEFAULT_MAX_DAILY_AI_CREDITS || '5000' }} - GH_AW_RUNTIME_FEATURES: ${{ vars.GH_AW_RUNTIME_FEATURES }} - outputs: - comment_id: "" - comment_repo: "" - daily_ai_credits_exceeded: ${{ steps.daily-effective-workflow-guardrail.outputs.daily_ai_credits_exceeded == 'true' }} - daily_ai_credits_guardrail_status: ${{ steps.daily-effective-workflow-guardrail.outputs.daily_ai_credits_guardrail_status || '' }} - daily_ai_credits_threshold: ${{ steps.daily-effective-workflow-guardrail.outputs.daily_ai_credits_threshold || '' }} - daily_ai_credits_total_effective_tokens: ${{ steps.daily-effective-workflow-guardrail.outputs.daily_ai_credits_total_effective_tokens || '' }} - engine_id: ${{ steps.generate_aw_info.outputs.engine_id }} - lockdown_check_failed: ${{ steps.generate_aw_info.outputs.lockdown_check_failed == 'true' }} - model: ${{ steps.generate_aw_info.outputs.model }} - oauth_token_check_failed: ${{ steps.check-oauth-tokens.outputs.oauth_token_check_failed == 'true' }} - setup-parent-span-id: ${{ steps.setup.outputs.parent-span-id || steps.setup.outputs.span-id }} - setup-span-id: ${{ steps.setup.outputs.span-id }} - setup-trace-id: ${{ steps.setup.outputs.trace-id }} - stale_lock_file_failed: ${{ steps.check-lock-file.outputs.stale_lock_file_failed == 'true' }} - steps: - - name: Setup Scripts - id: setup - uses: github/gh-aw-actions/setup@6aab9e5b5c91c615506061f09bedd81a23babe3c # v0.86.2 - with: - destination: ${{ runner.temp }}/gh-aw/actions - job-name: ${{ github.job }} - safe-output-artifact-client: ${{ env.GH_AW_MAX_DAILY_AI_CREDITS != '' }} - env: - GH_AW_SETUP_WORKFLOW_NAME: "SFL PR Review" - GH_AW_CURRENT_WORKFLOW_REF: ${{ github.repository }}/.github/workflows/sfl-pr-review.lock.yml@${{ github.ref }} - GH_AW_INFO_VERSION: "1.0.79" - GH_AW_INFO_AWF_VERSION: "v0.27.44" - GH_AW_INFO_BODY_MODIFIED: "false" - GH_AW_INFO_ENGINE_ID: "copilot" - - name: Generate agentic run info - id: generate_aw_info - env: - GH_AW_INFO_ENGINE_ID: "copilot" - GH_AW_INFO_ENGINE_NAME: "GitHub Copilot CLI" - GH_AW_INFO_MODEL: "moonshotai/kimi-k3" - GH_AW_INFO_VERSION: "1.0.79" - GH_AW_INFO_AGENT_VERSION: "1.0.79" - GH_AW_INFO_CLI_VERSION: "v0.86.2" - GH_AW_INFO_WORKFLOW_NAME: "SFL PR Review" - GH_AW_INFO_EXPERIMENTAL: "false" - GH_AW_INFO_SUPPORTS_TOOLS_ALLOWLIST: "true" - GH_AW_INFO_STAGED: "false" - GH_AW_INFO_ALLOWED_DOMAINS: '["openrouter.ai"]' - GH_AW_INFO_FIREWALL_ENABLED: "true" - GH_AW_INFO_AWF_VERSION: "v0.27.44" - GH_AW_INFO_AWMG_VERSION: "" - GH_AW_INFO_FIREWALL_TYPE: "squid" - GH_AW_INFO_AGENT_RUNTIME: "" - GH_AW_INFO_FRONTMATTER_SOURCE: "HemSoft/set-it-free-loop/deployment/workflows/sfl-pr-review.md@main" - GH_AW_INFO_BODY_MODIFIED: "false" - GH_AW_COMPILED_STRICT: "true" - GH_AW_INFO_MODEL_COSTS: '{"providers":{"github-copilot":{"models":{"moonshotai/kimi-k3":{"cost":{"input":"3e-06","output":"1.5e-05"}}}}}}' - uses: actions/github-script@3a2844b7e9c422d3c10d287c895573f7108da1b3 # v9.0.0 - with: - script: | - const { setupGlobals } = require('${{ runner.temp }}/gh-aw/actions/setup_globals.cjs'); - setupGlobals(core, github, context, exec, io, getOctokit); - const { main } = require('${{ runner.temp }}/gh-aw/actions/generate_aw_info.cjs'); - await main(core, context); - - name: Restore daily AIC usage cache - id: restore-daily-aic-cache - if: ${{ env.GH_AW_MAX_DAILY_AI_CREDITS != '' }} - continue-on-error: true - uses: actions/cache/restore@55cc8345863c7cc4c66a329aec7e433d2d1c52a9 # v6.1.0 - with: - key: agentic-workflow-usage-sflprreview-${{ github.run_id }} - restore-keys: agentic-workflow-usage-sflprreview- - path: /tmp/gh-aw/agentic-workflow-usage-cache.jsonl - - name: Restore daily AIC usage cache (artifact fallback) - id: restore-daily-aic-cache-fallback - if: ${{ env.GH_AW_MAX_DAILY_AI_CREDITS != '' }} - continue-on-error: true - uses: actions/github-script@3a2844b7e9c422d3c10d287c895573f7108da1b3 # v9.0.0 - env: - GH_AW_RESTORE_DAILY_AIC_CACHE_HIT: ${{ steps.restore-daily-aic-cache.outputs.cache-hit }} - GH_AW_RESTORE_DAILY_AIC_CACHE_MATCHED_KEY: ${{ steps.restore-daily-aic-cache.outputs.cache-matched-key }} - with: - github-token: ${{ secrets.GITHUB_TOKEN }} - script: | - const { setupGlobals } = require('${{ runner.temp }}/gh-aw/actions/setup_globals.cjs'); - setupGlobals(core, github, context, exec, io, getOctokit); - const { main } = require('${{ runner.temp }}/gh-aw/actions/restore_aic_usage_cache_fallback.cjs'); - await main(); - - name: Check daily workflow token guardrail - id: daily-effective-workflow-guardrail - if: ${{ env.GH_AW_MAX_DAILY_AI_CREDITS != '' }} - uses: actions/github-script@3a2844b7e9c422d3c10d287c895573f7108da1b3 # v9.0.0 - env: - GH_AW_WORKFLOW_NAME: "SFL PR Review" - GH_AW_WORKFLOW_ID: "sfl-pr-review" - GH_AW_RUN_URL: ${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }} - GH_AW_WORKFLOW_DISPATCH_AW_CONTEXT: ${{ github.event.inputs.aw_context || '' }} - GH_AW_HAS_SLASH_COMMAND: "false" - GH_AW_HAS_LABEL_COMMAND: "false" - GH_AW_GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} - GH_AW_MAX_DAILY_AI_CREDITS: ${{ vars.GH_AW_DEFAULT_MAX_DAILY_AI_CREDITS || '5000' }} - with: - github-token: ${{ secrets.GITHUB_TOKEN }} - script: | - const { setupGlobals } = require('${{ runner.temp }}/gh-aw/actions/setup_globals.cjs'); - setupGlobals(core, github, context, exec, io, getOctokit); - const { main } = require('${{ runner.temp }}/gh-aw/actions/check_daily_aic_workflow_guardrail.cjs'); - await main(); - - name: Check for OAuth tokens - id: check-oauth-tokens - run: bash "${RUNNER_TEMP}/gh-aw/actions/check_oauth_tokens.sh" - env: - COPILOT_GITHUB_TOKEN: ${{ secrets.COPILOT_GITHUB_TOKEN }} - GH_AW_GITHUB_TOKEN: ${{ secrets.GH_AW_GITHUB_TOKEN }} - GH_AW_GITHUB_MCP_SERVER_TOKEN: ${{ secrets.GH_AW_GITHUB_MCP_SERVER_TOKEN }} - - name: Checkout .github and .agents folders - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 - with: - persist-credentials: false - sparse-checkout: | - .github - .agents - .claude - .codex - .gemini - .pi - sparse-checkout-cone-mode: true - fetch-depth: 1 - - name: Save agent config folders for base branch restoration - env: - GH_AW_AGENT_FOLDERS: ".agents .github" - GH_AW_AGENT_FILES: "AGENTS.md" - run: | - bash "${RUNNER_TEMP}/gh-aw/actions/save_base_github_folders.sh" - - name: Check workflow lock file - id: check-lock-file - uses: actions/github-script@3a2844b7e9c422d3c10d287c895573f7108da1b3 # v9.0.0 - env: - GH_AW_WORKFLOW_FILE: "sfl-pr-review.lock.yml" - GH_AW_CONTEXT_WORKFLOW_REF: "${{ github.workflow_ref }}" - with: - script: | - const { setupGlobals } = require('${{ runner.temp }}/gh-aw/actions/setup_globals.cjs'); - setupGlobals(core, github, context, exec, io, getOctokit); - const { main } = require('${{ runner.temp }}/gh-aw/actions/check_workflow_timestamp_api.cjs'); - await main(); - - name: Check compile-agentic version - uses: actions/github-script@3a2844b7e9c422d3c10d287c895573f7108da1b3 # v9.0.0 - env: - GH_AW_COMPILED_VERSION: "v0.86.2" - with: - script: | - const { setupGlobals } = require('${{ runner.temp }}/gh-aw/actions/setup_globals.cjs'); - setupGlobals(core, github, context, exec, io, getOctokit); - const { main } = require('${{ runner.temp }}/gh-aw/actions/check_version_updates.cjs'); - await main(); - - name: Log runtime features - if: ${{ contains(toJSON(vars), '"GH_AW_RUNTIME_FEATURES":') }} - run: bash "${RUNNER_TEMP}/gh-aw/actions/log_runtime_features_summary.sh" - - name: Create prompt with built-in context - uses: actions/github-script@3a2844b7e9c422d3c10d287c895573f7108da1b3 # v9.0.0 - env: - GH_AW_ACTIONS_DIR: ${{ runner.temp }}/gh-aw/actions - GH_AW_PROMPT: ${{ runner.temp }}/gh-aw/aw-prompts/prompt.txt - GH_AW_SAFE_OUTPUTS: ${{ runner.temp }}/gh-aw/safeoutputs/outputs.jsonl - GH_AW_PROMPT_CONFIG: "{\"items\":[{\"content_env\":\"GH_AW_PROMPT_CONTENT_0000\"},{\"file\":\"xpia.md\"},{\"file\":\"temp_folder_prompt.md\"},{\"file\":\"markdown.md\"},{\"file\":\"safe_outputs_prompt.md\"},{\"content_env\":\"GH_AW_PROMPT_CONTENT_0001\"},{\"content_env\":\"GH_AW_PROMPT_CONTENT_0002\"},{\"file\":\"mcp_cli_tools_prompt.md\"},{\"content_env\":\"GH_AW_PROMPT_CONTENT_0003\"},{\"file\":\"github_mcp_tools_with_safeoutputs_prompt.md\"},{\"content_env\":\"GH_AW_PROMPT_CONTENT_0004\"},{\"content_env\":\"GH_AW_PROMPT_CONTENT_0005\"}]}" - GH_AW_EXPR_1A3A194A: ${{ github.event.discussion.number || (fromJSON(github.event.inputs.aw_context || github.event.client_payload.aw_context || '{}').item_type == 'discussion' && fromJSON(github.event.inputs.aw_context || github.event.client_payload.aw_context || '{}').item_number) }} - GH_AW_EXPR_463A214A: ${{ github.event.pull_request.number || (fromJSON(github.event.inputs.aw_context || github.event.client_payload.aw_context || '{}').item_type == 'pull_request' && fromJSON(github.event.inputs.aw_context || github.event.client_payload.aw_context || '{}').item_number) }} - GH_AW_EXPR_802A9F6A: ${{ github.event.issue.number || (fromJSON(github.event.inputs.aw_context || github.event.client_payload.aw_context || '{}').item_type == 'issue' && fromJSON(github.event.inputs.aw_context || github.event.client_payload.aw_context || '{}').item_number) }} - GH_AW_EXPR_FF1D34CE: ${{ github.event.comment.id || fromJSON(github.event.inputs.aw_context || github.event.client_payload.aw_context || '{}').comment_id }} - GH_AW_GITHUB_ACTOR: ${{ github.actor }} - GH_AW_GITHUB_REPOSITORY: ${{ github.repository }} - GH_AW_GITHUB_RUN_ID: ${{ github.run_id }} - GH_AW_GITHUB_SERVER_URL: ${{ github.server_url }} - GH_AW_GITHUB_WORKSPACE: ${{ github.workspace }} - GH_AW_INPUTS_BASE_SHA: ${{ inputs.base_sha }} - GH_AW_INPUTS_HEAD_SHA: ${{ inputs.head_sha }} - GH_AW_INPUTS_ITEM_NUMBER: ${{ inputs.item_number }} - GH_AW_PROMPT_CONTENT_0000: "\n" - GH_AW_PROMPT_CONTENT_0001: "\nTools: create_pull_request_review_comment(max:30), submit_pull_request_review, missing_tool, missing_data, noop, resolve_sfl_review_thread\n" - GH_AW_PROMPT_CONTENT_0002: "\n" - GH_AW_PROMPT_CONTENT_0003: "\nThe following GitHub context information is available for this workflow:\n{{#if github.actor}}\n- **actor**: __GH_AW_GITHUB_ACTOR__\n{{/if}}\n{{#if github.repository}}\n- **repository**: __GH_AW_GITHUB_REPOSITORY__\n{{/if}}\n{{#if github.workspace}}\n- **workspace**: __GH_AW_GITHUB_WORKSPACE__\n{{/if}}\n{{#if github.event.issue.number || (github.aw.context.item_type == 'issue' && github.aw.context.item_number)}}\n- **issue-number**: #__GH_AW_EXPR_802A9F6A__\n{{/if}}\n{{#if github.event.discussion.number || (github.aw.context.item_type == 'discussion' && github.aw.context.item_number)}}\n- **discussion-number**: #__GH_AW_EXPR_1A3A194A__\n{{/if}}\n{{#if github.event.pull_request.number || (github.aw.context.item_type == 'pull_request' && github.aw.context.item_number)}}\n- **pull-request-number**: #__GH_AW_EXPR_463A214A__\n{{/if}}\n{{#if github.event.comment.id || github.aw.context.comment_id}}\n- **comment-id**: __GH_AW_EXPR_FF1D34CE__\n{{/if}}\n{{#if github.run_id}}\n- **workflow-run-id**: __GH_AW_GITHUB_RUN_ID__\n{{/if}}\n\n\n" - GH_AW_PROMPT_CONTENT_0004: "\n" - GH_AW_PROMPT_CONTENT_0005: "{{#runtime-import .github/workflows/sfl-pr-review.md}}\n" - with: - script: | - const { setupGlobals } = require(process.env.GH_AW_ACTIONS_DIR + '/setup_globals.cjs'); - setupGlobals(core, github, context, exec, io, getOctokit); - const { main } = require(process.env.GH_AW_ACTIONS_DIR + '/create_prompt.cjs'); - await main(core); - - name: Interpolate variables and render templates - uses: actions/github-script@3a2844b7e9c422d3c10d287c895573f7108da1b3 # v9.0.0 - env: - GH_AW_PROMPT: ${{ runner.temp }}/gh-aw/aw-prompts/prompt.txt - GH_AW_ENGINE_ID: "copilot" - GH_AW_GITHUB_REPOSITORY: ${{ github.repository }} - GH_AW_GITHUB_RUN_ID: ${{ github.run_id }} - GH_AW_GITHUB_SERVER_URL: ${{ github.server_url }} - GH_AW_INPUTS_BASE_SHA: ${{ inputs.base_sha }} - GH_AW_INPUTS_HEAD_SHA: ${{ inputs.head_sha }} - GH_AW_INPUTS_ITEM_NUMBER: ${{ inputs.item_number }} - with: - script: | - const { setupGlobals } = require('${{ runner.temp }}/gh-aw/actions/setup_globals.cjs'); - setupGlobals(core, github, context, exec, io, getOctokit); - const { main } = require('${{ runner.temp }}/gh-aw/actions/interpolate_prompt.cjs'); - await main(); - - name: Substitute placeholders - uses: actions/github-script@3a2844b7e9c422d3c10d287c895573f7108da1b3 # v9.0.0 - env: - GH_AW_PROMPT: ${{ runner.temp }}/gh-aw/aw-prompts/prompt.txt - GH_AW_EXPR_1A3A194A: ${{ github.event.discussion.number || (fromJSON(github.event.inputs.aw_context || github.event.client_payload.aw_context || '{}').item_type == 'discussion' && fromJSON(github.event.inputs.aw_context || github.event.client_payload.aw_context || '{}').item_number) }} - GH_AW_EXPR_463A214A: ${{ github.event.pull_request.number || (fromJSON(github.event.inputs.aw_context || github.event.client_payload.aw_context || '{}').item_type == 'pull_request' && fromJSON(github.event.inputs.aw_context || github.event.client_payload.aw_context || '{}').item_number) }} - GH_AW_EXPR_802A9F6A: ${{ github.event.issue.number || (fromJSON(github.event.inputs.aw_context || github.event.client_payload.aw_context || '{}').item_type == 'issue' && fromJSON(github.event.inputs.aw_context || github.event.client_payload.aw_context || '{}').item_number) }} - GH_AW_EXPR_FF1D34CE: ${{ github.event.comment.id || fromJSON(github.event.inputs.aw_context || github.event.client_payload.aw_context || '{}').comment_id }} - GH_AW_GITHUB_ACTOR: ${{ github.actor }} - GH_AW_GITHUB_REPOSITORY: ${{ github.repository }} - GH_AW_GITHUB_RUN_ID: ${{ github.run_id }} - GH_AW_GITHUB_SERVER_URL: ${{ github.server_url }} - GH_AW_GITHUB_WORKSPACE: ${{ github.workspace }} - GH_AW_INPUTS_BASE_SHA: ${{ inputs.base_sha }} - GH_AW_INPUTS_HEAD_SHA: ${{ inputs.head_sha }} - GH_AW_INPUTS_ITEM_NUMBER: ${{ inputs.item_number }} - GH_AW_MCP_CLI_SERVERS_LIST: "- `github` — run `github --help` to see available tools\n- `safeoutputs` — run `safeoutputs --help` to see available tools" - with: - script: | - const { setupGlobals } = require('${{ runner.temp }}/gh-aw/actions/setup_globals.cjs'); - setupGlobals(core, github, context, exec, io, getOctokit); - - const substitutePlaceholders = require('${{ runner.temp }}/gh-aw/actions/substitute_placeholders.cjs'); - - // Call the substitution function - return await substitutePlaceholders({ - file: process.env.GH_AW_PROMPT, - substitutions: { - GH_AW_EXPR_1A3A194A: process.env.GH_AW_EXPR_1A3A194A, - GH_AW_EXPR_463A214A: process.env.GH_AW_EXPR_463A214A, - GH_AW_EXPR_802A9F6A: process.env.GH_AW_EXPR_802A9F6A, - GH_AW_EXPR_FF1D34CE: process.env.GH_AW_EXPR_FF1D34CE, - GH_AW_GITHUB_ACTOR: process.env.GH_AW_GITHUB_ACTOR, - GH_AW_GITHUB_REPOSITORY: process.env.GH_AW_GITHUB_REPOSITORY, - GH_AW_GITHUB_RUN_ID: process.env.GH_AW_GITHUB_RUN_ID, - GH_AW_GITHUB_SERVER_URL: process.env.GH_AW_GITHUB_SERVER_URL, - GH_AW_GITHUB_WORKSPACE: process.env.GH_AW_GITHUB_WORKSPACE, - GH_AW_INPUTS_BASE_SHA: process.env.GH_AW_INPUTS_BASE_SHA, - GH_AW_INPUTS_HEAD_SHA: process.env.GH_AW_INPUTS_HEAD_SHA, - GH_AW_INPUTS_ITEM_NUMBER: process.env.GH_AW_INPUTS_ITEM_NUMBER, - GH_AW_MCP_CLI_SERVERS_LIST: process.env.GH_AW_MCP_CLI_SERVERS_LIST - } - }); - - name: Validate prompt placeholders - env: - GH_AW_PROMPT: ${{ runner.temp }}/gh-aw/aw-prompts/prompt.txt - run: | - bash "${RUNNER_TEMP}/gh-aw/actions/validate_prompt_placeholders.sh" - - name: Print prompt - env: - GH_AW_PROMPT: ${{ runner.temp }}/gh-aw/aw-prompts/prompt.txt - run: | - bash "${RUNNER_TEMP}/gh-aw/actions/print_prompt_summary.sh" - - name: Stage prompt files for artifact upload - run: | - mkdir -p /tmp/gh-aw/aw-prompts - cp -a "${RUNNER_TEMP}/gh-aw/aw-prompts/." /tmp/gh-aw/aw-prompts/ - - name: Upload activation artifact - if: success() - uses: actions/upload-artifact@043fb46d1a93c77aae656e7c1c64a875d1fc6a0a # v7.0.1 - with: - name: activation - include-hidden-files: true - path: | - /tmp/gh-aw/aw_info.json - /tmp/gh-aw/models.json - /tmp/gh-aw/aw-prompts/prompt.txt - /tmp/gh-aw/aw-prompts/prompt-template.txt - /tmp/gh-aw/aw-prompts/prompt-import-tree.json - /tmp/gh-aw/github_rate_limits.jsonl - /tmp/gh-aw/base - /tmp/gh-aw/.github/agents - /tmp/gh-aw/.github/skills - if-no-files-found: ignore - retention-days: 1 - - agent: - needs: - - activation - - publish_review_provenance - if: needs.activation.outputs.daily_ai_credits_exceeded != 'true' - runs-on: ubuntu-latest - permissions: - checks: read - contents: read - issues: read - pull-requests: read - env: - DEFAULT_BRANCH: ${{ github.event.repository.default_branch }} - GH_AW_ASSETS_ALLOWED_EXTS: "" - GH_AW_ASSETS_BRANCH: "" - GH_AW_ASSETS_MAX_SIZE_KB: 0 - GH_AW_MCP_LOG_DIR: /tmp/gh-aw/mcp-logs/safeoutputs - GH_AW_RUNTIME_FEATURES: ${{ vars.GH_AW_RUNTIME_FEATURES }} - GH_AW_WORKFLOW_ID_SANITIZED: sflprreview - outputs: - agentic_engine_timeout: ${{ steps.detect-agent-errors.outputs.agentic_engine_timeout || 'false' }} - ai_credits_rate_limit_error: ${{ steps.parse-mcp-gateway.outputs.ai_credits_rate_limit_error || 'false' }} - aic: ${{ steps.parse-mcp-gateway.outputs.aic }} - ambient_context: ${{ steps.parse-mcp-gateway.outputs.ambient_context }} - checkout_pr_success: ${{ steps.checkout-pr.outputs.checkout_pr_success || 'true' }} - effective_tokens: ${{ steps.parse-mcp-gateway.outputs.effective_tokens }} - has_patch: ${{ steps.collect_output.outputs.has_patch }} - http_400_response_error: ${{ steps.detect-agent-errors.outputs.http_400_response_error || 'false' }} - inference_access_error: ${{ steps.detect-agent-errors.outputs.inference_access_error || 'false' }} - invocation_cap_exceeded: ${{ steps.detect-agent-errors.outputs.invocation_cap_exceeded || 'false' }} - max_cache_misses_exceeded: ${{ steps.detect-agent-errors.outputs.max_cache_misses_exceeded || 'false' }} - mcp_policy_error: ${{ steps.detect-agent-errors.outputs.mcp_policy_error || 'false' }} - missing_model_pricing_error: ${{ steps.detect-agent-errors.outputs.missing_model_pricing_error || 'false' }} - missing_model_pricing_model_name: ${{ steps.detect-agent-errors.outputs.missing_model_pricing_model_name || '' }} - model: ${{ needs.activation.outputs.model }} - model_not_supported_error: ${{ steps.detect-agent-errors.outputs.model_not_supported_error || 'false' }} - output: ${{ steps.collect_output.outputs.output }} - output_types: ${{ steps.collect_output.outputs.output_types }} - setup-parent-span-id: ${{ steps.setup.outputs.parent-span-id || steps.setup.outputs.span-id }} - setup-span-id: ${{ steps.setup.outputs.span-id }} - setup-trace-id: ${{ steps.setup.outputs.trace-id }} - unknown_model_ai_credits: ${{ steps.parse-mcp-gateway.outputs.unknown_model_ai_credits || 'false' }} - steps: - - name: Setup Scripts - id: setup - uses: github/gh-aw-actions/setup@6aab9e5b5c91c615506061f09bedd81a23babe3c # v0.86.2 - with: - destination: ${{ runner.temp }}/gh-aw/actions - job-name: ${{ github.job }} - trace-id: ${{ needs.activation.outputs.setup-trace-id }} - parent-span-id: ${{ needs.activation.outputs.setup-parent-span-id || needs.activation.outputs.setup-span-id }} - env: - GH_AW_SETUP_WORKFLOW_NAME: "SFL PR Review" - GH_AW_CURRENT_WORKFLOW_REF: ${{ github.repository }}/.github/workflows/sfl-pr-review.lock.yml@${{ github.ref }} - GH_AW_INFO_VERSION: "1.0.79" - GH_AW_INFO_AWF_VERSION: "v0.27.44" - GH_AW_INFO_BODY_MODIFIED: "false" - GH_AW_INFO_ENGINE_ID: "copilot" - - name: Set runtime paths - id: set-runtime-paths - run: | - { - echo "GH_AW_SAFE_OUTPUTS=${RUNNER_TEMP}/gh-aw/safeoutputs/outputs.jsonl" - echo "GH_AW_SAFE_OUTPUTS_CONFIG_PATH=${RUNNER_TEMP}/gh-aw/safeoutputs/config.json" - echo "GH_AW_SAFE_OUTPUTS_TOOLS_PATH=${RUNNER_TEMP}/gh-aw/safeoutputs/tools.json" - } >> "$GITHUB_OUTPUT" - - env: - AW_CONTEXT: ${{ inputs.aw_context }} - BASE_SHA: ${{ inputs.base_sha }} - DISPATCH_ID: ${{ inputs.dispatch_id }} - HEAD_SHA: ${{ inputs.head_sha }} - ITEM_NUMBER: ${{ inputs.item_number }} - name: Validate trusted review context - run: "set -euo pipefail\nif ! [[ \"$ITEM_NUMBER\" =~ ^[1-9][0-9]*$ ]]; then\n echo \"::error::Invalid pull request number\"\n exit 1\nfi\nif ! [[ \"$BASE_SHA\" =~ ^[0-9a-f]{40}$ ]] \\\n || ! [[ \"$HEAD_SHA\" =~ ^[0-9a-f]{40}$ ]]; then\n echo \"::error::Invalid pull request revision\"\n exit 1\nfi\nif ! [[ \"$DISPATCH_ID\" =~ ^[A-Za-z0-9._-]{1,64}$ ]]; then\n echo \"::error::Invalid dispatch identifier\"\n exit 1\nfi\nif ! jq -e \\\n --argjson item_number \"$ITEM_NUMBER\" \\\n --arg base_sha \"$BASE_SHA\" \\\n --arg head_sha \"$HEAD_SHA\" '\n . == {\n item_type: \"pull_request\",\n item_number: $item_number,\n base_sha: $base_sha,\n head_sha: $head_sha\n }\n ' <<< \"$AW_CONTEXT\" > /dev/null; then\n echo \"::error::Untrusted or inconsistent review context\"\n exit 1\nfi\n" - - id: sfl-ripgrep - name: Install ripgrep with bounded diagnostics - run: "set -euo pipefail\nlog_path=\"${RUNNER_TEMP}/sfl-ripgrep-install.log\"\n: > \"$log_path\"\nif command -v rg > /dev/null 2>&1; then\n rg --version | tee -a \"$log_path\"\n exit 0\nfi\n\nset +e\ntimeout --signal=TERM --kill-after=15s 180s bash -c '\n set -euo pipefail\n sudo apt-get update \\\n -o Acquire::Retries=3 \\\n -o Acquire::http::Timeout=30 \\\n -o Acquire::https::Timeout=30\n sudo apt-get install --no-install-recommends -y ripgrep\n' 2>&1 | tee -a \"$log_path\"\ninstall_status=${PIPESTATUS[0]}\nset -e\n\nif [ \"$install_status\" -eq 124 ] || [ \"$install_status\" -eq 137 ]; then\n echo \"::error::ripgrep installation exceeded the 180-second setup limit\"\n exit 1\nfi\nif [ \"$install_status\" -ne 0 ]; then\n echo \"::error::ripgrep installation failed with exit code ${install_status}\"\n exit \"$install_status\"\nfi\nif ! command -v rg > /dev/null 2>&1; then\n echo \"::error::ripgrep installation completed without making rg available\"\n exit 1\nfi\nrg --version | tee -a \"$log_path\"\n" - shell: bash - - if: always() - name: Retain ripgrep setup diagnostics - uses: actions/upload-artifact@043fb46d1a93c77aae656e7c1c64a875d1fc6a0a # v7.0.1 - with: - if-no-files-found: ignore - name: sfl-ripgrep-setup-${{ github.run_id }}-${{ github.run_attempt }} - path: ${{ runner.temp }}/sfl-ripgrep-install.log - retention-days: 1 - - - name: Checkout repository - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 - with: - persist-credentials: false - - name: Create gh-aw temp directory - run: bash "${RUNNER_TEMP}/gh-aw/actions/create_gh_aw_tmp_dir.sh" - - name: Configure gh CLI for GitHub Enterprise - run: bash "${RUNNER_TEMP}/gh-aw/actions/configure_gh_for_ghe.sh" - env: - GH_TOKEN: ${{ github.token }} - - name: Download activation artifact - uses: actions/download-artifact@3e5f45b2cfb9172054b4087a40e8e0b5a5461e7c # v8.0.1 - with: - name: activation - path: /tmp/gh-aw - - name: Configure Git credentials - env: - GITHUB_REPOSITORY: ${{ github.repository }} - GITHUB_SERVER_URL: ${{ github.server_url }} - GITHUB_TOKEN: ${{ github.token }} - run: bash "${RUNNER_TEMP}/gh-aw/actions/configure_git_credentials.sh" - - name: Checkout PR branch - id: checkout-pr - if: | - github.event.pull_request || github.event.issue.pull_request || github.event_name == 'workflow_dispatch' && fromJSON(github.event.inputs.aw_context || '{}').item_type == 'pull_request' - uses: actions/github-script@3a2844b7e9c422d3c10d287c895573f7108da1b3 # v9.0.0 - env: - GH_TOKEN: ${{ secrets.GH_AW_GITHUB_MCP_SERVER_TOKEN || secrets.GH_AW_GITHUB_TOKEN || secrets.GITHUB_TOKEN }} - with: - github-token: ${{ secrets.GH_AW_GITHUB_MCP_SERVER_TOKEN || secrets.GH_AW_GITHUB_TOKEN || secrets.GITHUB_TOKEN }} - script: | - const { setupGlobals } = require('${{ runner.temp }}/gh-aw/actions/setup_globals.cjs'); - setupGlobals(core, github, context, exec, io, getOctokit); - const { main } = require('${{ runner.temp }}/gh-aw/actions/checkout_pr_branch.cjs'); - await main(); - - name: Install ripgrep - run: bash "${RUNNER_TEMP}/gh-aw/actions/install_ripgrep.sh" - - name: Install GitHub Copilot CLI - run: bash "${RUNNER_TEMP}/gh-aw/actions/install_copilot_cli.sh" - env: - GH_HOST: github.com - GH_AW_COMPILED_VERSION: v0.86.2 - - name: Install AWF binary - run: bash "${RUNNER_TEMP}/gh-aw/actions/install_awf_binary.sh" v0.27.44 --rootless - - name: Determine automatic lockdown mode for GitHub MCP Server - id: determine-automatic-lockdown - uses: actions/github-script@3a2844b7e9c422d3c10d287c895573f7108da1b3 # v9.0.0 (source v9) - env: - GH_AW_GITHUB_TOKEN: ${{ secrets.GH_AW_GITHUB_TOKEN }} - GH_AW_GITHUB_MCP_SERVER_TOKEN: ${{ secrets.GH_AW_GITHUB_MCP_SERVER_TOKEN }} - with: - script: | - const determineAutomaticLockdown = require('${{ runner.temp }}/gh-aw/actions/determine_automatic_lockdown.cjs'); - await determineAutomaticLockdown(github, context, core); - - name: Restore agent config folders from base branch - if: steps.checkout-pr.outcome == 'success' - env: - GH_AW_AGENT_FOLDERS: ".agents .github" - GH_AW_AGENT_FILES: "AGENTS.md" - run: bash "${RUNNER_TEMP}/gh-aw/actions/restore_base_github_folders.sh" - - name: Restore inline sub-agents from activation artifact - env: - GH_AW_SUB_AGENT_DIR: ".github/agents" - GH_AW_SUB_AGENT_EXT: ".agent.md" - run: bash "${RUNNER_TEMP}/gh-aw/actions/restore_inline_sub_agents.sh" - - name: Restore inline skills from activation artifact - env: - GH_AW_SKILL_DIR: ".github/skills" - run: bash "${RUNNER_TEMP}/gh-aw/actions/restore_inline_skills.sh" - - name: Download container images - run: bash "${RUNNER_TEMP}/gh-aw/actions/download_docker_images.sh" ghcr.io/github/gh-aw-firewall/agent:0.27.44@sha256:0d727725c737b58c7bdf51f640cffb928385ec46517e0917c7f1a02f1bada8b4 ghcr.io/github/gh-aw-firewall/api-proxy:0.27.44@sha256:b50fbadba138f6e9aba94aca09711335c489bb3b15861220cb66f6092e042dc7 ghcr.io/github/gh-aw-firewall/squid:0.27.44@sha256:83e48bbe12c634be8c228a576832fe45f66c529ac3659db92bddbcf2eeb6d627 ghcr.io/github/gh-aw-mcpg:v0.4.9@sha256:e5a1569aeaf41820fa7bdee3e94468cae448133cdbf00119ad24f5b74db1ab9f ghcr.io/github/gh-aw-node@sha256:0d9f1fb5fd6610c0ac1f5194a38e45a8a1e81f8a390d5142d8e4e6f26a4b3196 ghcr.io/github/github-mcp-server:v1.9.0@sha256:881b53d6f75f69bdbc1b5b10fc2f1361717c19054143b3a8529fb5c32061a50e - - name: Generate Safe Outputs Config - env: - GH_AW_INPUT_HEAD_SHA: ${{ inputs.head_sha }} - GH_AW_INPUT_ITEM_NUMBER: ${{ inputs.item_number }} - run: | - mkdir -p "${RUNNER_TEMP}/gh-aw/safeoutputs" - mkdir -p /tmp/gh-aw/safeoutputs - mkdir -p /tmp/gh-aw/mcp-logs/safeoutputs - cat > "${RUNNER_TEMP}/gh-aw/safeoutputs/config.json" << 'GH_AW_SAFE_OUTPUTS_CONFIG_278c0a04523f507c_EOF' - {"create_pull_request_review_comment":{"commit_id":"${GH_AW_INPUT_HEAD_SHA}","max":30,"side":"RIGHT","target":"${GH_AW_INPUT_ITEM_NUMBER}"},"create_report_incomplete_issue":{},"missing_data":{},"missing_tool":{},"noop":{"max":1,"report-as-issue":"false"},"report_incomplete":{},"resolve-sfl-review-thread":{"description":"Resolve one obsolete SFL finding thread after deterministic validation","inputs":{"thread_id":{"default":null,"description":"GraphQL node ID of the obsolete SFL review thread","required":true,"type":"string"}}},"submit_pull_request_review":{"commit_id":"${GH_AW_INPUT_HEAD_SHA}","max":1,"target":"${GH_AW_INPUT_ITEM_NUMBER}"}} - GH_AW_SAFE_OUTPUTS_CONFIG_278c0a04523f507c_EOF - - name: Generate Safe Outputs Tools - env: - GH_AW_TOOLS_META_JSON: | - { - "description_suffixes": { - "create_pull_request_review_comment": " CONSTRAINTS: Maximum 30 review comment(s) can be created. Comments will be on the RIGHT side of the diff.", - "submit_pull_request_review": " CONSTRAINTS: Maximum 1 review(s) can be submitted. Target: ${{ inputs.item_number }}." - }, - "repo_params": {}, - "dynamic_tools": [ - { - "description": "Resolve one obsolete SFL finding thread after deterministic validation", - "inputSchema": { - "additionalProperties": false, - "properties": { - "thread_id": { - "description": "GraphQL node ID of the obsolete SFL review thread", - "type": "string" - } - }, - "required": [ - "thread_id" - ], - "type": "object" - }, - "name": "resolve_sfl_review_thread" - } - ] - } - GH_AW_VALIDATION_JSON: | - { - "create_pull_request_review_comment": { - "defaultMax": 1, - "fields": { - "body": { - "required": true, - "type": "string", - "sanitize": true, - "maxLength": 65000 - }, - "line": { - "required": true, - "positiveInteger": true - }, - "path": { - "required": true, - "type": "string" - }, - "pull_request_number": { - "optionalPositiveInteger": true - }, - "repo": { - "type": "string", - "maxLength": 256 - }, - "side": { - "type": "string", - "enum": [ - "LEFT", - "RIGHT" - ] - }, - "start_line": { - "optionalPositiveInteger": true - } - }, - "customValidation": "startLineLessOrEqualLine" - }, - "missing_data": { - "defaultMax": 20, - "fields": { - "alternatives": { - "type": "string", - "sanitize": true, - "maxLength": 256 - }, - "context": { - "type": "string", - "sanitize": true, - "maxLength": 256 - }, - "data_type": { - "type": "string", - "sanitize": true, - "maxLength": 128 - }, - "reason": { - "type": "string", - "sanitize": true, - "maxLength": 256 - } - } - }, - "missing_tool": { - "defaultMax": 20, - "fields": { - "alternatives": { - "type": "string", - "sanitize": true, - "maxLength": 512 - }, - "reason": { - "required": true, - "type": "string", - "sanitize": true, - "maxLength": 256 - }, - "tool": { - "type": "string", - "sanitize": true, - "maxLength": 128 - } - } - }, - "noop": { - "defaultMax": 1, - "fields": { - "message": { - "required": true, - "type": "string", - "sanitize": true, - "maxLength": 65000 - } - } - }, - "report_incomplete": { - "defaultMax": 5, - "fields": { - "details": { - "type": "string", - "sanitize": true, - "maxLength": 65000 - }, - "reason": { - "required": true, - "type": "string", - "sanitize": true, - "maxLength": 1024 - } - } - }, - "submit_pull_request_review": { - "defaultMax": 1, - "fields": { - "body": { - "type": "string", - "sanitize": true, - "maxLength": 65000 - }, - "event": { - "type": "string", - "enum": [ - "APPROVE", - "REQUEST_CHANGES", - "COMMENT" - ] - }, - "pull_request_number": { - "issueOrPRNumber": true - }, - "repo": { - "type": "string", - "maxLength": 256 - } - } - } - } - uses: actions/github-script@3a2844b7e9c422d3c10d287c895573f7108da1b3 # v9.0.0 - with: - script: | - const { setupGlobals } = require('${{ runner.temp }}/gh-aw/actions/setup_globals.cjs'); - setupGlobals(core, github, context, exec, io, getOctokit); - const { main } = require('${{ runner.temp }}/gh-aw/actions/generate_safe_outputs_tools.cjs'); - await main(); - - name: Start MCP Gateway - id: start-mcp-gateway - env: - GH_AW_POLICY_ALLOW_CREATE_PULL_REQUEST: ${{ vars.GH_AW_POLICY_ALLOW_CREATE_PULL_REQUEST || 'true' }} - GH_AW_SAFE_OUTPUTS: ${{ steps.set-runtime-paths.outputs.GH_AW_SAFE_OUTPUTS }} - GH_AW_SAFE_OUTPUTS_CONFIG_PATH: ${{ steps.set-runtime-paths.outputs.GH_AW_SAFE_OUTPUTS_CONFIG_PATH }} - GH_AW_SAFE_OUTPUTS_TOOLS_PATH: ${{ steps.set-runtime-paths.outputs.GH_AW_SAFE_OUTPUTS_TOOLS_PATH }} - GH_AW_SINK_VISIBILITY: ${{ steps.determine-automatic-lockdown.outputs.visibility }} - GITHUB_MCP_GUARD_MIN_INTEGRITY: ${{ steps.determine-automatic-lockdown.outputs.min_integrity }} - GITHUB_MCP_GUARD_REPOS: ${{ steps.determine-automatic-lockdown.outputs.repos }} - GITHUB_MCP_SERVER_TOKEN: ${{ secrets.GH_AW_GITHUB_MCP_SERVER_TOKEN || secrets.GH_AW_GITHUB_TOKEN || secrets.GITHUB_TOKEN }} - GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} - GH_AW_INPUT_HEAD_SHA: ${{ inputs.head_sha }} - GH_AW_INPUT_ITEM_NUMBER: ${{ inputs.item_number }} - run: | - set -eo pipefail - mkdir -p "${RUNNER_TEMP}/gh-aw/mcp-config" - - # Export gateway environment variables for MCP config and gateway script - export MCP_GATEWAY_PORT="8080" - export MCP_GATEWAY_DOMAIN="awmg-mcpg" - export MCP_GATEWAY_HOST_DOMAIN="localhost" - MCP_GATEWAY_API_KEY=$(openssl rand -base64 45 | tr -d '/+=') - echo "::add-mask::${MCP_GATEWAY_API_KEY}" - export MCP_GATEWAY_API_KEY - export MCP_GATEWAY_PAYLOAD_DIR="/tmp/gh-aw/mcp-payloads" - mkdir -p "${MCP_GATEWAY_PAYLOAD_DIR}" - export MCP_GATEWAY_PAYLOAD_SIZE_THRESHOLD="524288" - export MCP_GATEWAY_ALLOWED_MOUNT_ROOTS="${GITHUB_WORKSPACE}:rw,${RUNNER_TEMP}/gh-aw:ro,${RUNNER_TEMP}/gh-aw/safeoutputs:rw,/opt:ro,/tmp:rw,/usr/bin/gh:ro" - export DEBUG="*" - - export GH_AW_ENGINE="copilot" - MCP_GATEWAY_UID=$(id -u 2>/dev/null || echo '0') - MCP_GATEWAY_GID=$(id -g 2>/dev/null || echo '0') - source "${RUNNER_TEMP}/gh-aw/actions/resolve_docker_socket_gid.sh" - export MCP_GATEWAY_DOCKER_COMMAND='docker run -i --rm --network bridge -p 127.0.0.1:'"${MCP_GATEWAY_PORT}"':'"${MCP_GATEWAY_PORT}"' --name awmg-mcpg --add-host host.docker.internal:host-gateway --user '"${MCP_GATEWAY_UID}"':'"${MCP_GATEWAY_GID}"' --group-add '"${DOCKER_SOCK_GID}"' -v '"${DOCKER_SOCK_PATH}"':/var/run/docker.sock -e MCP_GATEWAY_PORT -e MCP_GATEWAY_DOMAIN -e MCP_GATEWAY_API_KEY -e MCP_GATEWAY_PAYLOAD_DIR -e MCP_GATEWAY_PAYLOAD_SIZE_THRESHOLD -e DOCKER_HOST=unix:///var/run/docker.sock -e DEBUG -e MCP_GATEWAY_LOG_DIR -e GH_AW_MCP_LOG_DIR -e GH_AW_SAFE_OUTPUTS -e GH_AW_SAFE_OUTPUTS_CONFIG_PATH -e GH_AW_SAFE_OUTPUTS_TOOLS_PATH -e GH_AW_POLICY_ALLOW_CREATE_PULL_REQUEST -e GH_AW_ASSETS_BRANCH -e GH_AW_ASSETS_MAX_SIZE_KB -e GH_AW_ASSETS_ALLOWED_EXTS -e DEFAULT_BRANCH -e GITHUB_MCP_SERVER_TOKEN -e GITHUB_MCP_GUARD_MIN_INTEGRITY -e GITHUB_MCP_GUARD_REPOS -e GH_AW_SINK_VISIBILITY -e GITHUB_REPOSITORY -e GITHUB_SERVER_URL -e GITHUB_SHA -e GITHUB_WORKSPACE -e GITHUB_TOKEN -e GITHUB_RUN_ID -e GITHUB_RUN_NUMBER -e GITHUB_RUN_ATTEMPT -e GITHUB_JOB -e GITHUB_ACTION -e GITHUB_EVENT_NAME -e GITHUB_EVENT_PATH -e GITHUB_ACTOR -e GITHUB_ACTOR_ID -e GITHUB_TRIGGERING_ACTOR -e GITHUB_WORKFLOW -e GITHUB_WORKFLOW_REF -e GITHUB_WORKFLOW_SHA -e GITHUB_REF -e GITHUB_REF_NAME -e GITHUB_REF_TYPE -e GITHUB_HEAD_REF -e GITHUB_BASE_REF -e RUNNER_TEMP -e MCP_GATEWAY_ALLOWED_MOUNT_ROOTS -e GH_AW_INPUT_HEAD_SHA -e GH_AW_INPUT_ITEM_NUMBER -v /tmp/gh-aw/mcp-payloads:/tmp/gh-aw/mcp-payloads:rw -v /opt:/opt:ro -v /tmp:/tmp:rw -v '"${GITHUB_WORKSPACE}"':'"${GITHUB_WORKSPACE}"':rw -v '"${RUNNER_TEMP}"'/gh-aw/safeoutputs:'"${RUNNER_TEMP}"'/gh-aw/safeoutputs:rw ghcr.io/github/gh-aw-mcpg:v0.4.9' - - mkdir -p "$HOME/.copilot" - GH_AW_NODE=$(which node 2>/dev/null || command -v node 2>/dev/null || echo node) - cat << GH_AW_MCP_CONFIG_600e18092f4c456e_EOF | "$GH_AW_NODE" "${RUNNER_TEMP}/gh-aw/actions/start_mcp_gateway.cjs" - { - "mcpServers": { - "github": { - "type": "stdio", - "container": "ghcr.io/github/github-mcp-server:v1.9.0", - "env": { - "GITHUB_FEATURES": "fields_param", - "GITHUB_HOST": "${GITHUB_SERVER_URL}", - "GITHUB_PERSONAL_ACCESS_TOKEN": "${GITHUB_MCP_SERVER_TOKEN}", - "GITHUB_READ_ONLY": "1", - "GITHUB_TOOLSETS": "context,repos,issues,pull_requests" - }, - "guard-policies": { - "allow-only": { - "min-integrity": "$GITHUB_MCP_GUARD_MIN_INTEGRITY", - "repos": "$GITHUB_MCP_GUARD_REPOS" - } - } - }, - "safeoutputs": { - "type": "stdio", - "container": "ghcr.io/github/gh-aw-node", - "mounts": ["\${GITHUB_WORKSPACE}:\${GITHUB_WORKSPACE}:rw", "${RUNNER_TEMP}/gh-aw/safeoutputs:${RUNNER_TEMP}/gh-aw/safeoutputs:rw", "/tmp/gh-aw:/tmp/gh-aw:rw"], - "args": ["-w", "\${GITHUB_WORKSPACE}"], - "entrypoint": "sh", - "entrypointArgs": ["-c", "sh ${RUNNER_TEMP}/gh-aw/safeoutputs/start_safe_outputs_mcp.sh"], - "env": { - "DEBUG": "*", - "DEFAULT_BRANCH": "\${DEFAULT_BRANCH}", - "GH_AW_ASSETS_ALLOWED_EXTS": "\${GH_AW_ASSETS_ALLOWED_EXTS}", - "GH_AW_ASSETS_BRANCH": "\${GH_AW_ASSETS_BRANCH}", - "GH_AW_ASSETS_MAX_SIZE_KB": "\${GH_AW_ASSETS_MAX_SIZE_KB}", - "GH_AW_MCP_LOG_DIR": "\${GH_AW_MCP_LOG_DIR}", - "GH_AW_SAFE_OUTPUTS": "\${GH_AW_SAFE_OUTPUTS}", - "GH_AW_SAFE_OUTPUTS_CONFIG_PATH": "\${GH_AW_SAFE_OUTPUTS_CONFIG_PATH}", - "GH_AW_SAFE_OUTPUTS_TOOLS_PATH": "\${GH_AW_SAFE_OUTPUTS_TOOLS_PATH}", - "GH_AW_POLICY_ALLOW_CREATE_PULL_REQUEST": "\${GH_AW_POLICY_ALLOW_CREATE_PULL_REQUEST}", - "GITHUB_REPOSITORY": "\${GITHUB_REPOSITORY}", - "GITHUB_SHA": "\${GITHUB_SHA}", - "GITHUB_TOKEN": "\${GITHUB_TOKEN}", - "GITHUB_WORKSPACE": "\${GITHUB_WORKSPACE}", - "RUNNER_TEMP": "\${RUNNER_TEMP}", - "GH_AW_INPUT_HEAD_SHA": "\${GH_AW_INPUT_HEAD_SHA}", - "GH_AW_INPUT_ITEM_NUMBER": "\${GH_AW_INPUT_ITEM_NUMBER}" - }, - "guard-policies": { - "write-sink": { - "accept": [ - "*" - ], - "sink-visibility": "${GH_AW_SINK_VISIBILITY}" - } - } - } - }, - "gateway": { - "port": $MCP_GATEWAY_PORT, - "domain": "${MCP_GATEWAY_DOMAIN}", - "apiKey": "${MCP_GATEWAY_API_KEY}", - "payloadDir": "${MCP_GATEWAY_PAYLOAD_DIR}", - "startupTimeout": 120 - } - } - GH_AW_MCP_CONFIG_600e18092f4c456e_EOF - - name: Mount MCP servers as CLIs - id: mount-mcp-clis - continue-on-error: true - env: - MCP_GATEWAY_API_KEY: ${{ steps.start-mcp-gateway.outputs.gateway-api-key }} - MCP_GATEWAY_DOMAIN: ${{ steps.start-mcp-gateway.outputs.gateway-domain }} - MCP_GATEWAY_PORT: ${{ steps.start-mcp-gateway.outputs.gateway-port }} - uses: actions/github-script@3a2844b7e9c422d3c10d287c895573f7108da1b3 # v9.0.0 - with: - script: | - const { setupGlobals } = require('${{ runner.temp }}/gh-aw/actions/setup_globals.cjs'); - setupGlobals(core, github, context, exec, io); - const { main } = require('${{ runner.temp }}/gh-aw/actions/mount_mcp_as_cli.cjs'); - await main(); - - name: Clean credentials - continue-on-error: true - run: bash "${RUNNER_TEMP}/gh-aw/actions/clean_git_credentials.sh" - - name: Audit pre-agent workspace - id: pre_agent_audit - continue-on-error: true - run: bash "${RUNNER_TEMP}/gh-aw/actions/audit_pre_agent_workspace.sh" - - name: Execute GitHub Copilot CLI - id: agentic_execution - # Copilot CLI tool arguments (sorted): - timeout-minutes: 20 - run: | - set -o pipefail - printf '%s' "$(date +%s%3N)" > /tmp/gh-aw/agent_cli_start_ms.txt - trap 'gh_aw_exit_code=$?; mkdir -p /tmp/gh-aw >/dev/null 2>&1 || true; printf "%s" "$gh_aw_exit_code" > /tmp/gh-aw/agent_execution_exit_code.txt || true; rm -f "$HOME/.copilot/settings.json"' EXIT - mkdir -p "$HOME/.copilot" - printf '%s' '{"builtInAgents":{"rubberDuck":false}}' > "$HOME/.copilot/settings.json" - export XDG_CONFIG_HOME="$HOME" - export GH_AW_MCP_CONFIG="$HOME/.copilot/mcp-config.json" - GH_AW_COPILOT_SRC="$(command -v copilot 2>/dev/null || true)" - if [ -z "$GH_AW_COPILOT_SRC" ] || [ ! -x "$GH_AW_COPILOT_SRC" ]; then - echo "GitHub Copilot CLI executable not found on PATH after installation" >&2 - exit 127 - fi - GH_AW_COPILOT_BIN="${RUNNER_TEMP}/gh-aw/bin/copilot" - mkdir -p "${RUNNER_TEMP}/gh-aw/bin" - if [ "$GH_AW_COPILOT_SRC" != "$GH_AW_COPILOT_BIN" ]; then - cp "$GH_AW_COPILOT_SRC" "$GH_AW_COPILOT_BIN" - fi - chmod 755 "$GH_AW_COPILOT_BIN" - - touch /tmp/gh-aw/agent-step-summary.md - GH_AW_NODE_BIN=$(command -v node 2>/dev/null || true) - export GH_AW_NODE_BIN - export COPILOT_API_KEY="$COPILOT_DUMMY_BYOK" - (umask 177 && touch /tmp/gh-aw/agent-stdio.log) - GH_AW_MAX_AI_CREDITS="${GH_AW_MAX_AI_CREDITS:-1000}" - printf '%s\n' "{\"\$schema\":\"https://github.com/github/gh-aw-firewall/releases/download/v0.27.44/awf-config.schema.json\",\"network\":{\"allowDomains\":[\"api.business.githubcopilot.com\",\"api.enterprise.githubcopilot.com\",\"api.github.com\",\"api.githubcopilot.com\",\"api.individual.githubcopilot.com\",\"github.com\",\"host.docker.internal\",\"openrouter.ai\",\"raw.githubusercontent.com\",\"registry.npmjs.org\",\"telemetry.enterprise.githubcopilot.com\"],\"isolation\":true,\"topologyAttach\":[\"awmg-mcpg\"]},\"apiProxy\":{\"enabled\":true,\"enableTokenSteering\":true,\"maxRuns\":500,\"maxAiCredits\":${GH_AW_MAX_AI_CREDITS},\"maxCacheMisses\":10,\"targets\":{\"copilot\":{\"host\":\"openrouter.ai\"}},\"providers\":{\"github-copilot\":{\"models\":{\"moonshotai/kimi-k3\":{\"cost\":{\"input\":\"3e-06\",\"output\":\"1.5e-05\"}}}}},\"models\":{\"agent\":[\"sonnet-6x\",\"gpt-5.4\",\"gpt-5.5\",\"gpt-5.6\",\"gpt-5.3\",\"gemini-pro\",\"any\"],\"antigravity\":[\"copilot/antigravity*\",\"google/antigravity*\",\"gemini/antigravity*\"],\"any\":[\"copilot/*\",\"anthropic/*\",\"openai/*\",\"google/*\",\"gemini/*\"],\"auto\":[\"copilot/auto\",\"large\"],\"claude\":[\"agent\"],\"codex\":[\"agent\"],\"coding\":[\"copilot/gpt-5*codex*\",\"openai/gpt-5*codex*\",\"gpt-5-codex\",\"kimi\"],\"computer-use\":[\"copilot/*computer-use*\",\"google/*computer-use*\",\"gemini/*computer-use*\",\"openai/*computer-use*\"],\"copilot\":[\"agent\"],\"deep-research\":[\"copilot/deep-research*\",\"copilot/o3-deep-research*\",\"copilot/o4-mini-deep-research*\",\"google/deep-research*\",\"gemini/deep-research*\",\"openai/o3-deep-research*\",\"openai/o4-mini-deep-research*\"],\"detection\":[\"small\"],\"evals\":[\"small\"],\"fable\":[\"copilot/*fable*\",\"anthropic/*fable*\"],\"gemini\":[\"agent\"],\"gemini-3-flash\":[\"copilot/gemini-3*flash*\",\"google/gemini-3*flash*\",\"gemini/gemini-3*flash*\"],\"gemini-3-pro\":[\"copilot/gemini-3*pro*\",\"google/gemini-3*pro*\",\"google/nano-banana*\",\"gemini/gemini-3*pro*\"],\"gemini-3.1-flash\":[\"copilot/gemini-3.1*flash*\",\"google/gemini-3.1*flash*\",\"gemini/gemini-3.1*flash*\"],\"gemini-3.1-pro\":[\"copilot/gemini-3.1*pro*\",\"google/gemini-3.1*pro*\",\"gemini/gemini-3.1*pro*\"],\"gemini-3.5-flash\":[\"copilot/gemini-3.5*flash*\",\"google/gemini-3.5*flash*\",\"gemini/gemini-3.5*flash*\"],\"gemini-3.6-flash\":[\"copilot/gemini-3.6*flash*\",\"google/gemini-3.6*flash*\",\"gemini/gemini-3.6*flash*\"],\"gemini-flash\":[\"copilot/gemini-*flash*\",\"google/gemini-*flash*\",\"gemini/gemini-*flash*\"],\"gemini-flash-lite\":[\"copilot/gemini-*flash*lite*\",\"google/gemini-*flash*lite*\",\"gemini/gemini-*flash*lite*\"],\"gemini-omni\":[\"copilot/gemini-omni*\",\"google/gemini-omni*\",\"gemini/gemini-omni*\"],\"gemini-pro\":[\"copilot/gemini-*pro*\",\"google/gemini-*pro*\",\"gemini/gemini-*pro*\"],\"gemma\":[\"copilot/gemma*\",\"google/gemma*\",\"gemini/gemma*\"],\"gpt-5\":[\"copilot/gpt-5*\",\"openai/gpt-5*\"],\"gpt-5-codex\":[\"copilot/gpt-5*codex*\",\"openai/gpt-5*codex*\"],\"gpt-5-mini\":[\"copilot/gpt-5*mini*\",\"openai/gpt-5*mini*\"],\"gpt-5-nano\":[\"copilot/gpt-5*nano*\",\"openai/gpt-5*nano*\"],\"gpt-5-pro\":[\"copilot/gpt-5*pro*\",\"openai/gpt-5*pro*\"],\"gpt-5.1\":[\"copilot/gpt-5.1*\",\"openai/gpt-5.1*\"],\"gpt-5.2\":[\"copilot/gpt-5.2*\",\"openai/gpt-5.2*\"],\"gpt-5.3\":[\"copilot/gpt-5.3*\",\"openai/gpt-5.3*\"],\"gpt-5.4\":[\"copilot/gpt-5.4*\",\"openai/gpt-5.4*\"],\"gpt-5.5\":[\"copilot/gpt-5.5*\",\"openai/gpt-5.5*\"],\"gpt-5.6\":[\"copilot/gpt-5.6*\",\"openai/gpt-5.6*\"],\"grok\":[\"copilot/*grok*\",\"openai/*grok*\"],\"haiku\":[\"copilot/*haiku*\",\"anthropic/*haiku*\"],\"image-generation\":[\"copilot/gpt-image*\",\"openai/gpt-image*\",\"openai/chatgpt-image*\",\"copilot/gemini-*image*\",\"google/gemini-*image*\",\"gemini/gemini-*image*\",\"google/imagen*\"],\"kimi\":[\"copilot/kimi*\",\"openai/kimi*\"],\"kiwi\":[\"copilot/kiwi*\",\"openai/kiwi*\"],\"large\":[\"sonnet\",\"gpt-5-pro\",\"gpt-5\",\"gemini-pro\"],\"lyria\":[\"google/lyria*\",\"gemini/lyria*\",\"copilot/lyria*\"],\"mai-code\":[\"copilot/MAI-Code*\",\"copilot/mai-code*\",\"openai/MAI-Code*\"],\"mai-code-1-flash-picker\":[\"copilot/MAI-Code-1-Flash-picker*\",\"copilot/mai-code-1-flash-picker*\",\"openai/MAI-Code-1-Flash-picker*\"],\"mini\":[\"haiku\",\"gpt-5-mini\",\"gpt-5-nano\",\"gemini-flash-lite\"],\"nano-banana\":[\"copilot/nano-banana*\",\"google/nano-banana*\",\"gemini/nano-banana*\"],\"opus\":[\"copilot/*opus*\",\"anthropic/*opus*\"],\"opusplan\":[\"opus?effort=high\"],\"raptor-mini\":[\"copilot/raptor*\",\"openai/raptor*\"],\"reasoning\":[\"copilot/o1*\",\"copilot/o3*\",\"copilot/o4*\",\"openai/o1*\",\"openai/o3*\",\"openai/o4*\"],\"robotics\":[\"copilot/*robotics*\",\"google/*robotics*\",\"gemini/*robotics*\"],\"small\":[\"mini\"],\"small-agent\":[\"haiku\",\"gpt-5-mini\",\"gemini-flash\"],\"sonnet\":[\"copilot/*sonnet*\",\"anthropic/*sonnet*\"],\"sonnet-6x\":[\"copilot/*sonnet-4.5*\",\"copilot/*sonnet-4.6*\",\"copilot/*sonnet-5*\",\"copilot/*sonnet-4-5-*\",\"anthropic/*sonnet-4-5-*\",\"copilot/*sonnet-4-6*\",\"anthropic/*sonnet-4-6*\",\"anthropic/*sonnet-5*\"],\"summarization\":[\"haiku\",\"gpt-5-mini\",\"gemini-flash-lite\",\"mini\"],\"veo\":[\"google/veo*\",\"gemini/veo*\"],\"vision\":[\"copilot/gemini-*image*\",\"google/gemini-*image*\",\"gemini/gemini-*image*\",\"copilot/gemini-*flash*\",\"google/gemini-*flash*\",\"gemini/gemini-*flash*\"]}},\"container\":{\"imageTag\":\"0.27.44,squid=sha256:83e48bbe12c634be8c228a576832fe45f66c529ac3659db92bddbcf2eeb6d627,agent=sha256:0d727725c737b58c7bdf51f640cffb928385ec46517e0917c7f1a02f1bada8b4,api-proxy=sha256:b50fbadba138f6e9aba94aca09711335c489bb3b15861220cb66f6092e042dc7,cli-proxy=sha256:c064d15974f7c933ec7d3f7b4038f4fd203547b3154bdc821afd379144887eff\"},\"logging\":{\"proxyLogsDir\":\"/tmp/gh-aw/sandbox/firewall/logs\",\"auditDir\":\"/tmp/gh-aw/sandbox/firewall/audit\"}}" > "${RUNNER_TEMP}/gh-aw/awf-config.json" - cp "${RUNNER_TEMP}/gh-aw/awf-config.json" /tmp/gh-aw/awf-config.json - export GH_AW_MODELS_JSON_PATH="/tmp/gh-aw/models.json" - GH_AW_DOCKER_HOST="" - if [[ "${DOCKER_HOST:-}" =~ ^tcp:// ]]; then - GH_AW_DOCKER_HOST="${DOCKER_HOST}" - fi - if [[ "${DOCKER_HOST:-}" =~ ^tcp:// ]]; then - GH_AW_CHROOT_BINARIES_SOURCE_PATH="${RUNNER_TEMP}/gh-aw" GH_AW_CHROOT_IDENTITY_HOME="${RUNNER_TEMP}/gh-aw/home" node "${RUNNER_TEMP}/gh-aw/actions/patch_awf_chroot_config.cjs" - fi - GH_AW_TOOL_CACHE_MOUNT="" - GH_AW_TOOL_CACHE="${RUNNER_TOOL_CACHE:?RUNNER_TOOL_CACHE must be set}" - if [ -d "$GH_AW_TOOL_CACHE" ]; then - if [[ "$GH_AW_TOOL_CACHE" != /opt/* ]]; then - GH_AW_TOOL_CACHE_MOUNT="$GH_AW_TOOL_CACHE:$GH_AW_TOOL_CACHE:ro" - fi - fi - # shellcheck disable=SC1003,SC2016,SC2086 - awf --config "${RUNNER_TEMP}/gh-aw/awf-config.json" --container-workdir "${GITHUB_WORKSPACE}" --mount "${RUNNER_TEMP}/gh-aw:${RUNNER_TEMP}/gh-aw:ro" --mount "${RUNNER_TEMP}/gh-aw:/host${RUNNER_TEMP}/gh-aw:ro" ${GH_AW_TOOL_CACHE_MOUNT:+--mount "$GH_AW_TOOL_CACHE_MOUNT"} ${GH_AW_DOCKER_HOST:+--docker-host "$GH_AW_DOCKER_HOST"} --env-all --exclude-env ACTIONS_ID_TOKEN_REQUEST_TOKEN --exclude-env ACTIONS_ID_TOKEN_REQUEST_URL --exclude-env COPILOT_PROVIDER_API_KEY --exclude-env GITHUB_MCP_SERVER_TOKEN --exclude-env MCP_GATEWAY_API_KEY --mount /tmp/gh-aw:/tmp/gh-aw:rw --log-level info --skip-pull \ - -- /bin/bash -c 'set +o histexpand; export PATH="${RUNNER_TEMP}/gh-aw/mcp-cli/bin:$PATH" && : "${RUNNER_TOOL_CACHE:?RUNNER_TOOL_CACHE must be set}"; GH_AW_TOOL_CACHE="$RUNNER_TOOL_CACHE"; export PATH="$(find "$GH_AW_TOOL_CACHE" -maxdepth 5 -type d -name bin 2>/dev/null | tr '\''\n'\'' '\'':'\'')$PATH"; [ -n "$GOROOT" ] && export PATH="$GOROOT/bin:$PATH" || true; [ -n "$ERLANG_HOME" ] && export PATH="$ERLANG_HOME/bin:$PATH" || true && GH_AW_NODE_EXEC="${GH_AW_NODE_BIN:-}"; if [ -z "$GH_AW_NODE_EXEC" ] || [ ! -x "$GH_AW_NODE_EXEC" ]; then GH_AW_NODE_EXEC="$(command -v node 2>/dev/null || true)"; fi; if [ -z "$GH_AW_NODE_EXEC" ]; then echo "node runtime missing on this runner — check runtimes.node in workflow YAML" >&2; exit 127; fi; GH_AW_NPM_GLOBAL_ROOT="$(npm root -g 2>/dev/null || true)"; if [ -n "$GH_AW_NPM_GLOBAL_ROOT" ]; then export NODE_PATH="${GH_AW_NPM_GLOBAL_ROOT}${NODE_PATH:+:${NODE_PATH}}"; fi; "$GH_AW_NODE_EXEC" "${RUNNER_TEMP}/gh-aw/actions/copilot_harness.cjs" "${RUNNER_TEMP}/gh-aw/bin/copilot" --add-dir /tmp/gh-aw/ --log-level all --log-dir /tmp/gh-aw/sandbox/agent/logs/ --disable-builtin-mcps --no-ask-user --allow-all-tools --allow-all-paths --add-dir "${GITHUB_WORKSPACE}" --prompt-file /tmp/gh-aw/aw-prompts/prompt.txt' 2>&1 | tee -a /tmp/gh-aw/agent-stdio.log - env: - AWF_REFLECT_ENABLED: 1 - COPILOT_AGENT_RUNNER_TYPE: STANDALONE - COPILOT_DUMMY_BYOK: dummy-byok-key-for-offline-mode - COPILOT_MODEL: moonshotai/kimi-k3 - COPILOT_PROVIDER_API_KEY: ${{ secrets.OPENROUTER_API_KEY }} - COPILOT_PROVIDER_BASE_URL: https://openrouter.ai/api/v1 - COPILOT_PROVIDER_TYPE: openai - COPILOT_PROVIDER_WIRE_API: responses - GH_AW_INPUT_HEAD_SHA: ${{ inputs.head_sha }} - GH_AW_INPUT_ITEM_NUMBER: ${{ inputs.item_number }} - GH_AW_LLM_PROVIDER: github - GH_AW_MAX_AI_CREDITS: ${{ vars.GH_AW_DEFAULT_MAX_AI_CREDITS || '1000' }} - GH_AW_MAX_TURNS: ${{ vars.GH_AW_DEFAULT_MAX_TURNS || '' }} - GH_AW_PHASE: agent - GH_AW_PROMPT: /tmp/gh-aw/aw-prompts/prompt.txt - GH_AW_SAFE_OUTPUTS: ${{ steps.set-runtime-paths.outputs.GH_AW_SAFE_OUTPUTS }} - GH_AW_TIMEOUT_MINUTES: 20 - GH_AW_VERSION: v0.86.2 - GITHUB_API_URL: ${{ github.api_url }} - GITHUB_AW: true - GITHUB_COPILOT_INTEGRATION_ID: agentic-workflows - GITHUB_HEAD_REF: ${{ github.head_ref }} - GITHUB_MCP_SERVER_TOKEN: ${{ secrets.GH_AW_GITHUB_MCP_SERVER_TOKEN || secrets.GH_AW_GITHUB_TOKEN || secrets.GITHUB_TOKEN }} - GITHUB_REF_NAME: ${{ github.ref_name }} - GITHUB_SERVER_URL: ${{ github.server_url }} - GITHUB_STEP_SUMMARY: /tmp/gh-aw/agent-step-summary.md - GITHUB_WORKSPACE: ${{ github.workspace }} - GIT_AUTHOR_EMAIL: github-actions[bot]@users.noreply.github.com - GIT_AUTHOR_NAME: github-actions[bot] - GIT_COMMITTER_EMAIL: github-actions[bot]@users.noreply.github.com - GIT_COMMITTER_NAME: github-actions[bot] - RUNNER_TEMP: ${{ runner.temp }} - TRACEPARENT: ${{ env.GITHUB_AW_OTEL_TRACE_ID != '' && env.GITHUB_AW_OTEL_PARENT_SPAN_ID != '' && format('00-{0}-{1}-01', env.GITHUB_AW_OTEL_TRACE_ID, env.GITHUB_AW_OTEL_PARENT_SPAN_ID) || '' }} - - name: Detect agent errors - if: always() - id: detect-agent-errors - continue-on-error: true - run: node "${RUNNER_TEMP}/gh-aw/actions/detect_agent_errors.cjs" - - name: Configure Git credentials - env: - GITHUB_REPOSITORY: ${{ github.repository }} - GITHUB_SERVER_URL: ${{ github.server_url }} - GITHUB_TOKEN: ${{ github.token }} - run: bash "${RUNNER_TEMP}/gh-aw/actions/configure_git_credentials.sh" - - name: Copy Copilot session state files to logs - if: always() - continue-on-error: true - run: bash "${RUNNER_TEMP}/gh-aw/actions/copy_copilot_session_state.sh" - - name: Stop MCP Gateway - if: always() - continue-on-error: true - env: - MCP_GATEWAY_PORT: ${{ steps.start-mcp-gateway.outputs.gateway-port }} - MCP_GATEWAY_API_KEY: ${{ steps.start-mcp-gateway.outputs.gateway-api-key }} - GATEWAY_PID: ${{ steps.start-mcp-gateway.outputs.gateway-pid }} - run: | - bash "${RUNNER_TEMP}/gh-aw/actions/stop_mcp_gateway.sh" "$GATEWAY_PID" - - name: Redact secrets in logs - if: always() - uses: actions/github-script@3a2844b7e9c422d3c10d287c895573f7108da1b3 # v9.0.0 - with: - script: | - const { setupGlobals } = require('${{ runner.temp }}/gh-aw/actions/setup_globals.cjs'); - setupGlobals(core, github, context, exec, io, getOctokit); - const { main } = require('${{ runner.temp }}/gh-aw/actions/redact_secrets.cjs'); - await main(); - env: - GH_AW_SECRET_NAMES: 'GH_AW_GITHUB_MCP_SERVER_TOKEN,GH_AW_GITHUB_TOKEN,GITHUB_TOKEN,OPENROUTER_API_KEY' - SECRET_GH_AW_GITHUB_MCP_SERVER_TOKEN: ${{ secrets.GH_AW_GITHUB_MCP_SERVER_TOKEN }} - SECRET_GH_AW_GITHUB_TOKEN: ${{ secrets.GH_AW_GITHUB_TOKEN }} - SECRET_GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} - SECRET_OPENROUTER_API_KEY: ${{ secrets.OPENROUTER_API_KEY }} - - name: Append agent step summary - if: always() - run: bash "${RUNNER_TEMP}/gh-aw/actions/append_agent_step_summary.sh" - - name: Copy Safe Outputs - if: always() - env: - GH_AW_SAFE_OUTPUTS: ${{ steps.set-runtime-paths.outputs.GH_AW_SAFE_OUTPUTS }} - run: | - mkdir -p /tmp/gh-aw - cp "$GH_AW_SAFE_OUTPUTS" /tmp/gh-aw/safeoutputs.jsonl 2>/dev/null || true - - name: Ingest agent output - id: collect_output - if: always() - uses: actions/github-script@3a2844b7e9c422d3c10d287c895573f7108da1b3 # v9.0.0 - env: - GH_AW_SAFE_OUTPUTS: ${{ steps.set-runtime-paths.outputs.GH_AW_SAFE_OUTPUTS }} - GH_AW_ALLOWED_DOMAINS: "api.business.githubcopilot.com,api.enterprise.githubcopilot.com,api.github.com,api.githubcopilot.com,api.individual.githubcopilot.com,github.com,host.docker.internal,openrouter.ai,raw.githubusercontent.com,registry.npmjs.org,telemetry.enterprise.githubcopilot.com" - GITHUB_SERVER_URL: ${{ github.server_url }} - GITHUB_API_URL: ${{ github.api_url }} - with: - script: | - const { setupGlobals } = require('${{ runner.temp }}/gh-aw/actions/setup_globals.cjs'); - setupGlobals(core, github, context, exec, io, getOctokit); - const { main } = require('${{ runner.temp }}/gh-aw/actions/collect_ndjson_output.cjs'); - await main(); - - name: Parse agent logs for step summary - if: always() - uses: actions/github-script@3a2844b7e9c422d3c10d287c895573f7108da1b3 # v9.0.0 - env: - GH_AW_AGENT_OUTPUT: /tmp/gh-aw/sandbox/agent/logs/ - GH_AW_SAFE_OUTPUTS: ${{ steps.set-runtime-paths.outputs.GH_AW_SAFE_OUTPUTS }} - with: - script: | - const { setupGlobals } = require('${{ runner.temp }}/gh-aw/actions/setup_globals.cjs'); - setupGlobals(core, github, context, exec, io, getOctokit); - const { main } = require('${{ runner.temp }}/gh-aw/actions/parse_copilot_log.cjs'); - await main(); - - name: Parse MCP Gateway logs for step summary - if: always() - id: parse-mcp-gateway - uses: actions/github-script@3a2844b7e9c422d3c10d287c895573f7108da1b3 # v9.0.0 - with: - script: | - const { setupGlobals } = require('${{ runner.temp }}/gh-aw/actions/setup_globals.cjs'); - setupGlobals(core, github, context, exec, io, getOctokit); - const { main } = require('${{ runner.temp }}/gh-aw/actions/parse_mcp_gateway_log.cjs'); - await main(); - - name: Print firewall logs - if: always() - continue-on-error: true - env: - AWF_LOGS_DIR: /tmp/gh-aw/sandbox/firewall/logs - run: bash "${RUNNER_TEMP}/gh-aw/actions/print_firewall_logs.sh" --rootless - - name: Parse token usage for step summary - if: always() - continue-on-error: true - uses: actions/github-script@3a2844b7e9c422d3c10d287c895573f7108da1b3 # v9.0.0 - with: - script: | - const { setupGlobals } = require('${{ runner.temp }}/gh-aw/actions/setup_globals.cjs'); - setupGlobals(core, github, context, exec, io, getOctokit); - const { main } = require('${{ runner.temp }}/gh-aw/actions/parse_token_usage.cjs'); - await main(); - - name: Print AWF reflect summary - if: always() - continue-on-error: true - uses: actions/github-script@3a2844b7e9c422d3c10d287c895573f7108da1b3 # v9.0.0 - with: - script: | - const { setupGlobals } = require('${{ runner.temp }}/gh-aw/actions/setup_globals.cjs'); - setupGlobals(core, github, context, exec, io, getOctokit); - const { main } = require('${{ runner.temp }}/gh-aw/actions/awf_reflect_summary.cjs'); - await main(); - - name: Write agent output placeholder if missing - if: always() - run: | - if [ ! -f /tmp/gh-aw/agent_output.json ]; then - echo '{"items":[]}' > /tmp/gh-aw/agent_output.json - fi - - name: Upload agent artifacts - if: always() - continue-on-error: true - uses: actions/upload-artifact@043fb46d1a93c77aae656e7c1c64a875d1fc6a0a # v7.0.1 - with: - name: agent - path: | - /tmp/gh-aw/aw-prompts/prompt.txt - /tmp/gh-aw/sandbox/agent/logs/ - /tmp/gh-aw/redacted-urls.log - /tmp/gh-aw/mcp-logs/ - /tmp/gh-aw/agent_usage.json - /tmp/gh-aw/agent-stdio.log - /tmp/gh-aw/pre-agent-audit.txt - /tmp/gh-aw/agent/ - /tmp/gh-aw/github_rate_limits.jsonl - /tmp/gh-aw/safeoutputs.jsonl - /tmp/gh-aw/agent_output.json - /tmp/gh-aw/aw-*.patch - /tmp/gh-aw/aw-*.bundle - /tmp/gh-aw/awf-config.json - /tmp/gh-aw/sandbox/firewall/logs/ - /tmp/gh-aw/sandbox/firewall/audit/ - /tmp/gh-aw/sandbox/firewall/awf-reflect.json - if-no-files-found: ignore - - conclusion: - needs: - - activation - - agent - - detection - - publish_review_provenance - - resolve_sfl_review_thread - - review_metadata - - safe_outputs - if: > - always() && (needs.agent.result != 'skipped' || needs.activation.outputs.lockdown_check_failed == 'true' || - needs.activation.outputs.oauth_token_check_failed == 'true' || needs.activation.outputs.stale_lock_file_failed == 'true' || - needs.activation.outputs.daily_ai_credits_exceeded == 'true') - runs-on: ubuntu-slim - permissions: - actions: read - issues: write - pull-requests: write - concurrency: - group: "gh-aw-conclusion-sfl-pr-review" - cancel-in-progress: false - queue: max - env: - GH_AW_RUNTIME_FEATURES: ${{ vars.GH_AW_RUNTIME_FEATURES }} - outputs: - incomplete_count: ${{ steps.report_incomplete.outputs.incomplete_count }} - noop_message: ${{ steps.noop.outputs.noop_message }} - tools_reported: ${{ steps.missing_tool.outputs.tools_reported }} - total_count: ${{ steps.missing_tool.outputs.total_count }} - steps: - - name: Setup Scripts - id: setup - uses: github/gh-aw-actions/setup@6aab9e5b5c91c615506061f09bedd81a23babe3c # v0.86.2 - with: - destination: ${{ runner.temp }}/gh-aw/actions - job-name: ${{ github.job }} - trace-id: ${{ needs.activation.outputs.setup-trace-id }} - parent-span-id: ${{ needs.activation.outputs.setup-parent-span-id || needs.activation.outputs.setup-span-id }} - env: - GH_AW_SETUP_WORKFLOW_NAME: "SFL PR Review" - GH_AW_CURRENT_WORKFLOW_REF: ${{ github.repository }}/.github/workflows/sfl-pr-review.lock.yml@${{ github.ref }} - GH_AW_INFO_VERSION: "1.0.79" - GH_AW_INFO_AWF_VERSION: "v0.27.44" - GH_AW_INFO_BODY_MODIFIED: "false" - GH_AW_INFO_ENGINE_ID: "copilot" - - name: Generate GitHub App token - id: safe-outputs-app-token - uses: actions/create-github-app-token@bcd2ba49218906704ab6c1aa796996da409d3eb1 # v3.2.0 - with: - client-id: ${{ vars.SFL_APP_CLIENT_ID }} - private-key: ${{ secrets.SFL_APP_PRIVATE_KEY }} - owner: ${{ github.repository_owner }} - repositories: ${{ github.event.repository.name }} - github-api-url: ${{ github.api_url }} - permission-issues: write - permission-pull-requests: write - - name: Download agent output artifact - id: download-agent-output - continue-on-error: true - uses: actions/download-artifact@3e5f45b2cfb9172054b4087a40e8e0b5a5461e7c # v8.0.1 - with: - name: agent - path: /tmp/gh-aw/ - - name: Setup agent output environment variable - id: setup-agent-output-env - if: steps.download-agent-output.outcome == 'success' - run: | - mkdir -p /tmp/gh-aw/ - find "/tmp/gh-aw/" -type f -print - echo "GH_AW_AGENT_OUTPUT=/tmp/gh-aw/agent_output.json" >> "$GITHUB_OUTPUT" - - name: Download Safe Outputs Items Manifest - id: download-safe-outputs-manifest - if: always() - continue-on-error: true - uses: actions/download-artifact@3e5f45b2cfb9172054b4087a40e8e0b5a5461e7c # v8.0.1 - with: - name: safe-outputs-items - path: /tmp/gh-aw/ - - name: Collect usage artifact files - if: always() - continue-on-error: true - run: bash "${RUNNER_TEMP}/gh-aw/actions/collect_usage_artifact_files.sh" - - name: Upload usage artifact - if: always() - continue-on-error: true - uses: actions/upload-artifact@043fb46d1a93c77aae656e7c1c64a875d1fc6a0a # v7.0.1 - with: - name: usage - path: | - /tmp/gh-aw/usage/aw_info.json - /tmp/gh-aw/usage/aw-info.jsonl - /tmp/gh-aw/usage/agent_usage.json - /tmp/gh-aw/usage/agent_usage.jsonl - /tmp/gh-aw/usage/detection_usage.jsonl - /tmp/gh-aw/usage/evals.jsonl - /tmp/gh-aw/usage/github_rate_limits.jsonl - /tmp/gh-aw/usage/agent/token_usage.jsonl - /tmp/gh-aw/usage/detection/token_usage.jsonl - /tmp/gh-aw/usage/activity/summary.json - if-no-files-found: ignore - - name: Restore daily AIC usage cache - id: restore-daily-aic-cache-conclusion - if: always() - continue-on-error: true - uses: actions/cache/restore@55cc8345863c7cc4c66a329aec7e433d2d1c52a9 # v6.1.0 - with: - key: agentic-workflow-usage-sflprreview-${{ github.run_id }} - restore-keys: agentic-workflow-usage-sflprreview- - path: /tmp/gh-aw/agentic-workflow-usage-cache.jsonl - - name: Write daily AIC usage cache entry - id: write-daily-aic-cache - if: always() - continue-on-error: true - uses: actions/github-script@3a2844b7e9c422d3c10d287c895573f7108da1b3 # v9.0.0 - with: - github-token: ${{ github.token }} - script: | - const { setupGlobals } = require('${{ runner.temp }}/gh-aw/actions/setup_globals.cjs'); - setupGlobals(core, github, context); - const { main } = require('${{ runner.temp }}/gh-aw/actions/write_daily_aic_usage_cache.cjs'); - await main(); - - name: Save daily AIC usage cache - id: save-daily-aic-cache - if: always() - continue-on-error: true - uses: actions/cache/save@55cc8345863c7cc4c66a329aec7e433d2d1c52a9 # v6.1.0 - with: - key: agentic-workflow-usage-sflprreview-${{ github.run_id }} - path: /tmp/gh-aw/agentic-workflow-usage-cache.jsonl - - name: Upload daily AIC usage cache artifact - id: upload-daily-aic-cache - if: always() - continue-on-error: true - uses: actions/upload-artifact@043fb46d1a93c77aae656e7c1c64a875d1fc6a0a # v7.0.1 - with: - name: aic-usage-cache - path: /tmp/gh-aw/agentic-workflow-usage-cache.jsonl - if-no-files-found: ignore - retention-days: 7 - - name: Process no-op messages - id: noop - uses: actions/github-script@3a2844b7e9c422d3c10d287c895573f7108da1b3 # v9.0.0 - env: - GH_AW_AGENT_OUTPUT: ${{ steps.setup-agent-output-env.outputs.GH_AW_AGENT_OUTPUT }} - GH_AW_NOOP_MAX: "1" - GH_AW_WORKFLOW_NAME: "SFL PR Review" - GH_AW_WORKFLOW_SOURCE: "HemSoft/set-it-free-loop/deployment/workflows/sfl-pr-review.md@main" - GH_AW_WORKFLOW_SOURCE_URL: "${{ github.server_url }}/HemSoft/set-it-free-loop/blob/main/deployment/workflows/sfl-pr-review.md" - GH_AW_RUN_URL: ${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }} - GH_AW_AGENT_CONCLUSION: ${{ needs.agent.result }} - GH_AW_NOOP_REPORT_AS_ISSUE: "false" - GH_AW_AIC: ${{ needs.agent.outputs.aic }} - GH_AW_THREAT_DETECTION_AIC: ${{ needs.detection.outputs.aic }} - GH_AW_AMBIENT_CONTEXT: ${{ needs.agent.outputs.ambient_context }} - GH_AW_WORKFLOW_ID: "sfl-pr-review" - with: - github-token: ${{ steps.safe-outputs-app-token.outputs.token }} - script: | - const { setupGlobals } = require('${{ runner.temp }}/gh-aw/actions/setup_globals.cjs'); - setupGlobals(core, github, context, exec, io, getOctokit); - const { main } = require('${{ runner.temp }}/gh-aw/actions/handle_noop_message.cjs'); - await main(); - - name: Log detection run - id: detection_runs - uses: actions/github-script@3a2844b7e9c422d3c10d287c895573f7108da1b3 # v9.0.0 - env: - GH_AW_AGENT_OUTPUT: ${{ steps.setup-agent-output-env.outputs.GH_AW_AGENT_OUTPUT }} - GH_AW_WORKFLOW_NAME: "SFL PR Review" - GH_AW_WORKFLOW_SOURCE: "HemSoft/set-it-free-loop/deployment/workflows/sfl-pr-review.md@main" - GH_AW_WORKFLOW_SOURCE_URL: "${{ github.server_url }}/HemSoft/set-it-free-loop/blob/main/deployment/workflows/sfl-pr-review.md" - GH_AW_RUN_URL: ${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }} - GH_AW_DETECTION_CONCLUSION: ${{ needs.detection.outputs.detection_conclusion }} - GH_AW_DETECTION_REASON: ${{ needs.detection.outputs.detection_reason }} - with: - github-token: ${{ steps.safe-outputs-app-token.outputs.token }} - script: | - const { setupGlobals } = require('${{ runner.temp }}/gh-aw/actions/setup_globals.cjs'); - setupGlobals(core, github, context, exec, io, getOctokit); - const { main } = require('${{ runner.temp }}/gh-aw/actions/handle_detection_runs.cjs'); - await main(); - - name: Record missing tool - id: missing_tool - uses: actions/github-script@3a2844b7e9c422d3c10d287c895573f7108da1b3 # v9.0.0 - env: - GH_AW_AGENT_OUTPUT: ${{ steps.setup-agent-output-env.outputs.GH_AW_AGENT_OUTPUT }} - GH_AW_MISSING_TOOL_CREATE_ISSUE: "true" - GH_AW_WORKFLOW_NAME: "SFL PR Review" - GH_AW_WORKFLOW_SOURCE: "HemSoft/set-it-free-loop/deployment/workflows/sfl-pr-review.md@main" - GH_AW_WORKFLOW_SOURCE_URL: "${{ github.server_url }}/HemSoft/set-it-free-loop/blob/main/deployment/workflows/sfl-pr-review.md" - with: - github-token: ${{ steps.safe-outputs-app-token.outputs.token }} - script: | - const { setupGlobals } = require('${{ runner.temp }}/gh-aw/actions/setup_globals.cjs'); - setupGlobals(core, github, context, exec, io, getOctokit); - const { main } = require('${{ runner.temp }}/gh-aw/actions/missing_tool.cjs'); - await main(); - - name: Record incomplete - id: report_incomplete - uses: actions/github-script@3a2844b7e9c422d3c10d287c895573f7108da1b3 # v9.0.0 - env: - GH_AW_AGENT_OUTPUT: ${{ steps.setup-agent-output-env.outputs.GH_AW_AGENT_OUTPUT }} - GH_AW_REPORT_INCOMPLETE_CREATE_ISSUE: "true" - GH_AW_WORKFLOW_NAME: "SFL PR Review" - GH_AW_WORKFLOW_SOURCE: "HemSoft/set-it-free-loop/deployment/workflows/sfl-pr-review.md@main" - GH_AW_WORKFLOW_SOURCE_URL: "${{ github.server_url }}/HemSoft/set-it-free-loop/blob/main/deployment/workflows/sfl-pr-review.md" - with: - github-token: ${{ steps.safe-outputs-app-token.outputs.token }} - script: | - const { setupGlobals } = require('${{ runner.temp }}/gh-aw/actions/setup_globals.cjs'); - setupGlobals(core, github, context, exec, io, getOctokit); - const { main } = require('${{ runner.temp }}/gh-aw/actions/report_incomplete_handler.cjs'); - await main(); - - name: Handle agent failure - id: handle_agent_failure - if: always() - uses: actions/github-script@3a2844b7e9c422d3c10d287c895573f7108da1b3 # v9.0.0 - env: - GH_AW_AGENT_OUTPUT: ${{ steps.setup-agent-output-env.outputs.GH_AW_AGENT_OUTPUT }} - GH_AW_WORKFLOW_NAME: "SFL PR Review" - GH_AW_WORKFLOW_SOURCE: "HemSoft/set-it-free-loop/deployment/workflows/sfl-pr-review.md@main" - GH_AW_WORKFLOW_SOURCE_URL: "${{ github.server_url }}/HemSoft/set-it-free-loop/blob/main/deployment/workflows/sfl-pr-review.md" - GH_AW_RUN_URL: ${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }} - GH_AW_AGENT_CONCLUSION: ${{ needs.agent.result }} - GH_AW_WORKFLOW_ID: "sfl-pr-review" - GH_AW_ACTION_FAILURE_ISSUE_EXPIRES_HOURS: "168" - GH_AW_ENGINE_ID: "copilot" - GH_AW_CHECKOUT_PR_SUCCESS: ${{ needs.agent.outputs.checkout_pr_success }} - GH_AW_EFFECTIVE_TOKENS: ${{ needs.agent.outputs.effective_tokens || '' }} - GH_AW_AI_CREDITS_RATE_LIMIT_ERROR: ${{ needs.agent.outputs.ai_credits_rate_limit_error || 'false' }} - GH_AW_UNKNOWN_MODEL_AI_CREDITS: ${{ needs.agent.outputs.unknown_model_ai_credits || 'false' }} - GH_AW_AIC: ${{ needs.agent.outputs.aic }} - GH_AW_THREAT_DETECTION_AIC: ${{ needs.detection.outputs.aic }} - GH_AW_MAX_AI_CREDITS: ${{ vars.GH_AW_DEFAULT_MAX_AI_CREDITS || '1000' }} - GH_AW_INFERENCE_ACCESS_ERROR: ${{ needs.agent.outputs.inference_access_error }} - GH_AW_MCP_POLICY_ERROR: ${{ needs.agent.outputs.mcp_policy_error }} - GH_AW_AGENTIC_ENGINE_TIMEOUT: ${{ needs.agent.outputs.agentic_engine_timeout }} - GH_AW_MODEL_NOT_SUPPORTED_ERROR: ${{ needs.agent.outputs.model_not_supported_error }} - GH_AW_HTTP_400_RESPONSE_ERROR: ${{ needs.agent.outputs.http_400_response_error }} - GH_AW_MAX_CACHE_MISSES_EXCEEDED: ${{ needs.agent.outputs.max_cache_misses_exceeded }} - GH_AW_MISSING_MODEL_PRICING_ERROR: ${{ needs.agent.outputs.missing_model_pricing_error }} - GH_AW_MISSING_MODEL_PRICING_MODEL_NAME: ${{ needs.agent.outputs.missing_model_pricing_model_name }} - GH_AW_ENGINE_API_HOSTS: "api.enterprise.githubcopilot.com,api.githubcopilot.com,api.business.githubcopilot.com,api.individual.githubcopilot.com" - GH_AW_SAFE_OUTPUTS_APP_TOKEN_MINTING_FAILED: ${{ needs.safe_outputs.outputs.app_token_minting_failed }} - GH_AW_CONCLUSION_APP_TOKEN_MINTING_FAILED: ${{ steps.safe-outputs-app-token.outcome == 'failure' }} - GH_AW_LOCKDOWN_CHECK_FAILED: ${{ needs.activation.outputs.lockdown_check_failed }} - GH_AW_OAUTH_TOKEN_CHECK_FAILED: ${{ needs.activation.outputs.oauth_token_check_failed }} - GH_AW_STALE_LOCK_FILE_FAILED: ${{ needs.activation.outputs.stale_lock_file_failed }} - GH_AW_DAILY_AI_CREDITS_EXCEEDED: ${{ needs.activation.outputs.daily_ai_credits_exceeded }} - GH_AW_DAILY_AI_CREDITS_TOTAL_EFFECTIVE_TOKENS: ${{ needs.activation.outputs.daily_ai_credits_total_effective_tokens }} - GH_AW_DAILY_AI_CREDITS_THRESHOLD: ${{ needs.activation.outputs.daily_ai_credits_threshold }} - GH_AW_GROUP_REPORTS: "false" - GH_AW_FAILURE_REPORT_AS_ISSUE: "true" - GH_AW_MISSING_TOOL_REPORT_AS_FAILURE: "true" - GH_AW_MISSING_DATA_REPORT_AS_FAILURE: "true" - GH_AW_TIMEOUT_MINUTES: "20" - with: - github-token: ${{ steps.safe-outputs-app-token.outputs.token }} - script: | - const { setupGlobals } = require('${{ runner.temp }}/gh-aw/actions/setup_globals.cjs'); - setupGlobals(core, github, context, exec, io, getOctokit); - const { main } = require('${{ runner.temp }}/gh-aw/actions/handle_agent_failure.cjs'); - await main(); - - name: Report failed jobs - id: report_failed_jobs - if: always() - uses: actions/github-script@3a2844b7e9c422d3c10d287c895573f7108da1b3 # v9.0.0 - env: - GH_AW_AGENT_OUTPUT: ${{ steps.setup-agent-output-env.outputs.GH_AW_AGENT_OUTPUT }} - GH_AW_WORKFLOW_NAME: "SFL PR Review" - GH_AW_WORKFLOW_SOURCE: "HemSoft/set-it-free-loop/deployment/workflows/sfl-pr-review.md@main" - GH_AW_WORKFLOW_SOURCE_URL: "${{ github.server_url }}/HemSoft/set-it-free-loop/blob/main/deployment/workflows/sfl-pr-review.md" - GH_AW_RUN_URL: ${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }} - GH_AW_REPORT_FAILED_JOBS: "true" - with: - github-token: ${{ steps.safe-outputs-app-token.outputs.token }} - script: | - const { setupGlobals } = require('${{ runner.temp }}/gh-aw/actions/setup_globals.cjs'); - setupGlobals(core, github, context, exec, io, getOctokit); - const { main } = require('${{ runner.temp }}/gh-aw/actions/report_failed_jobs.cjs'); - await main(); - - detection: - needs: - - activation - - agent - if: always() && needs.agent.result != 'skipped' - runs-on: ubuntu-latest - permissions: - contents: read - env: - GH_AW_RUNTIME_FEATURES: ${{ vars.GH_AW_RUNTIME_FEATURES }} - outputs: - aic: ${{ steps.parse_detection_token_usage.outputs.aic }} - detection_conclusion: ${{ steps.detection_conclusion.outputs.conclusion }} - detection_reason: ${{ steps.detection_conclusion.outputs.reason }} - detection_success: ${{ steps.detection_conclusion.outputs.success }} - steps: - - name: Setup Scripts - id: setup - uses: github/gh-aw-actions/setup@6aab9e5b5c91c615506061f09bedd81a23babe3c # v0.86.2 - with: - destination: ${{ runner.temp }}/gh-aw/actions - job-name: ${{ github.job }} - trace-id: ${{ needs.activation.outputs.setup-trace-id }} - parent-span-id: ${{ needs.activation.outputs.setup-parent-span-id || needs.activation.outputs.setup-span-id }} - env: - GH_AW_SETUP_WORKFLOW_NAME: "SFL PR Review" - GH_AW_CURRENT_WORKFLOW_REF: ${{ github.repository }}/.github/workflows/sfl-pr-review.lock.yml@${{ github.ref }} - GH_AW_INFO_VERSION: "1.0.79" - GH_AW_INFO_AWF_VERSION: "v0.27.44" - GH_AW_INFO_BODY_MODIFIED: "false" - GH_AW_INFO_ENGINE_ID: "copilot" - - name: Download agent output artifact - id: download-agent-output - continue-on-error: true - uses: actions/download-artifact@3e5f45b2cfb9172054b4087a40e8e0b5a5461e7c # v8.0.1 - with: - name: agent - path: /tmp/gh-aw/ - - name: Setup agent output environment variable - id: setup-agent-output-env - if: steps.download-agent-output.outcome == 'success' - run: | - mkdir -p /tmp/gh-aw/ - find "/tmp/gh-aw/" -type f -print - echo "GH_AW_AGENT_OUTPUT=/tmp/gh-aw/agent_output.json" >> "$GITHUB_OUTPUT" - - name: Checkout repository for patch context - if: needs.agent.outputs.has_patch == 'true' - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 - with: - persist-credentials: false - # --- Threat Detection --- - - name: Clean stale firewall files from agent artifact - run: | - rm -rf /tmp/gh-aw/sandbox/firewall/logs - rm -rf /tmp/gh-aw/sandbox/firewall/audit - - name: Download container images - run: bash "${RUNNER_TEMP}/gh-aw/actions/download_docker_images.sh" ghcr.io/github/gh-aw-firewall/agent:0.27.44@sha256:0d727725c737b58c7bdf51f640cffb928385ec46517e0917c7f1a02f1bada8b4 ghcr.io/github/gh-aw-firewall/api-proxy:0.27.44@sha256:b50fbadba138f6e9aba94aca09711335c489bb3b15861220cb66f6092e042dc7 ghcr.io/github/gh-aw-firewall/squid:0.27.44@sha256:83e48bbe12c634be8c228a576832fe45f66c529ac3659db92bddbcf2eeb6d627 - - name: Check if detection needed - id: detection_guard - if: always() - env: - OUTPUT_TYPES: ${{ needs.agent.outputs.output_types }} - HAS_PATCH: ${{ needs.agent.outputs.has_patch }} - run: | - if [[ -n "$OUTPUT_TYPES" || "$HAS_PATCH" == "true" ]]; then - echo "run_detection=true" >> "$GITHUB_OUTPUT" - echo "Detection will run: output_types=$OUTPUT_TYPES, has_patch=$HAS_PATCH" - else - echo "run_detection=false" >> "$GITHUB_OUTPUT" - echo "Detection skipped: no agent outputs or patches to analyze" - fi - - name: Clear MCP Config for detection - if: always() && steps.detection_guard.outputs.run_detection == 'true' - run: | - rm -f "${RUNNER_TEMP}/gh-aw/mcp-config/mcp-servers.json" - rm -f "$HOME/.copilot/mcp-config.json" - rm -f "$GITHUB_WORKSPACE/.gemini/settings.json" - - name: Prepare threat detection files - if: always() && steps.detection_guard.outputs.run_detection == 'true' - run: | - bash "${RUNNER_TEMP}/gh-aw/actions/prepare_threat_detection_files.sh" - - name: Install threat-detection ripgrep with bounded diagnostics - id: sfl-threat-detection-ripgrep - if: always() - run: | - set -euo pipefail - log_path="${RUNNER_TEMP}/sfl-threat-detection-ripgrep-install.log" - : > "$log_path" - if command -v rg > /dev/null 2>&1; then - rg --version | tee -a "$log_path" - exit 0 - fi - - set +e - timeout --signal=TERM --kill-after=15s 180s bash -c ' - set -euo pipefail - sudo apt-get update \ - -o Acquire::Retries=3 \ - -o Acquire::http::Timeout=30 \ - -o Acquire::https::Timeout=30 - sudo apt-get install --no-install-recommends -y ripgrep - ' 2>&1 | tee -a "$log_path" - install_status=${PIPESTATUS[0]} - set -e - - if [ "$install_status" -eq 124 ] || [ "$install_status" -eq 137 ]; then - echo "::error::threat-detection ripgrep installation exceeded the 180-second setup limit" - exit 1 - fi - if [ "$install_status" -ne 0 ]; then - echo "::error::threat-detection ripgrep installation failed with exit code ${install_status}" - exit "$install_status" - fi - if ! command -v rg > /dev/null 2>&1; then - echo "::error::threat-detection ripgrep installation completed without making rg available" - exit 1 - fi - rg --version | tee -a "$log_path" - shell: bash - - name: Retain threat-detection ripgrep setup diagnostics - if: always() - uses: actions/upload-artifact@043fb46d1a93c77aae656e7c1c64a875d1fc6a0a - with: - if-no-files-found: ignore - name: sfl-threat-detection-ripgrep-setup-${{ github.run_id }}-${{ github.run_attempt }} - path: ${{ runner.temp }}/sfl-threat-detection-ripgrep-install.log - retention-days: 1 - - name: Setup threat detection - if: always() && steps.detection_guard.outputs.run_detection == 'true' - uses: actions/github-script@3a2844b7e9c422d3c10d287c895573f7108da1b3 # v9.0.0 - env: - WORKFLOW_NAME: "SFL PR Review" - WORKFLOW_DESCRIPTION: "Full-spectrum PR review dispatched automatically by the SFL PR review wrapper\nor manually through workflow_dispatch. Performs 3 evidence-based review\npasses (Security, Accuracy & Reliability, Quality & Maintainability) and\nposts inline review comments on specific lines with an overall summary." - HAS_PATCH: ${{ needs.agent.outputs.has_patch }} - GH_AW_DETECTION_CONTINUE_ON_ERROR: "false" - with: - script: | - const { setupGlobals } = require('${{ runner.temp }}/gh-aw/actions/setup_globals.cjs'); - setupGlobals(core, github, context, exec, io, getOctokit); - const { main } = require('${{ runner.temp }}/gh-aw/actions/setup_threat_detection.cjs'); - await main(); - - name: Ensure threat-detection directory and log - if: always() && steps.detection_guard.outputs.run_detection == 'true' - run: | - mkdir -p /tmp/gh-aw/threat-detection - touch /tmp/gh-aw/threat-detection/detection.log - rm -f /tmp/gh-aw/step-summary.md - touch /tmp/gh-aw/step-summary.md - - name: Setup Node.js - uses: actions/setup-node@820762786026740c76f36085b0efc47a31fe5020 # v7.0.0 - with: - node-version: '24' - package-manager-cache: false - - name: Install ripgrep - run: bash "${RUNNER_TEMP}/gh-aw/actions/install_ripgrep.sh" - - name: Install GitHub Copilot CLI - run: bash "${RUNNER_TEMP}/gh-aw/actions/install_copilot_cli.sh" - env: - GH_HOST: github.com - GH_AW_COMPILED_VERSION: v0.86.2 - - name: Install AWF binary - run: bash "${RUNNER_TEMP}/gh-aw/actions/install_awf_binary.sh" v0.27.44 - - name: Execute GitHub Copilot CLI - if: always() && steps.detection_guard.outputs.run_detection == 'true' - continue-on-error: true - id: detection_agentic_execution - # Copilot CLI tool arguments (sorted): - timeout-minutes: 20 - run: | - set -o pipefail - printf '%s' "$(date +%s%3N)" > /tmp/gh-aw/agent_cli_start_ms.txt - trap 'gh_aw_exit_code=$?; mkdir -p /tmp/gh-aw >/dev/null 2>&1 || true; printf "%s" "$gh_aw_exit_code" > /tmp/gh-aw/agent_execution_exit_code.txt || true; rm -f "$HOME/.copilot/settings.json"' EXIT - mkdir -p "$HOME/.copilot" - printf '%s' '{"builtInAgents":{"rubberDuck":false}}' > "$HOME/.copilot/settings.json" - export XDG_CONFIG_HOME="$HOME" - GH_AW_COPILOT_SRC="$(command -v copilot 2>/dev/null || true)" - if [ -z "$GH_AW_COPILOT_SRC" ] || [ ! -x "$GH_AW_COPILOT_SRC" ]; then - echo "GitHub Copilot CLI executable not found on PATH after installation" >&2 - exit 127 - fi - GH_AW_COPILOT_BIN="${RUNNER_TEMP}/gh-aw/bin/copilot" - mkdir -p "${RUNNER_TEMP}/gh-aw/bin" - if [ "$GH_AW_COPILOT_SRC" != "$GH_AW_COPILOT_BIN" ]; then - cp "$GH_AW_COPILOT_SRC" "$GH_AW_COPILOT_BIN" - fi - chmod 755 "$GH_AW_COPILOT_BIN" - - touch /tmp/gh-aw/agent-step-summary.md - GH_AW_NODE_BIN=$(command -v node 2>/dev/null || true) - export GH_AW_NODE_BIN - export COPILOT_API_KEY="$COPILOT_DUMMY_BYOK" - (umask 177 && touch /tmp/gh-aw/threat-detection/detection.log) - GH_AW_MAX_AI_CREDITS="${GH_AW_MAX_AI_CREDITS:-400}" - printf '%s\n' "{\"\$schema\":\"https://github.com/github/gh-aw-firewall/releases/download/v0.27.44/awf-config.schema.json\",\"network\":{\"allowDomains\":[\"api.business.githubcopilot.com\",\"api.enterprise.githubcopilot.com\",\"api.github.com\",\"api.githubcopilot.com\",\"api.individual.githubcopilot.com\",\"github.com\",\"host.docker.internal\",\"openrouter.ai\",\"registry.npmjs.org\",\"telemetry.enterprise.githubcopilot.com\"]},\"apiProxy\":{\"enabled\":true,\"enableTokenSteering\":true,\"maxRuns\":500,\"maxAiCredits\":${GH_AW_MAX_AI_CREDITS},\"maxCacheMisses\":5,\"targets\":{\"copilot\":{\"host\":\"openrouter.ai\"}},\"providers\":{\"github-copilot\":{\"models\":{\"moonshotai/kimi-k3\":{\"cost\":{\"input\":\"3e-06\",\"output\":\"1.5e-05\"}}}}},\"models\":{\"agent\":[\"sonnet-6x\",\"gpt-5.4\",\"gpt-5.5\",\"gpt-5.6\",\"gpt-5.3\",\"gemini-pro\",\"any\"],\"antigravity\":[\"copilot/antigravity*\",\"google/antigravity*\",\"gemini/antigravity*\"],\"any\":[\"copilot/*\",\"anthropic/*\",\"openai/*\",\"google/*\",\"gemini/*\"],\"auto\":[\"copilot/auto\",\"large\"],\"claude\":[\"agent\"],\"codex\":[\"agent\"],\"coding\":[\"copilot/gpt-5*codex*\",\"openai/gpt-5*codex*\",\"gpt-5-codex\",\"kimi\"],\"computer-use\":[\"copilot/*computer-use*\",\"google/*computer-use*\",\"gemini/*computer-use*\",\"openai/*computer-use*\"],\"copilot\":[\"agent\"],\"deep-research\":[\"copilot/deep-research*\",\"copilot/o3-deep-research*\",\"copilot/o4-mini-deep-research*\",\"google/deep-research*\",\"gemini/deep-research*\",\"openai/o3-deep-research*\",\"openai/o4-mini-deep-research*\"],\"detection\":[\"small\"],\"evals\":[\"small\"],\"fable\":[\"copilot/*fable*\",\"anthropic/*fable*\"],\"gemini\":[\"agent\"],\"gemini-3-flash\":[\"copilot/gemini-3*flash*\",\"google/gemini-3*flash*\",\"gemini/gemini-3*flash*\"],\"gemini-3-pro\":[\"copilot/gemini-3*pro*\",\"google/gemini-3*pro*\",\"google/nano-banana*\",\"gemini/gemini-3*pro*\"],\"gemini-3.1-flash\":[\"copilot/gemini-3.1*flash*\",\"google/gemini-3.1*flash*\",\"gemini/gemini-3.1*flash*\"],\"gemini-3.1-pro\":[\"copilot/gemini-3.1*pro*\",\"google/gemini-3.1*pro*\",\"gemini/gemini-3.1*pro*\"],\"gemini-3.5-flash\":[\"copilot/gemini-3.5*flash*\",\"google/gemini-3.5*flash*\",\"gemini/gemini-3.5*flash*\"],\"gemini-3.6-flash\":[\"copilot/gemini-3.6*flash*\",\"google/gemini-3.6*flash*\",\"gemini/gemini-3.6*flash*\"],\"gemini-flash\":[\"copilot/gemini-*flash*\",\"google/gemini-*flash*\",\"gemini/gemini-*flash*\"],\"gemini-flash-lite\":[\"copilot/gemini-*flash*lite*\",\"google/gemini-*flash*lite*\",\"gemini/gemini-*flash*lite*\"],\"gemini-omni\":[\"copilot/gemini-omni*\",\"google/gemini-omni*\",\"gemini/gemini-omni*\"],\"gemini-pro\":[\"copilot/gemini-*pro*\",\"google/gemini-*pro*\",\"gemini/gemini-*pro*\"],\"gemma\":[\"copilot/gemma*\",\"google/gemma*\",\"gemini/gemma*\"],\"gpt-5\":[\"copilot/gpt-5*\",\"openai/gpt-5*\"],\"gpt-5-codex\":[\"copilot/gpt-5*codex*\",\"openai/gpt-5*codex*\"],\"gpt-5-mini\":[\"copilot/gpt-5*mini*\",\"openai/gpt-5*mini*\"],\"gpt-5-nano\":[\"copilot/gpt-5*nano*\",\"openai/gpt-5*nano*\"],\"gpt-5-pro\":[\"copilot/gpt-5*pro*\",\"openai/gpt-5*pro*\"],\"gpt-5.1\":[\"copilot/gpt-5.1*\",\"openai/gpt-5.1*\"],\"gpt-5.2\":[\"copilot/gpt-5.2*\",\"openai/gpt-5.2*\"],\"gpt-5.3\":[\"copilot/gpt-5.3*\",\"openai/gpt-5.3*\"],\"gpt-5.4\":[\"copilot/gpt-5.4*\",\"openai/gpt-5.4*\"],\"gpt-5.5\":[\"copilot/gpt-5.5*\",\"openai/gpt-5.5*\"],\"gpt-5.6\":[\"copilot/gpt-5.6*\",\"openai/gpt-5.6*\"],\"grok\":[\"copilot/*grok*\",\"openai/*grok*\"],\"haiku\":[\"copilot/*haiku*\",\"anthropic/*haiku*\"],\"image-generation\":[\"copilot/gpt-image*\",\"openai/gpt-image*\",\"openai/chatgpt-image*\",\"copilot/gemini-*image*\",\"google/gemini-*image*\",\"gemini/gemini-*image*\",\"google/imagen*\"],\"kimi\":[\"copilot/kimi*\",\"openai/kimi*\"],\"kiwi\":[\"copilot/kiwi*\",\"openai/kiwi*\"],\"large\":[\"sonnet\",\"gpt-5-pro\",\"gpt-5\",\"gemini-pro\"],\"lyria\":[\"google/lyria*\",\"gemini/lyria*\",\"copilot/lyria*\"],\"mai-code\":[\"copilot/MAI-Code*\",\"copilot/mai-code*\",\"openai/MAI-Code*\"],\"mai-code-1-flash-picker\":[\"copilot/MAI-Code-1-Flash-picker*\",\"copilot/mai-code-1-flash-picker*\",\"openai/MAI-Code-1-Flash-picker*\"],\"mini\":[\"haiku\",\"gpt-5-mini\",\"gpt-5-nano\",\"gemini-flash-lite\"],\"nano-banana\":[\"copilot/nano-banana*\",\"google/nano-banana*\",\"gemini/nano-banana*\"],\"opus\":[\"copilot/*opus*\",\"anthropic/*opus*\"],\"opusplan\":[\"opus?effort=high\"],\"raptor-mini\":[\"copilot/raptor*\",\"openai/raptor*\"],\"reasoning\":[\"copilot/o1*\",\"copilot/o3*\",\"copilot/o4*\",\"openai/o1*\",\"openai/o3*\",\"openai/o4*\"],\"robotics\":[\"copilot/*robotics*\",\"google/*robotics*\",\"gemini/*robotics*\"],\"small\":[\"mini\"],\"small-agent\":[\"haiku\",\"gpt-5-mini\",\"gemini-flash\"],\"sonnet\":[\"copilot/*sonnet*\",\"anthropic/*sonnet*\"],\"sonnet-6x\":[\"copilot/*sonnet-4.5*\",\"copilot/*sonnet-4.6*\",\"copilot/*sonnet-5*\",\"copilot/*sonnet-4-5-*\",\"anthropic/*sonnet-4-5-*\",\"copilot/*sonnet-4-6*\",\"anthropic/*sonnet-4-6*\",\"anthropic/*sonnet-5*\"],\"summarization\":[\"haiku\",\"gpt-5-mini\",\"gemini-flash-lite\",\"mini\"],\"veo\":[\"google/veo*\",\"gemini/veo*\"],\"vision\":[\"copilot/gemini-*image*\",\"google/gemini-*image*\",\"gemini/gemini-*image*\",\"copilot/gemini-*flash*\",\"google/gemini-*flash*\",\"gemini/gemini-*flash*\"]}},\"container\":{\"imageTag\":\"0.27.44,squid=sha256:83e48bbe12c634be8c228a576832fe45f66c529ac3659db92bddbcf2eeb6d627,agent=sha256:0d727725c737b58c7bdf51f640cffb928385ec46517e0917c7f1a02f1bada8b4,api-proxy=sha256:b50fbadba138f6e9aba94aca09711335c489bb3b15861220cb66f6092e042dc7,cli-proxy=sha256:c064d15974f7c933ec7d3f7b4038f4fd203547b3154bdc821afd379144887eff\"},\"logging\":{\"proxyLogsDir\":\"/tmp/gh-aw/sandbox/firewall/logs\",\"auditDir\":\"/tmp/gh-aw/sandbox/firewall/audit\"}}" > "${RUNNER_TEMP}/gh-aw/awf-config.json" - cp "${RUNNER_TEMP}/gh-aw/awf-config.json" /tmp/gh-aw/awf-config.json - export GH_AW_MODELS_JSON_PATH="/tmp/gh-aw/models.json" - GH_AW_DOCKER_HOST="" - if [[ "${DOCKER_HOST:-}" =~ ^tcp:// ]]; then - GH_AW_DOCKER_HOST="${DOCKER_HOST}" - fi - if [[ "${DOCKER_HOST:-}" =~ ^tcp:// ]]; then - _GH_AW_CHROOT_JSON=$(jq -c --arg src "${RUNNER_TEMP}/gh-aw" --arg user "$(id -un)" --argjson uid "$(id -u)" --argjson gid "$(id -g)" --arg home "${RUNNER_TEMP}/gh-aw/home" '.chroot={"binariesSourcePath":$src,"identity":{"user":$user,"uid":$uid,"gid":$gid,"home":$home}}' "${RUNNER_TEMP}/gh-aw/awf-config.json") || { echo "chroot config patch failed" >&2; exit 1; } - printf '%s\n' "$_GH_AW_CHROOT_JSON" > "${RUNNER_TEMP}/gh-aw/awf-config.json" - printf '%s\n' "$_GH_AW_CHROOT_JSON" > "${RUNNER_TEMP}/gh-aw/awf-config.json" - fi - GH_AW_TOOL_CACHE_MOUNT="" - GH_AW_TOOL_CACHE="${RUNNER_TOOL_CACHE:?RUNNER_TOOL_CACHE must be set}" - if [ -d "$GH_AW_TOOL_CACHE" ]; then - if [[ "$GH_AW_TOOL_CACHE" != /opt/* ]]; then - GH_AW_TOOL_CACHE_MOUNT="$GH_AW_TOOL_CACHE:$GH_AW_TOOL_CACHE:ro" - fi - fi - # shellcheck disable=SC1003,SC2016,SC2086 - awf --config "${RUNNER_TEMP}/gh-aw/awf-config.json" --container-workdir "${GITHUB_WORKSPACE}" --mount "${RUNNER_TEMP}/gh-aw:${RUNNER_TEMP}/gh-aw:ro" --mount "${RUNNER_TEMP}/gh-aw:/host${RUNNER_TEMP}/gh-aw:ro" ${GH_AW_TOOL_CACHE_MOUNT:+--mount "$GH_AW_TOOL_CACHE_MOUNT"} ${GH_AW_DOCKER_HOST:+--docker-host "$GH_AW_DOCKER_HOST"} --env-all --exclude-env ACTIONS_ID_TOKEN_REQUEST_TOKEN --exclude-env ACTIONS_ID_TOKEN_REQUEST_URL --exclude-env COPILOT_PROVIDER_API_KEY --mount /tmp/gh-aw:/tmp/gh-aw:rw --log-level info --skip-pull \ - -- /bin/bash -c 'set +o histexpand; : "${RUNNER_TOOL_CACHE:?RUNNER_TOOL_CACHE must be set}"; GH_AW_TOOL_CACHE="$RUNNER_TOOL_CACHE"; export PATH="$(find "$GH_AW_TOOL_CACHE" -maxdepth 5 -type d -name bin 2>/dev/null | tr '\''\n'\'' '\'':'\'')$PATH"; [ -n "$GOROOT" ] && export PATH="$GOROOT/bin:$PATH" || true; [ -n "$ERLANG_HOME" ] && export PATH="$ERLANG_HOME/bin:$PATH" || true && GH_AW_NODE_EXEC="${GH_AW_NODE_BIN:-}"; if [ -z "$GH_AW_NODE_EXEC" ] || [ ! -x "$GH_AW_NODE_EXEC" ]; then GH_AW_NODE_EXEC="$(command -v node 2>/dev/null || true)"; fi; if [ -z "$GH_AW_NODE_EXEC" ]; then echo "node runtime missing on this runner — check runtimes.node in workflow YAML" >&2; exit 127; fi; GH_AW_NPM_GLOBAL_ROOT="$(npm root -g 2>/dev/null || true)"; if [ -n "$GH_AW_NPM_GLOBAL_ROOT" ]; then export NODE_PATH="${GH_AW_NPM_GLOBAL_ROOT}${NODE_PATH:+:${NODE_PATH}}"; fi; "$GH_AW_NODE_EXEC" "${RUNNER_TEMP}/gh-aw/actions/copilot_harness.cjs" "${RUNNER_TEMP}/gh-aw/bin/copilot" --add-dir /tmp/gh-aw/ --log-level all --log-dir /tmp/gh-aw/sandbox/agent/logs/ --disable-builtin-mcps --no-ask-user --allow-all-tools --add-dir "${GITHUB_WORKSPACE}" --prompt-file /tmp/gh-aw/aw-prompts/prompt.txt' 2>&1 | tee -a /tmp/gh-aw/threat-detection/detection.log - env: - GITHUB_STEP_SUMMARY: /tmp/gh-aw/step-summary.md - AWF_REFLECT_ENABLED: 1 - COPILOT_AGENT_RUNNER_TYPE: STANDALONE - COPILOT_DUMMY_BYOK: dummy-byok-key-for-offline-mode - COPILOT_MODEL: moonshotai/kimi-k3 - COPILOT_PROVIDER_API_KEY: ${{ secrets.OPENROUTER_API_KEY }} - COPILOT_PROVIDER_BASE_URL: https://openrouter.ai/api/v1 - COPILOT_PROVIDER_TYPE: openai - COPILOT_PROVIDER_WIRE_API: responses - GH_AW_LLM_PROVIDER: github - GH_AW_MAX_AI_CREDITS: ${{ vars.GH_AW_DEFAULT_DETECTION_MAX_AI_CREDITS || '400' }} - GH_AW_MAX_TURNS: ${{ vars.GH_AW_DEFAULT_MAX_TURNS || '' }} - GH_AW_PHASE: detection - GH_AW_PROMPT: /tmp/gh-aw/aw-prompts/prompt.txt - GH_AW_TIMEOUT_MINUTES: 20 - GH_AW_VERSION: v0.86.2 - GITHUB_API_URL: ${{ github.api_url }} - GITHUB_AW: true - GITHUB_COPILOT_INTEGRATION_ID: agentic-workflows - GITHUB_HEAD_REF: ${{ github.head_ref }} - GITHUB_REF_NAME: ${{ github.ref_name }} - GITHUB_SERVER_URL: ${{ github.server_url }} - GITHUB_WORKSPACE: ${{ github.workspace }} - GIT_AUTHOR_EMAIL: github-actions[bot]@users.noreply.github.com - GIT_AUTHOR_NAME: github-actions[bot] - GIT_COMMITTER_EMAIL: github-actions[bot]@users.noreply.github.com - GIT_COMMITTER_NAME: github-actions[bot] - RUNNER_TEMP: ${{ runner.temp }} - TRACEPARENT: ${{ env.GITHUB_AW_OTEL_TRACE_ID != '' && env.GITHUB_AW_OTEL_PARENT_SPAN_ID != '' && format('00-{0}-{1}-01', env.GITHUB_AW_OTEL_TRACE_ID, env.GITHUB_AW_OTEL_PARENT_SPAN_ID) || '' }} - - name: Echo detection step summary - if: always() && steps.detection_guard.outputs.run_detection == 'true' - continue-on-error: true - run: | - if [ -s /tmp/gh-aw/step-summary.md ]; then - cat /tmp/gh-aw/step-summary.md - fi - - name: Render detection log - if: always() && steps.detection_guard.outputs.run_detection == 'true' - continue-on-error: true - uses: actions/github-script@3a2844b7e9c422d3c10d287c895573f7108da1b3 # v9.0.0 - with: - script: | - const { setupGlobals } = require('${{ runner.temp }}/gh-aw/actions/setup_globals.cjs'); - setupGlobals(core, github, context, exec, io, getOctokit); - const { main } = require('${{ runner.temp }}/gh-aw/actions/render_detection_log.cjs'); - await main(); - - name: Generate SFL App token for final head verification - id: sfl-final-head-token - if: always() && steps.detection_guard.outputs.run_detection == 'true' - uses: actions/create-github-app-token@bcd2ba49218906704ab6c1aa796996da409d3eb1 - with: - client-id: ${{ vars.SFL_APP_CLIENT_ID }} - owner: ${{ github.repository_owner }} - permission-pull-requests: read - private-key: ${{ secrets.SFL_APP_PRIVATE_KEY }} - repositories: ${{ github.event.repository.name }} - - name: Verify pull request base and head before safe outputs - if: always() && steps.detection_guard.outputs.run_detection == 'true' - run: | - set -euo pipefail - if [[ ! "${EXPECTED_HEAD}" =~ ^[0-9a-f]{40}$ ]]; then - echo "::error::Invalid expected pull request head: ${EXPECTED_HEAD}" - exit 1 - fi - if [[ ! "${EXPECTED_BASE}" =~ ^[0-9a-f]{40}$ ]]; then - echo "::error::Invalid expected pull request base: ${EXPECTED_BASE}" - exit 1 - fi - pull_request="$(gh api "repos/${REPOSITORY}/pulls/${PR_NUMBER}")" - live_head="$(jq -r '.head.sha' <<< "$pull_request")" - live_base="$(jq -r '.base.sha' <<< "$pull_request")" - if [[ "${live_head}" != "${EXPECTED_HEAD}" ]]; then - echo "::error::Pull request head changed from ${EXPECTED_HEAD} to ${live_head}; suppressing stale SFL outputs" - exit 1 - fi - if [[ "${live_base}" != "${EXPECTED_BASE}" ]]; then - echo "::error::Pull request base changed from ${EXPECTED_BASE} to ${live_base}; suppressing stale SFL outputs" - exit 1 - fi - env: - EXPECTED_BASE: ${{ inputs.base_sha }} - EXPECTED_HEAD: ${{ inputs.head_sha }} - GH_TOKEN: ${{ steps.sfl-final-head-token.outputs.token }} - PR_NUMBER: ${{ inputs.item_number }} - REPOSITORY: ${{ github.repository }} - - name: Parse threat detection token usage for step summary - id: parse_detection_token_usage - if: always() - continue-on-error: true - uses: actions/github-script@3a2844b7e9c422d3c10d287c895573f7108da1b3 # v9.0.0 - env: - GH_AW_TOKEN_USAGE_SUMMARY_TITLE: Threat Detection Token Usage - with: - script: | - const { setupGlobals } = require('${{ runner.temp }}/gh-aw/actions/setup_globals.cjs'); - setupGlobals(core, github, context, exec, io, getOctokit); - const { main } = require('${{ runner.temp }}/gh-aw/actions/parse_token_usage.cjs'); - await main(); - - name: Upload threat detection log - if: always() && steps.detection_guard.outputs.run_detection == 'true' - uses: actions/upload-artifact@043fb46d1a93c77aae656e7c1c64a875d1fc6a0a # v7.0.1 - with: - name: detection - path: /tmp/gh-aw/threat-detection/detection.log - if-no-files-found: ignore - - name: Parse and conclude threat detection - id: detection_conclusion - if: always() - uses: actions/github-script@3a2844b7e9c422d3c10d287c895573f7108da1b3 # v9.0.0 - env: - RUN_DETECTION: ${{ steps.detection_guard.outputs.run_detection }} - DETECTION_AGENTIC_EXECUTION_OUTCOME: ${{ steps.detection_agentic_execution.outcome }} - GH_AW_DETECTION_CONTINUE_ON_ERROR: "false" - with: - script: | - try { - const { setupGlobals } = require('${{ runner.temp }}/gh-aw/actions/setup_globals.cjs'); - setupGlobals(core, github, context, exec, io, getOctokit); - const { main } = require('${{ runner.temp }}/gh-aw/actions/parse_threat_detection_results.cjs'); - await main(); - } catch (loadErr) { - const continueOnError = process.env.GH_AW_DETECTION_CONTINUE_ON_ERROR !== 'false'; - const detectionExecutionFailed = process.env.DETECTION_AGENTIC_EXECUTION_OUTCOME === 'failure'; - const msg = 'ERR_SYSTEM: \u274C Unexpected error loading threat detection module: ' + (loadErr && loadErr.message ? loadErr.message : String(loadErr)); - core.error(msg); - core.setOutput('reason', 'parse_error'); - if (continueOnError && !detectionExecutionFailed) { - core.warning('\u26A0\uFE0F ' + msg); - core.setOutput('conclusion', 'warning'); - core.setOutput('success', 'false'); - } else { - core.setOutput('conclusion', 'failure'); - core.setOutput('success', 'false'); - core.setFailed(msg); - } - } - - publish_review_provenance: - needs: activation - if: always() - runs-on: ubuntu-slim - permissions: - checks: write - contents: read - pull-requests: read - outputs: - evidence-check-run-id: ${{ steps.initialize-evidence.outputs.check-run-id }} - steps: - - name: Configure GH_HOST for enterprise compatibility - id: ghes-host-config - shell: bash - run: | # zizmor: ignore[github-env] - GITHUB_SERVER_URL is set by GitHub Actions, not user input. - # Derive GH_HOST from GITHUB_SERVER_URL so the gh CLI targets the correct - # GitHub instance (GHES/GHEC). On github.com this is a harmless no-op. - GH_HOST="${GITHUB_SERVER_URL#https://}" - GH_HOST="${GH_HOST#http://}" - echo "GH_HOST=${GH_HOST}" >> "$GITHUB_ENV" - - name: Write immutable review dispatch provenance - run: | - set -euo pipefail - if ! [[ "$PR_NUMBER" =~ ^[1-9][0-9]*$ ]]; then - echo "::error::Invalid pull request number: ${PR_NUMBER}" - exit 1 - fi - if ! [[ "$HEAD_SHA" =~ ^[0-9a-f]{40}$ ]]; then - echo "::error::Invalid expected pull request head: ${HEAD_SHA}" - exit 1 - fi - if ! [[ "$BASE_SHA" =~ ^[0-9a-f]{40}$ ]]; then - echo "::error::Invalid expected pull request base: ${BASE_SHA}" - exit 1 - fi - if ! [[ "$RETRY_COUNT" =~ ^[01]$ ]]; then - echo "::error::Invalid review retry count: ${RETRY_COUNT}" - exit 1 - fi - case "$REVIEW_EFFORT" in - low|medium|high) ;; - *) - echo "::error::Invalid review effort: ${REVIEW_EFFORT}" - exit 1 - ;; - esac - jq -n \ - --arg repository "$REPOSITORY" \ - --arg workflow_run_id "$RUN_ID" \ - --argjson pr_number "$PR_NUMBER" \ - --arg base_sha "$BASE_SHA" \ - --arg head_sha "$HEAD_SHA" \ - --arg review_effort "$REVIEW_EFFORT" \ - --argjson retry_count "$RETRY_COUNT" \ - '{ - repository: $repository, - workflow_run_id: $workflow_run_id, - pr_number: $pr_number, - base_sha: $base_sha, - head_sha: $head_sha, - review_effort: $review_effort, - retry_count: $retry_count - }' > "$PROVENANCE_PATH" - env: - BASE_SHA: ${{ inputs.base_sha }} - HEAD_SHA: ${{ inputs.head_sha }} - PROVENANCE_PATH: ${{ runner.temp }}/sfl-review-run-provenance.json - PR_NUMBER: ${{ inputs.item_number }} - REPOSITORY: ${{ github.repository }} - RETRY_COUNT: ${{ inputs.retry_count }} - REVIEW_EFFORT: ${{ env.SFL_REVIEW_EFFORT }} - RUN_ID: ${{ github.run_id }} - - name: Initialize review evidence - id: initialize-evidence - uses: actions/github-script@3a2844b7e9c422d3c10d287c895573f7108da1b3 # v9.0.0 - env: - SFL_BASE_SHA: ${{ inputs.base_sha }} - SFL_HEAD_SHA: ${{ inputs.head_sha }} - SFL_PR_NUMBER: ${{ inputs.item_number }} - SFL_RUN_ID: ${{ github.run_id }} - with: - github-token: ${{ github.token }} - script: | - const baseSha = process.env.SFL_BASE_SHA; - const headSha = process.env.SFL_HEAD_SHA; - const pullNumber = Number(process.env.SFL_PR_NUMBER); - const runId = Number(process.env.SFL_RUN_ID); - if (!Number.isSafeInteger(runId) || runId <= 0) { - core.setFailed(`Invalid workflow run ID: ${process.env.SFL_RUN_ID}`); - return; - } - const { data: pullRequest } = await github.rest.pulls.get({ - owner: context.repo.owner, - repo: context.repo.repo, - pull_number: pullNumber, - }); - if ( - pullRequest.base.sha !== baseSha || - pullRequest.head.sha !== headSha - ) { - core.setFailed( - `Pull request changed from base ${baseSha} / head ${headSha} ` + - `to base ${pullRequest.base.sha} / head ${pullRequest.head.sha}` - ); - return; - } - const { data: evidence } = await github.rest.checks.create({ - owner: context.repo.owner, - repo: context.repo.repo, - name: 'SFL Review Evidence', - head_sha: headSha, - external_id: - `sfl-review:${pullNumber}:${baseSha}:${headSha}:${runId}`, - status: 'in_progress', - started_at: new Date().toISOString(), - output: { - title: 'SFL review is in progress', - summary: 'The immutable pull request state is being reviewed.', - text: 'replacement_retry_eligible=false', - }, - }); - core.setOutput('check-run-id', String(evidence.id)); - - name: Upload immutable review dispatch provenance - if: always() - uses: actions/upload-artifact@043fb46d1a93c77aae656e7c1c64a875d1fc6a0a # v7.0.1 - with: - if-no-files-found: error - name: sfl-review-run-provenance - path: ${{ runner.temp }}/sfl-review-run-provenance.json - retention-days: 1 - - resolve_sfl_review_thread: - needs: - - agent - - detection - - safe_outputs - if: > - (!cancelled()) && needs.agent.result != 'skipped' && contains(needs.agent.outputs.output_types, 'resolve_sfl_review_thread') && - (needs.safe_outputs.result == 'success') - runs-on: ubuntu-slim - permissions: - contents: write - pull-requests: write - steps: - - name: Download agent output artifact - continue-on-error: true - uses: actions/download-artifact@3e5f45b2cfb9172054b4087a40e8e0b5a5461e7c # v8.0.1 - with: - name: agent - path: ${{ runner.temp }}/gh-aw/safe-jobs/ - - name: Generate SFL App token for thread validation - id: validation-token - uses: actions/create-github-app-token@bcd2ba49218906704ab6c1aa796996da409d3eb1 # v3.2.0 - env: - GH_AW_AGENT_OUTPUT: ${{ runner.temp }}/gh-aw/safe-jobs/agent_output.json - with: - client-id: ${{ vars.SFL_APP_CLIENT_ID }} - owner: ${{ github.repository_owner }} - permission-contents: read - permission-pull-requests: read - private-key: ${{ secrets.SFL_APP_PRIVATE_KEY }} - repositories: ${{ github.event.repository.name }} - - name: Resolve SFL App identity - id: app-identity - uses: actions/github-script@3a2844b7e9c422d3c10d287c895573f7108da1b3 # v9.0.0 - env: - GH_AW_AGENT_OUTPUT: ${{ runner.temp }}/gh-aw/safe-jobs/agent_output.json - with: - github-token: ${{ steps.validation-token.outputs.token }} - script: | - const data = await github.graphql(`query { viewer { login } }`); - const login = (data.viewer?.login || '') - .replace(/\[bot\]$/i, '') - .toLowerCase(); - if (!login) { - core.setFailed('Could not resolve the SFL App identity'); - return; - } - core.setOutput('login', login); - - name: Validate and resolve requested SFL threads - uses: actions/github-script@3a2844b7e9c422d3c10d287c895573f7108da1b3 # v9.0.0 - env: - GH_AW_AGENT_OUTPUT: ${{ runner.temp }}/gh-aw/safe-jobs/agent_output.json - SFL_APP_LOGIN: ${{ steps.app-identity.outputs.login }} - SFL_EXPECTED_BASE: ${{ inputs.base_sha }} - SFL_EXPECTED_HEAD: ${{ inputs.head_sha }} - SFL_PR_NUMBER: ${{ inputs.item_number }} - with: - github-token: ${{ github.token }} - script: | - const fs = require('fs'); - const outputPath = process.env.GH_AW_AGENT_OUTPUT; - const expectedBase = process.env.SFL_EXPECTED_BASE; - const expectedHead = process.env.SFL_EXPECTED_HEAD; - const pullNumber = Number(process.env.SFL_PR_NUMBER); - if (!outputPath || !fs.existsSync(outputPath)) { - core.setFailed('SFL agent output is unavailable for thread resolution'); - return; - } - if (!/^[0-9a-f]{40}$/.test(expectedHead || '')) { - core.setFailed(`Invalid expected pull request head: ${expectedHead || 'missing'}`); - return; - } - if (!/^[0-9a-f]{40}$/.test(expectedBase || '')) { - core.setFailed(`Invalid expected pull request base: ${expectedBase || 'missing'}`); - return; - } - if (!Number.isInteger(pullNumber) || pullNumber <= 0) { - core.setFailed(`Invalid pull request number: ${process.env.SFL_PR_NUMBER}`); - return; - } - - const output = JSON.parse(fs.readFileSync(outputPath, 'utf8')); - const requests = (Array.isArray(output.items) ? output.items : []) - .filter(item => item.type === 'resolve_sfl_review_thread'); - const threadIds = [...new Set(requests.map(item => item.thread_id))]; - if (threadIds.length === 0) { - core.info('No obsolete SFL review threads requested for resolution'); - return; - } - if (threadIds.length > 30) { - core.setFailed(`Requested ${threadIds.length} thread resolutions; maximum is 30`); - return; - } - - const normalizeActorLogin = login => - (login || '').replace(/\[bot\]$/i, '').toLowerCase(); - const appLogin = normalizeActorLogin(process.env.SFL_APP_LOGIN); - if (!appLogin) { - core.setFailed('Could not resolve the SFL App identity'); - return; - } - const severityPrefix = - /^(🔴 \*\*CRITICAL|🟠 \*\*HIGH|🟡 \*\*MEDIUM|⚪ \*\*LOW) —/; - const unresolvedOutdatedThreadIds = []; - - for (const threadId of threadIds) { - if (typeof threadId !== 'string' || !threadId.startsWith('PRRT_')) { - core.setFailed(`Invalid pull request review thread ID: ${threadId}`); - return; - } - const data = await github.graphql( - `query($threadId: ID!) { - node(id: $threadId) { - ... on PullRequestReviewThread { - id - isOutdated - isResolved - pullRequest { - number - baseRefOid - headRefOid - repository { - nameWithOwner - } - } - comments(first: 1) { - nodes { - author { - login - __typename - } - body - path - } - } - } - } - }`, - { threadId } - ); - const thread = data.node; - const rootComment = thread?.comments?.nodes?.[0]; - const body = rootComment?.body || ''; - const path = rootComment?.path; - if ( - !thread || - thread.pullRequest?.repository?.nameWithOwner !== - `${context.repo.owner}/${context.repo.repo}` || - thread.pullRequest?.number !== pullNumber || - thread.pullRequest?.baseRefOid !== expectedBase || - thread.pullRequest?.headRefOid !== expectedHead || - rootComment?.author?.__typename !== 'Bot' || - normalizeActorLogin(rootComment.author.login) !== appLogin || - !severityPrefix.test(body) || - !body.includes('SFL Reviewer') || - typeof path !== 'string' || - path.length === 0 || - (!thread.isResolved && !thread.isOutdated) - ) { - core.setFailed( - `Thread ${threadId} is not an obsolete SFL finding on PR #${pullNumber} at base ${expectedBase} and head ${expectedHead}` - ); - return; - } - if (thread.isResolved) { - core.info(`SFL review thread ${threadId} is already resolved`); - continue; - } - try { - const { data: pathAtHead } = await github.rest.repos.getContent({ - owner: context.repo.owner, - repo: context.repo.repo, - path, - ref: expectedHead, - }); - if (Array.isArray(pathAtHead) || pathAtHead.type !== 'file') { - core.setFailed( - `Thread ${threadId} path ${path} is not a file at head ${expectedHead}` - ); - return; - } - } catch (error) { - if (error.status === 404) { - core.setFailed( - `Thread ${threadId} path ${path} no longer exists at head ${expectedHead}; human resolution is required` - ); - return; - } - throw error; - } - unresolvedOutdatedThreadIds.push(threadId); - } - - const failedThreadIds = []; - for (const threadId of unresolvedOutdatedThreadIds) { - let resolved = false; - let lastError; - for (let attempt = 1; attempt <= 3; attempt += 1) { - try { - const result = await github.graphql( - `mutation($threadId: ID!) { - resolveReviewThread(input: { threadId: $threadId }) { - thread { - id - isResolved - } - } - }`, - { threadId } - ); - if (result.resolveReviewThread?.thread?.isResolved) { - resolved = true; - break; - } - lastError = new Error('mutation did not return a resolved thread'); - } catch (error) { - lastError = error; - } - if (attempt < 3) { - await new Promise(resolve => setTimeout(resolve, attempt * 1000)); - } - } - if (!resolved) { - core.error( - `Failed to resolve SFL review thread ${threadId}: ${lastError?.message || 'unknown error'}` - ); - failedThreadIds.push(threadId); - continue; - } - core.info(`Resolved obsolete SFL review thread ${threadId}`); - } - if (failedThreadIds.length > 0) { - core.setFailed( - `Failed to resolve ${failedThreadIds.length} obsolete SFL review thread(s)` - ); - } - - review_metadata: - needs: - - agent - - publish_review_provenance - - resolve_sfl_review_thread - - safe_outputs - if: always() - runs-on: ubuntu-slim - permissions: - checks: write - contents: read - pull-requests: read - steps: - - name: Configure GH_HOST for enterprise compatibility - id: ghes-host-config - shell: bash - run: | # zizmor: ignore[github-env] - GITHUB_SERVER_URL is set by GitHub Actions, not user input. - # Derive GH_HOST from GITHUB_SERVER_URL so the gh CLI targets the correct - # GitHub instance (GHES/GHEC). On github.com this is a harmless no-op. - GH_HOST="${GITHUB_SERVER_URL#https://}" - GH_HOST="${GH_HOST#http://}" - echo "GH_HOST=${GH_HOST}" >> "$GITHUB_ENV" - - name: Verify expected pull request head - id: resolve-head - uses: actions/github-script@3a2844b7e9c422d3c10d287c895573f7108da1b3 # v9.0.0 - env: - SFL_EXPECTED_BASE: ${{ inputs.base_sha }} - SFL_EXPECTED_HEAD: ${{ inputs.head_sha }} - SFL_PR_NUMBER: ${{ inputs.item_number }} - with: - github-token: ${{ github.token }} - script: | - const pullNumber = Number(process.env.SFL_PR_NUMBER); - const expectedBase = process.env.SFL_EXPECTED_BASE; - const expectedHead = process.env.SFL_EXPECTED_HEAD; - if (!Number.isInteger(pullNumber) || pullNumber <= 0) { - core.setFailed(`Invalid pull request number: ${process.env.SFL_PR_NUMBER}`); - return; - } - if (!/^[0-9a-f]{40}$/.test(expectedHead || '')) { - core.setFailed(`Invalid expected pull request head: ${expectedHead || 'missing'}`); - return; - } - if (!/^[0-9a-f]{40}$/.test(expectedBase || '')) { - core.setFailed(`Invalid expected pull request base: ${expectedBase || 'missing'}`); - return; - } - - const { data: pullRequest } = await github.rest.pulls.get({ - owner: context.repo.owner, - repo: context.repo.repo, - pull_number: pullNumber, - }); - core.setOutput('head-sha', expectedHead); - if ( - pullRequest.base.sha !== expectedBase || - pullRequest.head.sha !== expectedHead - ) { - core.setFailed( - `Pull request changed from base ${expectedBase} / head ${expectedHead} to base ${pullRequest.base.sha} / head ${pullRequest.head.sha}` - ); - } - - name: Download safe output items - if: needs.agent.result == 'success' && needs.safe_outputs.result == 'success' && (needs.resolve_sfl_review_thread.result == 'success' || needs.resolve_sfl_review_thread.result == 'skipped') - uses: actions/download-artifact@3e5f45b2cfb9172054b4087a40e8e0b5a5461e7c # v8.0.1 - with: - name: safe-outputs-items - path: /tmp/sfl-review-safe-outputs - - name: Download reviewer agent output - if: needs.agent.result == 'success' && needs.safe_outputs.result == 'success' && (needs.resolve_sfl_review_thread.result == 'success' || needs.resolve_sfl_review_thread.result == 'skipped') - uses: actions/download-artifact@3e5f45b2cfb9172054b4087a40e8e0b5a5461e7c # v8.0.1 - with: - name: agent - path: /tmp/gh-aw - - name: Generate GitHub App token - id: app-token - if: always() - uses: actions/create-github-app-token@bcd2ba49218906704ab6c1aa796996da409d3eb1 # v3.2.0 - with: - client-id: ${{ vars.SFL_APP_CLIENT_ID }} - owner: ${{ github.repository_owner }} - permission-actions: read - permission-contents: read - permission-pull-requests: write - private-key: ${{ secrets.SFL_APP_PRIVATE_KEY }} - repositories: ${{ github.event.repository.name }} - - name: Finalize review metadata - id: finalize-review - if: needs.agent.result == 'success' && needs.safe_outputs.result == 'success' && (needs.resolve_sfl_review_thread.result == 'success' || needs.resolve_sfl_review_thread.result == 'skipped') - uses: actions/github-script@3a2844b7e9c422d3c10d287c895573f7108da1b3 # v9.0.0 - env: - SFL_AGENT_OUTPUT: /tmp/gh-aw/agent_output.json - SFL_AIC: ${{ needs.agent.outputs.aic }} - SFL_EFFORT: ${{ env.SFL_REVIEW_EFFORT }} - SFL_EXPECTED_BASE: ${{ inputs.base_sha }} - SFL_EXPECTED_HEAD: ${{ inputs.head_sha }} - SFL_MODEL: ${{ needs.agent.outputs.model }} - SFL_PR_NUMBER: ${{ github.event.pull_request.number || inputs.item_number }} - SFL_RUN_ID: ${{ github.run_id }} - SFL_SAFE_OUTPUT_ITEMS: /tmp/sfl-review-safe-outputs/safe-output-items.jsonl - with: - github-token: ${{ steps.app-token.outputs.token }} - script: | - // Keep these helpers inline so the HemSoft deployer can install a - // self-contained workflow without additional script dependencies. - function formatElapsed(startedAt, submittedAt) { - if (!Number.isFinite(startedAt) || !Number.isFinite(submittedAt)) { - return 'Unavailable'; - } - const elapsedSeconds = Math.max(0, Math.round((submittedAt - startedAt) / 1000)); - const hours = Math.floor(elapsedSeconds / 3600); - const minutes = Math.floor((elapsedSeconds % 3600) / 60); - const seconds = elapsedSeconds % 60; - return hours > 0 - ? `${hours}h ${minutes}m ${seconds}s` - : `${minutes}m ${seconds}s`; - } - - function formatModel(modelId) { - const [baseModel, query = ''] = modelId.split('?', 2); - const effort = new URLSearchParams(query).get('effort') || - (process.env.SFL_EFFORT || '').trim(); - const claude = baseModel.match(/^claude-(opus|sonnet|haiku)-(.+)$/); - let displayName = baseModel; - if (claude) { - displayName = `${claude[1][0].toUpperCase()}${claude[1].slice(1)} ${claude[2]}`; - } else if (baseModel.startsWith('gpt-')) { - const parts = baseModel.slice(4).split('-'); - displayName = `GPT-${parts.shift()}${parts.length ? ` ${parts.map(part => part[0].toUpperCase() + part.slice(1)).join(' ')}` : ''}`; - } - return effort - ? `${displayName} (${effort[0].toUpperCase()}${effort.slice(1)} effort)` - : displayName; - } - - function formatAic(rawAic) { - const value = rawAic.trim() === '' ? Number.NaN : Number(rawAic); - return Number.isFinite(value) - ? value.toFixed(1).replace(/\.0$/, '') - : 'Unavailable'; - } - - async function countUnresolvedSflFindings({ - owner, - repo, - pullNumber, - reviewAuthor, - }) { - const normalizeActorLogin = login => - (login || '').replace(/\[bot\]$/i, '').toLowerCase(); - const severityPrefix = - /^(🔴 \*\*CRITICAL|🟠 \*\*HIGH|🟡 \*\*MEDIUM|⚪ \*\*LOW) —/; - let cursor = null; - let unresolvedCount = 0; - do { - const data = await github.graphql( - `query($owner: String!, $repo: String!, $pullNumber: Int!, $cursor: String) { - repository(owner: $owner, name: $repo) { - pullRequest(number: $pullNumber) { - reviewThreads(first: 100, after: $cursor) { - nodes { - isResolved - comments(first: 1) { - nodes { - author { - login - __typename - } - body - } - } - } - pageInfo { - hasNextPage - endCursor - } - } - } - } - }`, - { owner, repo, pullNumber, cursor } - ); - const threads = data.repository?.pullRequest?.reviewThreads; - if (!threads) { - throw new Error('Pull request review threads were unavailable'); - } - for (const thread of threads.nodes || []) { - const rootComment = thread.comments?.nodes?.[0]; - const body = rootComment?.body || ''; - if ( - !thread.isResolved && - rootComment?.author?.__typename === 'Bot' && - normalizeActorLogin(rootComment.author.login) === - normalizeActorLogin(reviewAuthor) && - severityPrefix.test(body) && - body.includes('SFL Reviewer') - ) { - unresolvedCount += 1; - } - } - cursor = threads.pageInfo.hasNextPage - ? threads.pageInfo.endCursor - : null; - } while (cursor); - return unresolvedCount; - } - - const owner = context.repo.owner; - const repo = context.repo.repo; - const pullNumber = Number(process.env.SFL_PR_NUMBER); - const runId = Number(process.env.SFL_RUN_ID); - const expectedBase = process.env.SFL_EXPECTED_BASE; - const expectedHead = process.env.SFL_EXPECTED_HEAD; - if (!Number.isInteger(pullNumber) || pullNumber <= 0) { - core.setFailed(`Invalid pull request number: ${process.env.SFL_PR_NUMBER}`); - return; - } - if (!Number.isInteger(runId) || runId <= 0) { - core.setFailed(`Invalid workflow run ID: ${process.env.SFL_RUN_ID}`); - return; - } - if (!/^[0-9a-f]{40}$/.test(expectedHead || '')) { - core.setFailed(`Invalid expected pull request head: ${expectedHead || 'missing'}`); - return; - } - if (!/^[0-9a-f]{40}$/.test(expectedBase || '')) { - core.setFailed(`Invalid expected pull request base: ${expectedBase || 'missing'}`); - return; - } - - const { data: pullRequest } = await github.rest.pulls.get({ - owner, - repo, - pull_number: pullNumber, - }); - if ( - pullRequest.base.sha !== expectedBase || - pullRequest.head.sha !== expectedHead - ) { - core.setFailed( - `Pull request changed from base ${expectedBase} / head ${expectedHead} to base ${pullRequest.base.sha} / head ${pullRequest.head.sha}` - ); - return; - } - - const fs = require('fs'); - const safeOutputItemsPath = process.env.SFL_SAFE_OUTPUT_ITEMS; - let safeOutputItems; - try { - safeOutputItems = fs - .readFileSync(safeOutputItemsPath, 'utf8') - .split(/\r?\n/) - .filter(Boolean) - .map(line => JSON.parse(line)); - } catch (error) { - core.setFailed( - `Could not read safe-output review metadata: ${error.message}` - ); - return; - } - let agentItems = []; - let replacementRetryEligible = false; - try { - const agentOutput = JSON.parse( - fs.readFileSync(process.env.SFL_AGENT_OUTPUT, 'utf8') - ); - agentItems = Array.isArray(agentOutput) - ? agentOutput - : (agentOutput.items || []); - if (!Array.isArray(agentItems)) { - throw new Error('agent items are not an array'); - } - replacementRetryEligible = !agentItems.some(item => - item.type === 'missing_data' || - item.type === 'missing_tool' || - item.type === 'report_incomplete' - ); - } catch (error) { - core.warning( - `Could not inspect terminal review signals; suppressing replacement: ${error.message}` - ); - } - core.setOutput( - 'replacement-retry-eligible', - replacementRetryEligible - ); - const agentSubmittedReviews = agentItems.filter( - item => item.type === 'submit_pull_request_review' - ); - if (agentSubmittedReviews.length !== 1) { - core.setFailed( - `Expected exactly one immutable agent submitted-review item, found ${agentSubmittedReviews.length}` - ); - return; - } - const [agentSubmittedReview] = agentSubmittedReviews; - // gh-aw preserves the agent's raw tool arguments; on a - // fixed-target workflow an omitted repo/pull_request_number means - // the configured pull request. Default omitted targets while - // still rejecting explicitly mismatched ones. - const configuredRepo = `${owner}/${repo}`; - if ( - (agentSubmittedReview.repo ?? configuredRepo) !== configuredRepo || - Number(agentSubmittedReview.pull_request_number ?? pullNumber) !== pullNumber - ) { - core.setFailed('Immutable agent submitted-review target does not match this pull request'); - return; - } - const severityPrefix = - /^(🔴 \*\*CRITICAL|🟠 \*\*HIGH|🟡 \*\*MEDIUM|⚪ \*\*LOW) —/; - const agentReviewComments = agentItems.filter( - item => item.type === 'create_pull_request_review_comment' - ); - if (agentReviewComments.some(item => - (item.repo ?? configuredRepo) !== configuredRepo || - Number(item.pull_request_number ?? pullNumber) !== pullNumber || - !severityPrefix.test(item.body || '') - )) { - core.setFailed('Immutable agent review comments contain an invalid target or finding format'); - return; - } - const publishedReviewComments = safeOutputItems.filter( - item => item.type === 'create_pull_request_review_comment' - ); - if (publishedReviewComments.some(item => - item.repo !== `${owner}/${repo}` || - Number(item.number) !== pullNumber - )) { - core.setFailed('Published review-comment manifest target does not match this pull request'); - return; - } - if (publishedReviewComments.length !== agentReviewComments.length) { - core.setFailed( - `Published review-comment count ${publishedReviewComments.length} does not match immutable agent count ${agentReviewComments.length}` - ); - return; - } - const submittedReviews = safeOutputItems.filter( - item => item.type === 'submit_pull_request_review' - ); - if (submittedReviews.length !== 1) { - core.setFailed( - `Expected exactly one submitted-review manifest item, found ${submittedReviews.length}` - ); - return; - } - const [submittedReview] = submittedReviews; - if ( - submittedReview.repo !== `${owner}/${repo}` || - Number(submittedReview.number) !== pullNumber - ) { - core.setFailed('Submitted-review manifest target does not match this pull request'); - return; - } - const reviewId = Number(submittedReview.metadata?.review_id); - if (!Number.isInteger(reviewId) || reviewId <= 0) { - core.setFailed('Submitted-review manifest does not contain a valid review ID'); - return; - } - const { data: review } = await github.rest.pulls.getReview({ - owner, - repo, - pull_number: pullNumber, - review_id: reviewId, - }); - if (!review.commit_id || review.commit_id !== expectedHead) { - core.setFailed( - `SFL review commit ${review.commit_id || 'missing'} does not match expected head ${expectedHead}` - ); - return; - } - - const { data: run } = await github.rest.actions.getWorkflowRun({ - owner, - repo, - run_id: runId, - }); - const startedAt = Date.parse(run.run_started_at || run.created_at); - const submittedAt = Date.parse(review.submitted_at); - const elapsed = formatElapsed(startedAt, submittedAt); - const model = formatModel(process.env.SFL_MODEL || 'unknown'); - const aic = formatAic(process.env.SFL_AIC || ''); - - const originalBody = agentSubmittedReview.body || ''; - for (const placeholder of [ - 'SFL_MODEL_PENDING', - 'SFL_TIME_PENDING', - 'SFL_AIC_PENDING', - ]) { - const occurrences = originalBody.split(placeholder).length - 1; - if (occurrences !== 1) { - core.setFailed( - `Review body must contain ${placeholder} exactly once; found ${occurrences}` - ); - return; - } - } - const templatePlaceholders = [ - '{verdict_icon}', - '{VERDICT}', - '{verdict_reason}', - '{security_critical_or_dash}', - '{security_high_or_dash}', - '{security_medium_or_dash}', - '{security_low_or_dash}', - '{accuracy_critical_or_dash}', - '{accuracy_high_or_dash}', - '{accuracy_medium_or_dash}', - '{accuracy_low_or_dash}', - '{quality_maintainability_critical_or_dash}', - '{quality_maintainability_high_or_dash}', - '{quality_maintainability_medium_or_dash}', - '{quality_maintainability_low_or_dash}', - '{total_critical_or_dash}', - '{total_high_or_dash}', - '{total_medium_or_dash}', - '{total_low_or_dash}', - '{Top 3 most impactful current findings with their severity prefixes, or "No findings."}', - ]; - const unresolvedPlaceholder = templatePlaceholders.find(placeholder => - originalBody.includes(placeholder) - ); - if (unresolvedPlaceholder) { - core.setFailed( - `Review body contains unresolved template placeholder ${unresolvedPlaceholder}` - ); - return; - } - - const body = originalBody - .replaceAll('SFL_MODEL_PENDING', model) - .replaceAll('SFL_TIME_PENDING', elapsed) - .replaceAll('SFL_AIC_PENDING', aic); - - const verdictApproved = body.includes('## ✅ Verdict: APPROVE'); - const verdictNeedsWork = body.includes('## ⚠️ Verdict: NEEDS WORK'); - if (verdictApproved === verdictNeedsWork) { - core.setFailed('Review body must contain exactly one recognized SFL verdict'); - return; - } - - const prAuthor = pullRequest.user?.login; - const reviewAuthor = review.user?.login; - if (!reviewAuthor) { - core.setFailed('SFL review author identity is unavailable'); - return; - } - const selfAuthored = Boolean(prAuthor) && prAuthor === reviewAuthor; - const expectedReviewEvent = verdictApproved && !selfAuthored - ? 'APPROVE' - : 'COMMENT'; - const expectedReviewState = verdictApproved && !selfAuthored - ? 'APPROVED' - : 'COMMENTED'; - if ((agentSubmittedReview.event || '').toUpperCase() !== expectedReviewEvent) { - core.setFailed( - `Immutable agent review event ${agentSubmittedReview.event || 'missing'} does not match expected event ${expectedReviewEvent}` - ); - return; - } - if ( - (submittedReview.metadata?.review_event || '').toUpperCase() !== - expectedReviewEvent || - submittedReview.metadata?.review_state !== expectedReviewState - ) { - core.setFailed('Published review manifest does not match the immutable agent verdict'); - return; - } - if (review.state !== expectedReviewState) { - core.setFailed( - `Review state ${review.state || 'missing'} does not match expected state ${expectedReviewState}` - ); - return; - } - - // GitHub accepts body-only updates to submitted COMMENTED reviews - // when authenticated as the app that authored the review. - await github.request('PUT /repos/{owner}/{repo}/pulls/{pull_number}/reviews/{review_id}', { - owner, - repo, - pull_number: pullNumber, - review_id: review.id, - body, - }); - - let publishedFindingCount; - try { - const reviewComments = await github.paginate( - 'GET /repos/{owner}/{repo}/pulls/{pull_number}/reviews/{review_id}/comments', - { - owner, - repo, - pull_number: pullNumber, - review_id: review.id, - per_page: 100, - } - ); - const inlineRunMarker = `[Run ${runId}]`; - publishedFindingCount = reviewComments.filter(comment => - severityPrefix.test(comment.body || '') && - comment.body?.includes(inlineRunMarker) - ).length; - } catch (error) { - core.setFailed(`Could not enumerate SFL findings: ${error.message}`); - return; - } - const immutableFindingCount = agentReviewComments.length; - if (publishedFindingCount !== immutableFindingCount) { - core.setFailed( - `Published finding count ${publishedFindingCount} does not match immutable agent count ${immutableFindingCount}` - ); - return; - } - - let unresolvedFindingCount; - try { - unresolvedFindingCount = await countUnresolvedSflFindings({ - owner, - repo, - pullNumber, - reviewAuthor, - }); - } catch (error) { - core.setFailed(`Could not enumerate unresolved SFL findings: ${error.message}`); - return; - } - - const gateApproved = - verdictApproved && - immutableFindingCount === 0 && - unresolvedFindingCount === 0; - core.setOutput('head-sha', expectedHead); - core.setOutput('gate-approved', gateApproved); - core.setOutput('finding-count', unresolvedFindingCount); - core.setOutput('new-finding-count', immutableFindingCount); - core.setOutput('review-matches-head', true); - core.setOutput('review-state', review.state); - - name: Finalize SFL review evidence - if: always() - uses: actions/github-script@3a2844b7e9c422d3c10d287c895573f7108da1b3 # v9.0.0 - env: - SFL_BASE_SHA: ${{ inputs.base_sha }} - SFL_EVIDENCE_CHECK_RUN_ID: ${{ needs.publish_review_provenance.outputs.evidence-check-run-id }} - SFL_FINDING_COUNT: ${{ steps.finalize-review.outputs.finding-count }} - SFL_GATE_APPROVED: ${{ steps.finalize-review.outputs.gate-approved }} - SFL_HEAD_SHA: ${{ inputs.head_sha }} - SFL_PR_NUMBER: ${{ inputs.item_number }} - SFL_REPLACEMENT_RETRY_ELIGIBLE: ${{ steps.finalize-review.outputs.replacement-retry-eligible }} - SFL_REVIEW_MATCHES_HEAD: ${{ steps.finalize-review.outputs.review-matches-head }} - SFL_REVIEW_STATE: ${{ steps.finalize-review.outputs.review-state }} - SFL_RUN_ID: ${{ github.run_id }} - SFL_UPSTREAM_RESULT: agent=${{ needs.agent.result }}, safe_outputs=${{ needs.safe_outputs.result }}, thread_resolution=${{ needs.resolve_sfl_review_thread.result }} - with: - github-token: ${{ github.token }} - script: | - const baseSha = process.env.SFL_BASE_SHA; - const headSha = process.env.SFL_HEAD_SHA; - if (!/^[0-9a-f]{40}$/.test(baseSha || '')) { - core.setFailed(`Cannot publish SFL approval gate for invalid base SHA: ${baseSha || 'missing'}`); - return; - } - if (!/^[0-9a-f]{40}$/.test(headSha || '')) { - core.setFailed(`Cannot publish SFL approval gate for invalid head SHA: ${headSha || 'missing'}`); - return; - } - const pullNumber = Number(process.env.SFL_PR_NUMBER); - const runId = Number(process.env.SFL_RUN_ID); - if (!Number.isSafeInteger(runId) || runId <= 0) { - core.setFailed(`Invalid workflow run ID: ${process.env.SFL_RUN_ID}`); - return; - } - const { data: pullRequest } = await github.rest.pulls.get({ - owner: context.repo.owner, - repo: context.repo.repo, - pull_number: pullNumber, - }); - const baseMatchesExpected = pullRequest.base.sha === baseSha; - const headMatchesExpected = pullRequest.head.sha === headSha; - const gateApproved = process.env.SFL_GATE_APPROVED === 'true'; - const replacementRetryEligible = - process.env.SFL_REPLACEMENT_RETRY_ELIGIBLE === 'true'; - const evidenceCheckRunId = - Number(process.env.SFL_EVIDENCE_CHECK_RUN_ID); - const gateExternalId = - `sfl-review:${pullNumber}:${baseSha}:${headSha}:${runId}`; - if (!Number.isSafeInteger(evidenceCheckRunId) || evidenceCheckRunId <= 0) { - core.setFailed('Cannot finalize missing SFL review evidence'); - return; - } - const { data: initializedEvidence } = await github.rest.checks.get({ - owner: context.repo.owner, - repo: context.repo.repo, - check_run_id: evidenceCheckRunId, - }); - if ( - initializedEvidence.name !== 'SFL Review Evidence' || - initializedEvidence.head_sha !== headSha || - initializedEvidence.external_id !== gateExternalId || - initializedEvidence.app?.id !== 15368 - ) { - core.setFailed('Initialized SFL review evidence does not match this run'); - return; - } - const publishEvidence = async ({ approved, summary }) => { - let lastError; - for (let attempt = 1; attempt <= 3; attempt += 1) { - try { - return await github.rest.checks.update({ - owner: context.repo.owner, - repo: context.repo.repo, - check_run_id: evidenceCheckRunId, - name: 'SFL Review Evidence', - external_id: gateExternalId, - status: 'completed', - conclusion: approved ? 'success' : 'failure', - completed_at: new Date().toISOString(), - output: { - title: approved - ? 'SFL zero-finding approval gate satisfied' - : 'SFL Reviewer approval is required', - summary, - text: - `replacement_retry_eligible=${replacementRetryEligible}`, - }, - }); - } catch (error) { - lastError = error; - if (attempt < 3) { - core.warning( - `Evidence finalization attempt ${attempt} failed: ${error.message}` - ); - await new Promise(resolve => setTimeout(resolve, attempt * 1000)); - } - } - } - throw lastError; - }; - const publishFailure = async ({ reason }) => { - await publishEvidence({ - approved: false, - summary: - `${reason}. The review cannot satisfy the approval gate. ` + - `Upstream results: ${process.env.SFL_UPSTREAM_RESULT}.`, - }); - core.setFailed(reason); - }; - if (!baseMatchesExpected || !headMatchesExpected) { - await publishFailure({ - reason: - `Pull request changed from base ${baseSha} / head ${headSha} ` + - `to base ${pullRequest.base.sha} / head ${pullRequest.head.sha}`, - }); - return; - } - const summary = gateApproved - ? 'The latest SFL review approved this head with zero inline findings.' - : `The latest SFL review state is ${process.env.SFL_REVIEW_STATE || 'unavailable'} with ${process.env.SFL_FINDING_COUNT || 'unknown'} unresolved SFL finding thread(s), expected-head review match ${process.env.SFL_REVIEW_MATCHES_HEAD || 'unknown'}, live-base match ${baseMatchesExpected}, and live-head match ${headMatchesExpected}, so it does not satisfy the zero-finding approval gate. Upstream results: ${process.env.SFL_UPSTREAM_RESULT}.`; - await publishEvidence({ - approved: gateApproved, - summary, - }); - if (!gateApproved) { - core.setFailed(summary); - } - - safe_outputs: - needs: - - activation - - agent - - detection - if: (!cancelled()) && needs.agent.result != 'skipped' && needs.detection.result == 'success' - runs-on: ubuntu-slim - permissions: - issues: write - pull-requests: write - timeout-minutes: 45 - env: - GH_AW_AGENT_AIC: ${{ needs.agent.outputs.aic }} - GH_AW_AIC: ${{ needs.agent.outputs.aic }} - GH_AW_AMBIENT_CONTEXT: ${{ needs.agent.outputs.ambient_context }} - GH_AW_CALLER_WORKFLOW_ID: "${{ github.repository }}/sfl-pr-review" - GH_AW_DETECTION_CONCLUSION: ${{ needs.detection.outputs.detection_conclusion }} - GH_AW_DETECTION_REASON: ${{ needs.detection.outputs.detection_reason }} - GH_AW_EFFECTIVE_TOKENS: ${{ needs.agent.outputs.effective_tokens }} - GH_AW_ENGINE_ID: "copilot" - GH_AW_ENGINE_MODEL: "moonshotai/kimi-k3" - GH_AW_RUNTIME_FEATURES: ${{ vars.GH_AW_RUNTIME_FEATURES }} - GH_AW_THREAT_DETECTION_AIC: ${{ needs.detection.outputs.aic }} - GH_AW_WORKFLOW_ID: "sfl-pr-review" - GH_AW_WORKFLOW_NAME: "SFL PR Review" - GH_AW_WORKFLOW_SOURCE: "HemSoft/set-it-free-loop/deployment/workflows/sfl-pr-review.md@main" - GH_AW_WORKFLOW_SOURCE_URL: "${{ github.server_url }}/HemSoft/set-it-free-loop/blob/main/deployment/workflows/sfl-pr-review.md" - outputs: - app_token_minting_failed: ${{ steps.safe-outputs-app-token.outcome == 'failure' }} - code_push_failure_count: ${{ steps.process_safe_outputs.outputs.code_push_failure_count }} - code_push_failure_errors: ${{ steps.process_safe_outputs.outputs.code_push_failure_errors }} - create_discussion_error_count: ${{ steps.process_safe_outputs.outputs.create_discussion_error_count }} - create_discussion_errors: ${{ steps.process_safe_outputs.outputs.create_discussion_errors }} - process_safe_outputs_items_applied: ${{ steps.process_safe_outputs.outputs.items_applied }} - process_safe_outputs_items_cancelled: ${{ steps.process_safe_outputs.outputs.items_cancelled }} - process_safe_outputs_items_deferred: ${{ steps.process_safe_outputs.outputs.items_deferred }} - process_safe_outputs_items_failed: ${{ steps.process_safe_outputs.outputs.items_failed }} - process_safe_outputs_items_skipped: ${{ steps.process_safe_outputs.outputs.items_skipped }} - process_safe_outputs_items_succeeded: ${{ steps.process_safe_outputs.outputs.items_succeeded }} - process_safe_outputs_items_warnings: ${{ steps.process_safe_outputs.outputs.items_warnings }} - process_safe_outputs_processed_count: ${{ steps.process_safe_outputs.outputs.processed_count }} - process_safe_outputs_status: ${{ steps.process_safe_outputs.outputs.status }} - process_safe_outputs_temporary_id_map: ${{ steps.process_safe_outputs.outputs.temporary_id_map }} - steps: - - name: Setup Scripts - id: setup - uses: github/gh-aw-actions/setup@6aab9e5b5c91c615506061f09bedd81a23babe3c # v0.86.2 - with: - destination: ${{ runner.temp }}/gh-aw/actions - job-name: ${{ github.job }} - trace-id: ${{ needs.activation.outputs.setup-trace-id }} - parent-span-id: ${{ needs.activation.outputs.setup-parent-span-id || needs.activation.outputs.setup-span-id }} - env: - GH_AW_SETUP_WORKFLOW_NAME: "SFL PR Review" - GH_AW_CURRENT_WORKFLOW_REF: ${{ github.repository }}/.github/workflows/sfl-pr-review.lock.yml@${{ github.ref }} - GH_AW_INFO_VERSION: "1.0.79" - GH_AW_INFO_AWF_VERSION: "v0.27.44" - GH_AW_INFO_BODY_MODIFIED: "false" - GH_AW_INFO_ENGINE_ID: "copilot" - - name: Download agent output artifact - id: download-agent-output - continue-on-error: true - uses: actions/download-artifact@3e5f45b2cfb9172054b4087a40e8e0b5a5461e7c # v8.0.1 - with: - name: agent - path: /tmp/gh-aw/ - - name: Setup agent output environment variable - id: setup-agent-output-env - if: steps.download-agent-output.outcome == 'success' - run: | - mkdir -p /tmp/gh-aw/ - find "/tmp/gh-aw/" -type f -print - echo "GH_AW_AGENT_OUTPUT=/tmp/gh-aw/agent_output.json" >> "$GITHUB_OUTPUT" - - name: Generate GitHub App token - id: safe-outputs-app-token - uses: actions/create-github-app-token@bcd2ba49218906704ab6c1aa796996da409d3eb1 # v3.2.0 - with: - client-id: ${{ vars.SFL_APP_CLIENT_ID }} - private-key: ${{ secrets.SFL_APP_PRIVATE_KEY }} - owner: ${{ github.repository_owner }} - repositories: ${{ github.event.repository.name }} - github-api-url: ${{ github.api_url }} - permission-issues: write - permission-pull-requests: write - - name: Configure GH_HOST for enterprise compatibility - id: ghes-host-config - shell: bash - run: | # zizmor: ignore[github-env] - GITHUB_SERVER_URL is set by GitHub Actions, not user input. - # Derive GH_HOST from GITHUB_SERVER_URL so the gh CLI targets the correct - # GitHub instance (GHES/GHEC). On github.com this is a harmless no-op. - GH_HOST="${GITHUB_SERVER_URL#https://}" - GH_HOST="${GH_HOST#http://}" - echo "GH_HOST=${GH_HOST}" >> "$GITHUB_ENV" - - name: Process Safe Outputs - id: process_safe_outputs - uses: actions/github-script@3a2844b7e9c422d3c10d287c895573f7108da1b3 # v9.0.0 - env: - GH_AW_AGENT_OUTPUT: ${{ steps.setup-agent-output-env.outputs.GH_AW_AGENT_OUTPUT }} - GH_AW_COMMENT_ID: ${{ needs.activation.outputs.comment_id }} - GH_AW_ALLOWED_DOMAINS: "api.business.githubcopilot.com,api.enterprise.githubcopilot.com,api.github.com,api.githubcopilot.com,api.individual.githubcopilot.com,github.com,host.docker.internal,openrouter.ai,raw.githubusercontent.com,registry.npmjs.org,telemetry.enterprise.githubcopilot.com" - GITHUB_SERVER_URL: ${{ github.server_url }} - GITHUB_API_URL: ${{ github.api_url }} - GH_AW_SAFE_OUTPUT_JOBS: "{\"resolve_sfl_review_thread\":\"\"}" - GH_AW_SAFE_OUTPUTS_HANDLER_CONFIG: "{\"create_pull_request_review_comment\":{\"commit_id\":\"${{ inputs.head_sha }}\",\"max\":30,\"side\":\"RIGHT\",\"target\":\"${{ inputs.item_number }}\"},\"create_report_incomplete_issue\":{},\"missing_data\":{},\"missing_tool\":{},\"noop\":{\"max\":1,\"report-as-issue\":\"false\"},\"report_incomplete\":{},\"submit_pull_request_review\":{\"commit_id\":\"${{ inputs.head_sha }}\",\"max\":1,\"target\":\"${{ inputs.item_number }}\"}}" - with: - github-token: ${{ steps.safe-outputs-app-token.outputs.token }} - script: | - const { setupGlobals } = require('${{ runner.temp }}/gh-aw/actions/setup_globals.cjs'); - setupGlobals(core, github, context, exec, io, getOctokit); - const { main } = require('${{ runner.temp }}/gh-aw/actions/process_safe_outputs.cjs'); - await main(); - - name: Upload Safe Outputs Items - if: always() - uses: actions/upload-artifact@043fb46d1a93c77aae656e7c1c64a875d1fc6a0a # v7.0.1 - with: - name: safe-outputs-items - path: | - /tmp/gh-aw/safe-output-items.jsonl - /tmp/gh-aw/temporary-id-map.json - if-no-files-found: ignore diff --git a/.github/workflows/sfl-pr-review.md b/.github/workflows/sfl-pr-review.md deleted file mode 100644 index 8cdcca85..00000000 --- a/.github/workflows/sfl-pr-review.md +++ /dev/null @@ -1,1648 +0,0 @@ ---- -# HemSoft SFL reviewer platform v2 -name: SFL PR Review -description: | - Full-spectrum PR review dispatched automatically by the SFL PR review wrapper - or manually through workflow_dispatch. Performs 3 evidence-based review - passes (Security, Accuracy & Reliability, Quality & Maintainability) and - posts inline review comments on specific lines with an overall summary. -source: HemSoft/set-it-free-loop/deployment/workflows/sfl-pr-review.md@main - -on: - workflow_dispatch: - inputs: - aw_context: - description: gh-aw pull request context JSON - required: true - type: string - item_number: - description: Pull request number to review - required: true - type: string - base_sha: - description: Expected pull request base commit - required: true - type: string - head_sha: - description: Expected pull request head commit - required: true - type: string - dispatch_id: - description: Unique wrapper or recovery dispatch identifier - required: false - default: manual - type: string - retry_count: - description: Missing-review-output retry counter - required: false - default: "0" - type: string - review_effort: - description: Audit effort marker retained in provenance and recovery - required: false - default: low - type: choice - options: - - low - - medium - - high - -run-name: "SFL PR Review #${{ inputs.item_number }} ${{ inputs.base_sha }}:${{ inputs.head_sha }} retry=${{ inputs.retry_count }} dispatch=${{ inputs.dispatch_id }}" - -concurrency: - group: sfl-pr-review-${{ inputs.item_number }} - cancel-in-progress: true - -permissions: - contents: read - checks: read - issues: read - pull-requests: read - -env: - SFL_REVIEW_EFFORT: ${{ inputs.review_effort || 'low' }} - -pre-steps: - - name: Validate trusted review context - env: - AW_CONTEXT: ${{ inputs.aw_context }} - BASE_SHA: ${{ inputs.base_sha }} - DISPATCH_ID: ${{ inputs.dispatch_id }} - HEAD_SHA: ${{ inputs.head_sha }} - ITEM_NUMBER: ${{ inputs.item_number }} - run: | - set -euo pipefail - if ! [[ "$ITEM_NUMBER" =~ ^[1-9][0-9]*$ ]]; then - echo "::error::Invalid pull request number" - exit 1 - fi - if ! [[ "$BASE_SHA" =~ ^[0-9a-f]{40}$ ]] \ - || ! [[ "$HEAD_SHA" =~ ^[0-9a-f]{40}$ ]]; then - echo "::error::Invalid pull request revision" - exit 1 - fi - if ! [[ "$DISPATCH_ID" =~ ^[A-Za-z0-9._-]{1,64}$ ]]; then - echo "::error::Invalid dispatch identifier" - exit 1 - fi - if ! jq -e \ - --argjson item_number "$ITEM_NUMBER" \ - --arg base_sha "$BASE_SHA" \ - --arg head_sha "$HEAD_SHA" ' - . == { - item_type: "pull_request", - item_number: $item_number, - base_sha: $base_sha, - head_sha: $head_sha - } - ' <<< "$AW_CONTEXT" > /dev/null; then - echo "::error::Untrusted or inconsistent review context" - exit 1 - fi - - - name: Install ripgrep with bounded diagnostics - id: sfl-ripgrep - shell: bash - run: | - set -euo pipefail - log_path="${RUNNER_TEMP}/sfl-ripgrep-install.log" - : > "$log_path" - if command -v rg > /dev/null 2>&1; then - rg --version | tee -a "$log_path" - exit 0 - fi - - set +e - timeout --signal=TERM --kill-after=15s 180s bash -c ' - set -euo pipefail - sudo apt-get update \ - -o Acquire::Retries=3 \ - -o Acquire::http::Timeout=30 \ - -o Acquire::https::Timeout=30 - sudo apt-get install --no-install-recommends -y ripgrep - ' 2>&1 | tee -a "$log_path" - install_status=${PIPESTATUS[0]} - set -e - - if [ "$install_status" -eq 124 ] || [ "$install_status" -eq 137 ]; then - echo "::error::ripgrep installation exceeded the 180-second setup limit" - exit 1 - fi - if [ "$install_status" -ne 0 ]; then - echo "::error::ripgrep installation failed with exit code ${install_status}" - exit "$install_status" - fi - if ! command -v rg > /dev/null 2>&1; then - echo "::error::ripgrep installation completed without making rg available" - exit 1 - fi - rg --version | tee -a "$log_path" - - - name: Retain ripgrep setup diagnostics - if: always() - uses: actions/upload-artifact@043fb46d1a93c77aae656e7c1c64a875d1fc6a0a # v7.0.1 - with: - name: sfl-ripgrep-setup-${{ github.run_id }}-${{ github.run_attempt }} - path: ${{ runner.temp }}/sfl-ripgrep-install.log - if-no-files-found: ignore - retention-days: 1 - -# HemSoft runs the reviewer through Kimi K3 on its private OpenRouter route. - -models: - providers: - github-copilot: - models: - "moonshotai/kimi-k3": - cost: - input: "3e-06" - output: "1.5e-05" - -max-turn-cache-misses: 10 - -engine: - id: copilot - env: - COPILOT_MODEL: moonshotai/kimi-k3 - COPILOT_PROVIDER_API_KEY: ${{ secrets.OPENROUTER_API_KEY }} - COPILOT_PROVIDER_BASE_URL: https://openrouter.ai/api/v1 - COPILOT_PROVIDER_TYPE: openai - COPILOT_PROVIDER_WIRE_API: responses - -model: moonshotai/kimi-k3 - -network: - allowed: - - openrouter.ai - -tools: - github: - lockdown: false - bash: true - -safe-outputs: - threat-detection: - continue-on-error: false - steps: - - name: Install threat-detection ripgrep with bounded diagnostics - id: sfl-threat-detection-ripgrep - if: always() - shell: bash - run: | - set -euo pipefail - log_path="${RUNNER_TEMP}/sfl-threat-detection-ripgrep-install.log" - : > "$log_path" - if command -v rg > /dev/null 2>&1; then - rg --version | tee -a "$log_path" - exit 0 - fi - - set +e - timeout --signal=TERM --kill-after=15s 180s bash -c ' - set -euo pipefail - sudo apt-get update \ - -o Acquire::Retries=3 \ - -o Acquire::http::Timeout=30 \ - -o Acquire::https::Timeout=30 - sudo apt-get install --no-install-recommends -y ripgrep - ' 2>&1 | tee -a "$log_path" - install_status=${PIPESTATUS[0]} - set -e - - if [ "$install_status" -eq 124 ] || [ "$install_status" -eq 137 ]; then - echo "::error::threat-detection ripgrep installation exceeded the 180-second setup limit" - exit 1 - fi - if [ "$install_status" -ne 0 ]; then - echo "::error::threat-detection ripgrep installation failed with exit code ${install_status}" - exit "$install_status" - fi - if ! command -v rg > /dev/null 2>&1; then - echo "::error::threat-detection ripgrep installation completed without making rg available" - exit 1 - fi - rg --version | tee -a "$log_path" - - name: Retain threat-detection ripgrep setup diagnostics - if: always() - uses: actions/upload-artifact@043fb46d1a93c77aae656e7c1c64a875d1fc6a0a # v7.0.1 - with: - name: sfl-threat-detection-ripgrep-setup-${{ github.run_id }}-${{ github.run_attempt }} - path: ${{ runner.temp }}/sfl-threat-detection-ripgrep-install.log - if-no-files-found: ignore - retention-days: 1 - post-steps: - - name: Generate SFL App token for final head verification - id: sfl-final-head-token - uses: actions/create-github-app-token@bcd2ba49218906704ab6c1aa796996da409d3eb1 # v3.2.0 - with: - client-id: ${{ vars.SFL_APP_CLIENT_ID }} - private-key: ${{ secrets.SFL_APP_PRIVATE_KEY }} - owner: ${{ github.repository_owner }} - repositories: ${{ github.event.repository.name }} - permission-pull-requests: read - - name: Verify pull request base and head before safe outputs - env: - EXPECTED_BASE: ${{ inputs.base_sha }} - EXPECTED_HEAD: ${{ inputs.head_sha }} - GH_TOKEN: ${{ steps.sfl-final-head-token.outputs.token }} - PR_NUMBER: ${{ inputs.item_number }} - REPOSITORY: ${{ github.repository }} - run: | - set -euo pipefail - if [[ ! "${EXPECTED_HEAD}" =~ ^[0-9a-f]{40}$ ]]; then - echo "::error::Invalid expected pull request head: ${EXPECTED_HEAD}" - exit 1 - fi - if [[ ! "${EXPECTED_BASE}" =~ ^[0-9a-f]{40}$ ]]; then - echo "::error::Invalid expected pull request base: ${EXPECTED_BASE}" - exit 1 - fi - pull_request="$(gh api "repos/${REPOSITORY}/pulls/${PR_NUMBER}")" - live_head="$(jq -r '.head.sha' <<< "$pull_request")" - live_base="$(jq -r '.base.sha' <<< "$pull_request")" - if [[ "${live_head}" != "${EXPECTED_HEAD}" ]]; then - echo "::error::Pull request head changed from ${EXPECTED_HEAD} to ${live_head}; suppressing stale SFL outputs" - exit 1 - fi - if [[ "${live_base}" != "${EXPECTED_BASE}" ]]; then - echo "::error::Pull request base changed from ${EXPECTED_BASE} to ${live_base}; suppressing stale SFL outputs" - exit 1 - fi - github-app: - client-id: ${{ vars.SFL_APP_CLIENT_ID }} - private-key: ${{ secrets.SFL_APP_PRIVATE_KEY }} - create-pull-request-review-comment: - commit-id: "${{ inputs.head_sha }}" - max: 30 - target: "${{ inputs.item_number }}" - submit-pull-request-review: - commit-id: "${{ inputs.head_sha }}" - max: 1 - target: "${{ inputs.item_number }}" - jobs: - resolve-sfl-review-thread: - description: Resolve one obsolete SFL finding thread after deterministic validation - runs-on: ubuntu-slim - needs: safe_outputs - if: needs.safe_outputs.result == 'success' - permissions: - contents: write - pull-requests: write - inputs: - thread_id: - description: GraphQL node ID of the obsolete SFL review thread - required: true - type: string - steps: - - name: Generate SFL App token for thread validation - id: validation-token - uses: actions/create-github-app-token@bcd2ba49218906704ab6c1aa796996da409d3eb1 # v3.2.0 - with: - client-id: ${{ vars.SFL_APP_CLIENT_ID }} - private-key: ${{ secrets.SFL_APP_PRIVATE_KEY }} - owner: ${{ github.repository_owner }} - repositories: ${{ github.event.repository.name }} - permission-contents: read - permission-pull-requests: read - - name: Resolve SFL App identity - id: app-identity - uses: actions/github-script@3a2844b7e9c422d3c10d287c895573f7108da1b3 # v9.0.0 - with: - github-token: ${{ steps.validation-token.outputs.token }} - script: | - const data = await github.graphql(`query { viewer { login } }`); - const login = (data.viewer?.login || '') - .replace(/\[bot\]$/i, '') - .toLowerCase(); - if (!login) { - core.setFailed('Could not resolve the SFL App identity'); - return; - } - core.setOutput('login', login); - - name: Validate and resolve requested SFL threads - uses: actions/github-script@3a2844b7e9c422d3c10d287c895573f7108da1b3 # v9.0.0 - env: - SFL_APP_LOGIN: ${{ steps.app-identity.outputs.login }} - SFL_EXPECTED_BASE: ${{ inputs.base_sha }} - SFL_EXPECTED_HEAD: ${{ inputs.head_sha }} - SFL_PR_NUMBER: ${{ inputs.item_number }} - with: - github-token: ${{ github.token }} - script: | - const fs = require('fs'); - const outputPath = process.env.GH_AW_AGENT_OUTPUT; - const expectedBase = process.env.SFL_EXPECTED_BASE; - const expectedHead = process.env.SFL_EXPECTED_HEAD; - const pullNumber = Number(process.env.SFL_PR_NUMBER); - if (!outputPath || !fs.existsSync(outputPath)) { - core.setFailed('SFL agent output is unavailable for thread resolution'); - return; - } - if (!/^[0-9a-f]{40}$/.test(expectedHead || '')) { - core.setFailed(`Invalid expected pull request head: ${expectedHead || 'missing'}`); - return; - } - if (!/^[0-9a-f]{40}$/.test(expectedBase || '')) { - core.setFailed(`Invalid expected pull request base: ${expectedBase || 'missing'}`); - return; - } - if (!Number.isInteger(pullNumber) || pullNumber <= 0) { - core.setFailed(`Invalid pull request number: ${process.env.SFL_PR_NUMBER}`); - return; - } - - const output = JSON.parse(fs.readFileSync(outputPath, 'utf8')); - const requests = (Array.isArray(output.items) ? output.items : []) - .filter(item => item.type === 'resolve_sfl_review_thread'); - const threadIds = [...new Set(requests.map(item => item.thread_id))]; - if (threadIds.length === 0) { - core.info('No obsolete SFL review threads requested for resolution'); - return; - } - if (threadIds.length > 30) { - core.setFailed(`Requested ${threadIds.length} thread resolutions; maximum is 30`); - return; - } - - const normalizeActorLogin = login => - (login || '').replace(/\[bot\]$/i, '').toLowerCase(); - const appLogin = normalizeActorLogin(process.env.SFL_APP_LOGIN); - if (!appLogin) { - core.setFailed('Could not resolve the SFL App identity'); - return; - } - const severityPrefix = - /^(🔴 \*\*CRITICAL|🟠 \*\*HIGH|🟡 \*\*MEDIUM|⚪ \*\*LOW) —/; - const unresolvedOutdatedThreadIds = []; - - for (const threadId of threadIds) { - if (typeof threadId !== 'string' || !threadId.startsWith('PRRT_')) { - core.setFailed(`Invalid pull request review thread ID: ${threadId}`); - return; - } - const data = await github.graphql( - `query($threadId: ID!) { - node(id: $threadId) { - ... on PullRequestReviewThread { - id - isOutdated - isResolved - pullRequest { - number - baseRefOid - headRefOid - repository { - nameWithOwner - } - } - comments(first: 1) { - nodes { - author { - login - __typename - } - body - path - } - } - } - } - }`, - { threadId } - ); - const thread = data.node; - const rootComment = thread?.comments?.nodes?.[0]; - const body = rootComment?.body || ''; - const path = rootComment?.path; - if ( - !thread || - thread.pullRequest?.repository?.nameWithOwner !== - `${context.repo.owner}/${context.repo.repo}` || - thread.pullRequest?.number !== pullNumber || - thread.pullRequest?.baseRefOid !== expectedBase || - thread.pullRequest?.headRefOid !== expectedHead || - rootComment?.author?.__typename !== 'Bot' || - normalizeActorLogin(rootComment.author.login) !== appLogin || - !severityPrefix.test(body) || - !body.includes('SFL Reviewer') || - typeof path !== 'string' || - path.length === 0 || - (!thread.isResolved && !thread.isOutdated) - ) { - core.setFailed( - `Thread ${threadId} is not an obsolete SFL finding on PR #${pullNumber} at base ${expectedBase} and head ${expectedHead}` - ); - return; - } - if (thread.isResolved) { - core.info(`SFL review thread ${threadId} is already resolved`); - continue; - } - try { - const { data: pathAtHead } = await github.rest.repos.getContent({ - owner: context.repo.owner, - repo: context.repo.repo, - path, - ref: expectedHead, - }); - if (Array.isArray(pathAtHead) || pathAtHead.type !== 'file') { - core.setFailed( - `Thread ${threadId} path ${path} is not a file at head ${expectedHead}` - ); - return; - } - } catch (error) { - if (error.status === 404) { - core.setFailed( - `Thread ${threadId} path ${path} no longer exists at head ${expectedHead}; human resolution is required` - ); - return; - } - throw error; - } - unresolvedOutdatedThreadIds.push(threadId); - } - - const failedThreadIds = []; - for (const threadId of unresolvedOutdatedThreadIds) { - let resolved = false; - let lastError; - for (let attempt = 1; attempt <= 3; attempt += 1) { - try { - const result = await github.graphql( - `mutation($threadId: ID!) { - resolveReviewThread(input: { threadId: $threadId }) { - thread { - id - isResolved - } - } - }`, - { threadId } - ); - if (result.resolveReviewThread?.thread?.isResolved) { - resolved = true; - break; - } - lastError = new Error('mutation did not return a resolved thread'); - } catch (error) { - lastError = error; - } - if (attempt < 3) { - await new Promise(resolve => setTimeout(resolve, attempt * 1000)); - } - } - if (!resolved) { - core.error( - `Failed to resolve SFL review thread ${threadId}: ${lastError?.message || 'unknown error'}` - ); - failedThreadIds.push(threadId); - continue; - } - core.info(`Resolved obsolete SFL review thread ${threadId}`); - } - if (failedThreadIds.length > 0) { - core.setFailed( - `Failed to resolve ${failedThreadIds.length} obsolete SFL review thread(s)` - ); - } - -jobs: - publish_review_provenance: - if: always() - runs-on: ubuntu-slim - outputs: - evidence-check-run-id: ${{ steps.initialize-evidence.outputs.check-run-id }} - permissions: - checks: write - contents: read - pull-requests: read - steps: - - name: Write immutable review dispatch provenance - env: - BASE_SHA: ${{ inputs.base_sha }} - HEAD_SHA: ${{ inputs.head_sha }} - PR_NUMBER: ${{ inputs.item_number }} - PROVENANCE_PATH: ${{ runner.temp }}/sfl-review-run-provenance.json - REPOSITORY: ${{ github.repository }} - REVIEW_EFFORT: ${{ env.SFL_REVIEW_EFFORT }} - RETRY_COUNT: ${{ inputs.retry_count }} - RUN_ID: ${{ github.run_id }} - run: | - set -euo pipefail - if ! [[ "$PR_NUMBER" =~ ^[1-9][0-9]*$ ]]; then - echo "::error::Invalid pull request number: ${PR_NUMBER}" - exit 1 - fi - if ! [[ "$HEAD_SHA" =~ ^[0-9a-f]{40}$ ]]; then - echo "::error::Invalid expected pull request head: ${HEAD_SHA}" - exit 1 - fi - if ! [[ "$BASE_SHA" =~ ^[0-9a-f]{40}$ ]]; then - echo "::error::Invalid expected pull request base: ${BASE_SHA}" - exit 1 - fi - if ! [[ "$RETRY_COUNT" =~ ^[01]$ ]]; then - echo "::error::Invalid review retry count: ${RETRY_COUNT}" - exit 1 - fi - case "$REVIEW_EFFORT" in - low|medium|high) ;; - *) - echo "::error::Invalid review effort: ${REVIEW_EFFORT}" - exit 1 - ;; - esac - jq -n \ - --arg repository "$REPOSITORY" \ - --arg workflow_run_id "$RUN_ID" \ - --argjson pr_number "$PR_NUMBER" \ - --arg base_sha "$BASE_SHA" \ - --arg head_sha "$HEAD_SHA" \ - --arg review_effort "$REVIEW_EFFORT" \ - --argjson retry_count "$RETRY_COUNT" \ - '{ - repository: $repository, - workflow_run_id: $workflow_run_id, - pr_number: $pr_number, - base_sha: $base_sha, - head_sha: $head_sha, - review_effort: $review_effort, - retry_count: $retry_count - }' > "$PROVENANCE_PATH" - - name: Initialize review evidence - id: initialize-evidence - uses: actions/github-script@3a2844b7e9c422d3c10d287c895573f7108da1b3 # v9.0.0 - env: - SFL_BASE_SHA: ${{ inputs.base_sha }} - SFL_HEAD_SHA: ${{ inputs.head_sha }} - SFL_PR_NUMBER: ${{ inputs.item_number }} - SFL_RUN_ID: ${{ github.run_id }} - with: - github-token: ${{ github.token }} - script: | - const baseSha = process.env.SFL_BASE_SHA; - const headSha = process.env.SFL_HEAD_SHA; - const pullNumber = Number(process.env.SFL_PR_NUMBER); - const runId = Number(process.env.SFL_RUN_ID); - if (!Number.isSafeInteger(runId) || runId <= 0) { - core.setFailed(`Invalid workflow run ID: ${process.env.SFL_RUN_ID}`); - return; - } - const { data: pullRequest } = await github.rest.pulls.get({ - owner: context.repo.owner, - repo: context.repo.repo, - pull_number: pullNumber, - }); - if ( - pullRequest.base.sha !== baseSha || - pullRequest.head.sha !== headSha - ) { - core.setFailed( - `Pull request changed from base ${baseSha} / head ${headSha} ` + - `to base ${pullRequest.base.sha} / head ${pullRequest.head.sha}` - ); - return; - } - const { data: evidence } = await github.rest.checks.create({ - owner: context.repo.owner, - repo: context.repo.repo, - name: 'SFL Review Evidence', - head_sha: headSha, - external_id: - `sfl-review:${pullNumber}:${baseSha}:${headSha}:${runId}`, - status: 'in_progress', - started_at: new Date().toISOString(), - output: { - title: 'SFL review is in progress', - summary: 'The immutable pull request state is being reviewed.', - text: 'replacement_retry_eligible=false', - }, - }); - core.setOutput('check-run-id', String(evidence.id)); - - name: Upload immutable review dispatch provenance - if: always() - uses: actions/upload-artifact@043fb46d1a93c77aae656e7c1c64a875d1fc6a0a # v7.0.1 - with: - name: sfl-review-run-provenance - path: ${{ runner.temp }}/sfl-review-run-provenance.json - if-no-files-found: error - retention-days: 1 - - review_metadata: - needs: [agent, publish_review_provenance, safe_outputs, resolve_sfl_review_thread] - if: always() - runs-on: ubuntu-slim - permissions: - checks: write - contents: read - pull-requests: read - steps: - - name: Verify expected pull request head - id: resolve-head - uses: actions/github-script@3a2844b7e9c422d3c10d287c895573f7108da1b3 # v9.0.0 - env: - SFL_EXPECTED_BASE: ${{ inputs.base_sha }} - SFL_EXPECTED_HEAD: ${{ inputs.head_sha }} - SFL_PR_NUMBER: ${{ inputs.item_number }} - with: - github-token: ${{ github.token }} - script: | - const pullNumber = Number(process.env.SFL_PR_NUMBER); - const expectedBase = process.env.SFL_EXPECTED_BASE; - const expectedHead = process.env.SFL_EXPECTED_HEAD; - if (!Number.isInteger(pullNumber) || pullNumber <= 0) { - core.setFailed(`Invalid pull request number: ${process.env.SFL_PR_NUMBER}`); - return; - } - if (!/^[0-9a-f]{40}$/.test(expectedHead || '')) { - core.setFailed(`Invalid expected pull request head: ${expectedHead || 'missing'}`); - return; - } - if (!/^[0-9a-f]{40}$/.test(expectedBase || '')) { - core.setFailed(`Invalid expected pull request base: ${expectedBase || 'missing'}`); - return; - } - - const { data: pullRequest } = await github.rest.pulls.get({ - owner: context.repo.owner, - repo: context.repo.repo, - pull_number: pullNumber, - }); - core.setOutput('head-sha', expectedHead); - if ( - pullRequest.base.sha !== expectedBase || - pullRequest.head.sha !== expectedHead - ) { - core.setFailed( - `Pull request changed from base ${expectedBase} / head ${expectedHead} to base ${pullRequest.base.sha} / head ${pullRequest.head.sha}` - ); - } - - - name: Download safe output items - if: needs.agent.result == 'success' && needs.safe_outputs.result == 'success' && (needs.resolve_sfl_review_thread.result == 'success' || needs.resolve_sfl_review_thread.result == 'skipped') - uses: actions/download-artifact@3e5f45b2cfb9172054b4087a40e8e0b5a5461e7c # v8.0.1 - with: - name: safe-outputs-items - path: /tmp/sfl-review-safe-outputs - - - name: Download reviewer agent output - if: needs.agent.result == 'success' && needs.safe_outputs.result == 'success' && (needs.resolve_sfl_review_thread.result == 'success' || needs.resolve_sfl_review_thread.result == 'skipped') - uses: actions/download-artifact@3e5f45b2cfb9172054b4087a40e8e0b5a5461e7c # v8.0.1 - with: - name: agent - path: /tmp/gh-aw - - - name: Generate GitHub App token - if: always() - id: app-token - uses: actions/create-github-app-token@bcd2ba49218906704ab6c1aa796996da409d3eb1 # v3.2.0 - with: - client-id: ${{ vars.SFL_APP_CLIENT_ID }} - private-key: ${{ secrets.SFL_APP_PRIVATE_KEY }} - owner: ${{ github.repository_owner }} - repositories: ${{ github.event.repository.name }} - permission-actions: read - permission-contents: read - permission-pull-requests: write - - name: Finalize review metadata - if: needs.agent.result == 'success' && needs.safe_outputs.result == 'success' && (needs.resolve_sfl_review_thread.result == 'success' || needs.resolve_sfl_review_thread.result == 'skipped') - id: finalize-review - uses: actions/github-script@3a2844b7e9c422d3c10d287c895573f7108da1b3 # v9.0.0 - env: - SFL_AIC: ${{ needs.agent.outputs.aic }} - SFL_EFFORT: ${{ env.SFL_REVIEW_EFFORT }} - SFL_EXPECTED_BASE: ${{ inputs.base_sha }} - SFL_EXPECTED_HEAD: ${{ inputs.head_sha }} - SFL_AGENT_OUTPUT: /tmp/gh-aw/agent_output.json - SFL_MODEL: ${{ needs.agent.outputs.model }} - SFL_PR_NUMBER: ${{ github.event.pull_request.number || inputs.item_number }} - SFL_RUN_ID: ${{ github.run_id }} - SFL_SAFE_OUTPUT_ITEMS: /tmp/sfl-review-safe-outputs/safe-output-items.jsonl - with: - github-token: ${{ steps.app-token.outputs.token }} - script: | - // Keep these helpers inline so the HemSoft deployer can install a - // self-contained workflow without additional script dependencies. - function formatElapsed(startedAt, submittedAt) { - if (!Number.isFinite(startedAt) || !Number.isFinite(submittedAt)) { - return 'Unavailable'; - } - const elapsedSeconds = Math.max(0, Math.round((submittedAt - startedAt) / 1000)); - const hours = Math.floor(elapsedSeconds / 3600); - const minutes = Math.floor((elapsedSeconds % 3600) / 60); - const seconds = elapsedSeconds % 60; - return hours > 0 - ? `${hours}h ${minutes}m ${seconds}s` - : `${minutes}m ${seconds}s`; - } - - function formatModel(modelId) { - const [baseModel, query = ''] = modelId.split('?', 2); - const effort = new URLSearchParams(query).get('effort') || - (process.env.SFL_EFFORT || '').trim(); - const claude = baseModel.match(/^claude-(opus|sonnet|haiku)-(.+)$/); - let displayName = baseModel; - if (claude) { - displayName = `${claude[1][0].toUpperCase()}${claude[1].slice(1)} ${claude[2]}`; - } else if (baseModel.startsWith('gpt-')) { - const parts = baseModel.slice(4).split('-'); - displayName = `GPT-${parts.shift()}${parts.length ? ` ${parts.map(part => part[0].toUpperCase() + part.slice(1)).join(' ')}` : ''}`; - } - return effort - ? `${displayName} (${effort[0].toUpperCase()}${effort.slice(1)} effort)` - : displayName; - } - - function formatAic(rawAic) { - const value = rawAic.trim() === '' ? Number.NaN : Number(rawAic); - return Number.isFinite(value) - ? value.toFixed(1).replace(/\.0$/, '') - : 'Unavailable'; - } - - async function countUnresolvedSflFindings({ - owner, - repo, - pullNumber, - reviewAuthor, - }) { - const normalizeActorLogin = login => - (login || '').replace(/\[bot\]$/i, '').toLowerCase(); - const severityPrefix = - /^(🔴 \*\*CRITICAL|🟠 \*\*HIGH|🟡 \*\*MEDIUM|⚪ \*\*LOW) —/; - let cursor = null; - let unresolvedCount = 0; - do { - const data = await github.graphql( - `query($owner: String!, $repo: String!, $pullNumber: Int!, $cursor: String) { - repository(owner: $owner, name: $repo) { - pullRequest(number: $pullNumber) { - reviewThreads(first: 100, after: $cursor) { - nodes { - isResolved - comments(first: 1) { - nodes { - author { - login - __typename - } - body - } - } - } - pageInfo { - hasNextPage - endCursor - } - } - } - } - }`, - { owner, repo, pullNumber, cursor } - ); - const threads = data.repository?.pullRequest?.reviewThreads; - if (!threads) { - throw new Error('Pull request review threads were unavailable'); - } - for (const thread of threads.nodes || []) { - const rootComment = thread.comments?.nodes?.[0]; - const body = rootComment?.body || ''; - if ( - !thread.isResolved && - rootComment?.author?.__typename === 'Bot' && - normalizeActorLogin(rootComment.author.login) === - normalizeActorLogin(reviewAuthor) && - severityPrefix.test(body) && - body.includes('SFL Reviewer') - ) { - unresolvedCount += 1; - } - } - cursor = threads.pageInfo.hasNextPage - ? threads.pageInfo.endCursor - : null; - } while (cursor); - return unresolvedCount; - } - - const owner = context.repo.owner; - const repo = context.repo.repo; - const pullNumber = Number(process.env.SFL_PR_NUMBER); - const runId = Number(process.env.SFL_RUN_ID); - const expectedBase = process.env.SFL_EXPECTED_BASE; - const expectedHead = process.env.SFL_EXPECTED_HEAD; - if (!Number.isInteger(pullNumber) || pullNumber <= 0) { - core.setFailed(`Invalid pull request number: ${process.env.SFL_PR_NUMBER}`); - return; - } - if (!Number.isInteger(runId) || runId <= 0) { - core.setFailed(`Invalid workflow run ID: ${process.env.SFL_RUN_ID}`); - return; - } - if (!/^[0-9a-f]{40}$/.test(expectedHead || '')) { - core.setFailed(`Invalid expected pull request head: ${expectedHead || 'missing'}`); - return; - } - if (!/^[0-9a-f]{40}$/.test(expectedBase || '')) { - core.setFailed(`Invalid expected pull request base: ${expectedBase || 'missing'}`); - return; - } - - const { data: pullRequest } = await github.rest.pulls.get({ - owner, - repo, - pull_number: pullNumber, - }); - if ( - pullRequest.base.sha !== expectedBase || - pullRequest.head.sha !== expectedHead - ) { - core.setFailed( - `Pull request changed from base ${expectedBase} / head ${expectedHead} to base ${pullRequest.base.sha} / head ${pullRequest.head.sha}` - ); - return; - } - - const fs = require('fs'); - const safeOutputItemsPath = process.env.SFL_SAFE_OUTPUT_ITEMS; - let safeOutputItems; - try { - safeOutputItems = fs - .readFileSync(safeOutputItemsPath, 'utf8') - .split(/\r?\n/) - .filter(Boolean) - .map(line => JSON.parse(line)); - } catch (error) { - core.setFailed( - `Could not read safe-output review metadata: ${error.message}` - ); - return; - } - let agentItems = []; - let replacementRetryEligible = false; - try { - const agentOutput = JSON.parse( - fs.readFileSync(process.env.SFL_AGENT_OUTPUT, 'utf8') - ); - agentItems = Array.isArray(agentOutput) - ? agentOutput - : (agentOutput.items || []); - if (!Array.isArray(agentItems)) { - throw new Error('agent items are not an array'); - } - replacementRetryEligible = !agentItems.some(item => - item.type === 'missing_data' || - item.type === 'missing_tool' || - item.type === 'report_incomplete' - ); - } catch (error) { - core.warning( - `Could not inspect terminal review signals; suppressing replacement: ${error.message}` - ); - } - core.setOutput( - 'replacement-retry-eligible', - replacementRetryEligible - ); - const agentSubmittedReviews = agentItems.filter( - item => item.type === 'submit_pull_request_review' - ); - if (agentSubmittedReviews.length !== 1) { - core.setFailed( - `Expected exactly one immutable agent submitted-review item, found ${agentSubmittedReviews.length}` - ); - return; - } - const [agentSubmittedReview] = agentSubmittedReviews; - // gh-aw preserves the agent's raw tool arguments; on a - // fixed-target workflow an omitted repo/pull_request_number means - // the configured pull request. Default omitted targets while - // still rejecting explicitly mismatched ones. - const configuredRepo = `${owner}/${repo}`; - if ( - (agentSubmittedReview.repo ?? configuredRepo) !== configuredRepo || - Number(agentSubmittedReview.pull_request_number ?? pullNumber) !== pullNumber - ) { - core.setFailed('Immutable agent submitted-review target does not match this pull request'); - return; - } - const severityPrefix = - /^(🔴 \*\*CRITICAL|🟠 \*\*HIGH|🟡 \*\*MEDIUM|⚪ \*\*LOW) —/; - const agentReviewComments = agentItems.filter( - item => item.type === 'create_pull_request_review_comment' - ); - if (agentReviewComments.some(item => - (item.repo ?? configuredRepo) !== configuredRepo || - Number(item.pull_request_number ?? pullNumber) !== pullNumber || - !severityPrefix.test(item.body || '') - )) { - core.setFailed('Immutable agent review comments contain an invalid target or finding format'); - return; - } - const publishedReviewComments = safeOutputItems.filter( - item => item.type === 'create_pull_request_review_comment' - ); - if (publishedReviewComments.some(item => - item.repo !== `${owner}/${repo}` || - Number(item.number) !== pullNumber - )) { - core.setFailed('Published review-comment manifest target does not match this pull request'); - return; - } - if (publishedReviewComments.length !== agentReviewComments.length) { - core.setFailed( - `Published review-comment count ${publishedReviewComments.length} does not match immutable agent count ${agentReviewComments.length}` - ); - return; - } - const submittedReviews = safeOutputItems.filter( - item => item.type === 'submit_pull_request_review' - ); - if (submittedReviews.length !== 1) { - core.setFailed( - `Expected exactly one submitted-review manifest item, found ${submittedReviews.length}` - ); - return; - } - const [submittedReview] = submittedReviews; - if ( - submittedReview.repo !== `${owner}/${repo}` || - Number(submittedReview.number) !== pullNumber - ) { - core.setFailed('Submitted-review manifest target does not match this pull request'); - return; - } - const reviewId = Number(submittedReview.metadata?.review_id); - if (!Number.isInteger(reviewId) || reviewId <= 0) { - core.setFailed('Submitted-review manifest does not contain a valid review ID'); - return; - } - const { data: review } = await github.rest.pulls.getReview({ - owner, - repo, - pull_number: pullNumber, - review_id: reviewId, - }); - if (!review.commit_id || review.commit_id !== expectedHead) { - core.setFailed( - `SFL review commit ${review.commit_id || 'missing'} does not match expected head ${expectedHead}` - ); - return; - } - - const { data: run } = await github.rest.actions.getWorkflowRun({ - owner, - repo, - run_id: runId, - }); - const startedAt = Date.parse(run.run_started_at || run.created_at); - const submittedAt = Date.parse(review.submitted_at); - const elapsed = formatElapsed(startedAt, submittedAt); - const model = formatModel(process.env.SFL_MODEL || 'unknown'); - const aic = formatAic(process.env.SFL_AIC || ''); - - const originalBody = agentSubmittedReview.body || ''; - for (const placeholder of [ - 'SFL_MODEL_PENDING', - 'SFL_TIME_PENDING', - 'SFL_AIC_PENDING', - ]) { - const occurrences = originalBody.split(placeholder).length - 1; - if (occurrences !== 1) { - core.setFailed( - `Review body must contain ${placeholder} exactly once; found ${occurrences}` - ); - return; - } - } - const templatePlaceholders = [ - '{verdict_icon}', - '{VERDICT}', - '{verdict_reason}', - '{security_critical_or_dash}', - '{security_high_or_dash}', - '{security_medium_or_dash}', - '{security_low_or_dash}', - '{accuracy_critical_or_dash}', - '{accuracy_high_or_dash}', - '{accuracy_medium_or_dash}', - '{accuracy_low_or_dash}', - '{quality_maintainability_critical_or_dash}', - '{quality_maintainability_high_or_dash}', - '{quality_maintainability_medium_or_dash}', - '{quality_maintainability_low_or_dash}', - '{total_critical_or_dash}', - '{total_high_or_dash}', - '{total_medium_or_dash}', - '{total_low_or_dash}', - '{Top 3 most impactful current findings with their severity prefixes, or "No findings."}', - ]; - const unresolvedPlaceholder = templatePlaceholders.find(placeholder => - originalBody.includes(placeholder) - ); - if (unresolvedPlaceholder) { - core.setFailed( - `Review body contains unresolved template placeholder ${unresolvedPlaceholder}` - ); - return; - } - - const body = originalBody - .replaceAll('SFL_MODEL_PENDING', model) - .replaceAll('SFL_TIME_PENDING', elapsed) - .replaceAll('SFL_AIC_PENDING', aic); - - const verdictApproved = body.includes('## ✅ Verdict: APPROVE'); - const verdictNeedsWork = body.includes('## ⚠️ Verdict: NEEDS WORK'); - if (verdictApproved === verdictNeedsWork) { - core.setFailed('Review body must contain exactly one recognized SFL verdict'); - return; - } - - const prAuthor = pullRequest.user?.login; - const reviewAuthor = review.user?.login; - if (!reviewAuthor) { - core.setFailed('SFL review author identity is unavailable'); - return; - } - const selfAuthored = Boolean(prAuthor) && prAuthor === reviewAuthor; - const expectedReviewEvent = verdictApproved && !selfAuthored - ? 'APPROVE' - : 'COMMENT'; - const expectedReviewState = verdictApproved && !selfAuthored - ? 'APPROVED' - : 'COMMENTED'; - if ((agentSubmittedReview.event || '').toUpperCase() !== expectedReviewEvent) { - core.setFailed( - `Immutable agent review event ${agentSubmittedReview.event || 'missing'} does not match expected event ${expectedReviewEvent}` - ); - return; - } - if ( - (submittedReview.metadata?.review_event || '').toUpperCase() !== - expectedReviewEvent || - submittedReview.metadata?.review_state !== expectedReviewState - ) { - core.setFailed('Published review manifest does not match the immutable agent verdict'); - return; - } - if (review.state !== expectedReviewState) { - core.setFailed( - `Review state ${review.state || 'missing'} does not match expected state ${expectedReviewState}` - ); - return; - } - - // GitHub accepts body-only updates to submitted COMMENTED reviews - // when authenticated as the app that authored the review. - await github.request('PUT /repos/{owner}/{repo}/pulls/{pull_number}/reviews/{review_id}', { - owner, - repo, - pull_number: pullNumber, - review_id: review.id, - body, - }); - - let publishedFindingCount; - try { - const reviewComments = await github.paginate( - 'GET /repos/{owner}/{repo}/pulls/{pull_number}/reviews/{review_id}/comments', - { - owner, - repo, - pull_number: pullNumber, - review_id: review.id, - per_page: 100, - } - ); - const inlineRunMarker = `[Run ${runId}]`; - publishedFindingCount = reviewComments.filter(comment => - severityPrefix.test(comment.body || '') && - comment.body?.includes(inlineRunMarker) - ).length; - } catch (error) { - core.setFailed(`Could not enumerate SFL findings: ${error.message}`); - return; - } - const immutableFindingCount = agentReviewComments.length; - if (publishedFindingCount !== immutableFindingCount) { - core.setFailed( - `Published finding count ${publishedFindingCount} does not match immutable agent count ${immutableFindingCount}` - ); - return; - } - - let unresolvedFindingCount; - try { - unresolvedFindingCount = await countUnresolvedSflFindings({ - owner, - repo, - pullNumber, - reviewAuthor, - }); - } catch (error) { - core.setFailed(`Could not enumerate unresolved SFL findings: ${error.message}`); - return; - } - - const gateApproved = - verdictApproved && - immutableFindingCount === 0 && - unresolvedFindingCount === 0; - core.setOutput('head-sha', expectedHead); - core.setOutput('gate-approved', gateApproved); - core.setOutput('finding-count', unresolvedFindingCount); - core.setOutput('new-finding-count', immutableFindingCount); - core.setOutput('review-matches-head', true); - core.setOutput('review-state', review.state); - - name: Finalize SFL review evidence - if: always() - uses: actions/github-script@3a2844b7e9c422d3c10d287c895573f7108da1b3 # v9.0.0 - env: - SFL_BASE_SHA: ${{ inputs.base_sha }} - SFL_EVIDENCE_CHECK_RUN_ID: ${{ needs.publish_review_provenance.outputs.evidence-check-run-id }} - SFL_HEAD_SHA: ${{ inputs.head_sha }} - SFL_GATE_APPROVED: ${{ steps.finalize-review.outputs.gate-approved }} - SFL_FINDING_COUNT: ${{ steps.finalize-review.outputs.finding-count }} - SFL_REPLACEMENT_RETRY_ELIGIBLE: ${{ steps.finalize-review.outputs.replacement-retry-eligible }} - SFL_REVIEW_MATCHES_HEAD: ${{ steps.finalize-review.outputs.review-matches-head }} - SFL_REVIEW_STATE: ${{ steps.finalize-review.outputs.review-state }} - SFL_PR_NUMBER: ${{ inputs.item_number }} - SFL_RUN_ID: ${{ github.run_id }} - SFL_UPSTREAM_RESULT: agent=${{ needs.agent.result }}, safe_outputs=${{ needs.safe_outputs.result }}, thread_resolution=${{ needs.resolve_sfl_review_thread.result }} - with: - github-token: ${{ github.token }} - script: | - const baseSha = process.env.SFL_BASE_SHA; - const headSha = process.env.SFL_HEAD_SHA; - if (!/^[0-9a-f]{40}$/.test(baseSha || '')) { - core.setFailed(`Cannot publish SFL approval gate for invalid base SHA: ${baseSha || 'missing'}`); - return; - } - if (!/^[0-9a-f]{40}$/.test(headSha || '')) { - core.setFailed(`Cannot publish SFL approval gate for invalid head SHA: ${headSha || 'missing'}`); - return; - } - const pullNumber = Number(process.env.SFL_PR_NUMBER); - const runId = Number(process.env.SFL_RUN_ID); - if (!Number.isSafeInteger(runId) || runId <= 0) { - core.setFailed(`Invalid workflow run ID: ${process.env.SFL_RUN_ID}`); - return; - } - const { data: pullRequest } = await github.rest.pulls.get({ - owner: context.repo.owner, - repo: context.repo.repo, - pull_number: pullNumber, - }); - const baseMatchesExpected = pullRequest.base.sha === baseSha; - const headMatchesExpected = pullRequest.head.sha === headSha; - const gateApproved = process.env.SFL_GATE_APPROVED === 'true'; - const replacementRetryEligible = - process.env.SFL_REPLACEMENT_RETRY_ELIGIBLE === 'true'; - const evidenceCheckRunId = - Number(process.env.SFL_EVIDENCE_CHECK_RUN_ID); - const gateExternalId = - `sfl-review:${pullNumber}:${baseSha}:${headSha}:${runId}`; - if (!Number.isSafeInteger(evidenceCheckRunId) || evidenceCheckRunId <= 0) { - core.setFailed('Cannot finalize missing SFL review evidence'); - return; - } - const { data: initializedEvidence } = await github.rest.checks.get({ - owner: context.repo.owner, - repo: context.repo.repo, - check_run_id: evidenceCheckRunId, - }); - if ( - initializedEvidence.name !== 'SFL Review Evidence' || - initializedEvidence.head_sha !== headSha || - initializedEvidence.external_id !== gateExternalId || - initializedEvidence.app?.id !== 15368 - ) { - core.setFailed('Initialized SFL review evidence does not match this run'); - return; - } - const publishEvidence = async ({ approved, summary }) => { - let lastError; - for (let attempt = 1; attempt <= 3; attempt += 1) { - try { - return await github.rest.checks.update({ - owner: context.repo.owner, - repo: context.repo.repo, - check_run_id: evidenceCheckRunId, - name: 'SFL Review Evidence', - external_id: gateExternalId, - status: 'completed', - conclusion: approved ? 'success' : 'failure', - completed_at: new Date().toISOString(), - output: { - title: approved - ? 'SFL zero-finding approval gate satisfied' - : 'SFL Reviewer approval is required', - summary, - text: - `replacement_retry_eligible=${replacementRetryEligible}`, - }, - }); - } catch (error) { - lastError = error; - if (attempt < 3) { - core.warning( - `Evidence finalization attempt ${attempt} failed: ${error.message}` - ); - await new Promise(resolve => setTimeout(resolve, attempt * 1000)); - } - } - } - throw lastError; - }; - const publishFailure = async ({ reason }) => { - await publishEvidence({ - approved: false, - summary: - `${reason}. The review cannot satisfy the approval gate. ` + - `Upstream results: ${process.env.SFL_UPSTREAM_RESULT}.`, - }); - core.setFailed(reason); - }; - if (!baseMatchesExpected || !headMatchesExpected) { - await publishFailure({ - reason: - `Pull request changed from base ${baseSha} / head ${headSha} ` + - `to base ${pullRequest.base.sha} / head ${pullRequest.head.sha}`, - }); - return; - } - const summary = gateApproved - ? 'The latest SFL review approved this head with zero inline findings.' - : `The latest SFL review state is ${process.env.SFL_REVIEW_STATE || 'unavailable'} with ${process.env.SFL_FINDING_COUNT || 'unknown'} unresolved SFL finding thread(s), expected-head review match ${process.env.SFL_REVIEW_MATCHES_HEAD || 'unknown'}, live-base match ${baseMatchesExpected}, and live-head match ${headMatchesExpected}, so it does not satisfy the zero-finding approval gate. Upstream results: ${process.env.SFL_UPSTREAM_RESULT}.`; - await publishEvidence({ - approved: gateApproved, - summary, - }); - if (!gateApproved) { - core.setFailed(summary); - } ---- - -## Full-Spectrum PR Review - -## Required Configuration - -This workflow uses a GitHub App for safe-outputs (posting inline review -comments). The following must be configured in the target repository: - -| Kind | Name | Description | -| ---- | ---- | ----------- | -| Variable | `SFL_APP_CLIENT_ID` | Client ID of the GitHub App used for safe-outputs authentication | -| Secret | `SFL_APP_PRIVATE_KEY` | Private key (PEM) of the GitHub App used to mint installation tokens | -| Secret | `OPENROUTER_API_KEY` | HemSoft OpenRouter credential used to run Kimi K3 | - -Set these under **Settings → Secrets and variables → Actions** in the target -repository. If they are missing, the token minting step will fail and review -comments will not be posted. - -**Required Permissions**: Automatic reviews are dispatched by -`sfl-pr-review-auto.yml` with an SFL GitHub App installation token. Humans can -request an explicit rerun with: - -```bash -gh workflow run sfl-pr-review.lock.yml \ - -f item_number=PR_NUMBER \ - -f base_sha=PULL_REQUEST_BASE_SHA \ - -f head_sha=PULL_REQUEST_HEAD_SHA \ - -f review_effort=low \ - -f 'aw_context={"item_type":"pull_request","item_number":PR_NUMBER,"base_sha":"PULL_REQUEST_BASE_SHA","head_sha":"PULL_REQUEST_HEAD_SHA"}' -``` - ---- - -You are an expert code reviewer performing a comprehensive 3-pass analysis of -this pull request. Your goal is to surface **real issues** — bugs, security -vulnerabilities, logic errors, and architectural concerns — not style nits. - -## Setup - -1. Get the PR diff and changed files: - - ```bash - gh pr diff ${{ inputs.item_number }} --repo ${{ github.repository }} - ``` - -2. Get the list of changed files: - - ```bash - gh pr view ${{ inputs.item_number }} --repo ${{ github.repository }} --json files --jq '.files[].path' - ``` - -3. Read the full content of each changed file to understand context beyond the - diff (imports, class structure, related functions). - -4. Treat `${{ inputs.base_sha }}` and `${{ inputs.head_sha }}` as the immutable - expected base and head for this run. Read the live pull request base and head - before reviewing. If either differs, stop without posting review output; the - changed pull request state requires its own SFL run. - -5. List the pull request's unresolved review threads before creating comments. - Re-evaluate every unresolved SFL finding against the expected head. A prior - finding is an unresolved inline comment authored by the SFL reviewer whose - body starts with one of the exact severity prefixes below and contains the - `SFL Reviewer` run footer. Carry forward findings that still apply. Exclude - resolved findings. For every unresolved SFL finding that no longer applies - and whose thread GitHub reports as outdated, call - `resolve_sfl_review_thread` with its thread ID before submitting the current - review. Never request resolution for a live unresolved thread; carry it - forward until a maintainer resolves it or a later commit makes it outdated. - -## Review Passes - -Execute all 3 passes sequentially. For each finding, post an **inline review -comment** on the specific line using the `create_pull_request_review_comment` -tool. - -### Severity Accounting - -Every inline comment must represent **exactly one finding** and begin with -exactly one of these prefixes, replacing `{Pass}` with `Security`, -`Accuracy`, or `Quality & Maintainability`: - -| Severity | Prefix | Definition | -| ---------- | -------- | ------------ | -| Critical | `🔴 **CRITICAL — {Pass}**` | Exploitable security issue, data loss, systemic outage, or release-blocking defect | -| High | `🟠 **HIGH — {Pass}**` | Likely production defect, serious security weakness, or major regression | -| Medium | `🟡 **MEDIUM — {Pass}**` | Material correctness, maintainability, or operational issue that should be fixed | -| Low | `⚪ **LOW — {Pass}**` | Minor but actionable improvement with limited risk | - -Apply this same severity scale to all three passes. Do not combine multiple -findings into one inline comment. Maintain a ledger containing both new -findings successfully posted in this run and still-applicable unresolved SFL -findings carried from earlier runs, grouped by pass and severity. Do not post a -duplicate inline comment for a carried finding. - -For each new finding, pass `side: RIGHT` for an added or context line and -`side: LEFT` for a deleted line. Always pass the side explicitly. End every new -inline finding with this run marker: - -```markdown ---- -*SFL Reviewer • {Pass} • {Severity} • [Run ${{ github.run_id }}](${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }})* -``` - -The run marker makes findings from repeated reviews distinguishable. New -findings use the current run ID; carried findings retain their original run -marker and are counted through the unresolved-finding ledger. - ---- - -### Pass 1: 🔒 Security Review - -Analyze for security vulnerabilities using OWASP Top 10 and CWE Top 25 as -the baseline. Look for: - -| Category | Examples | -| ---------- | ---------- | -| Injection | SQL injection, command injection, XSS, SSTI | -| Authentication | Broken auth flows, weak token handling, session issues | -| Authorization | Missing access control, privilege escalation, IDOR | -| Data exposure | PII leaks, sensitive data in logs, missing encryption | -| Configuration | Hardcoded secrets, debug mode in prod, insecure defaults | -| Dependencies | Known vulnerable packages, outdated critical deps | -| Cryptography | Weak algorithms, improper key management, predictable random | -| Input validation | Missing validation, type confusion, buffer overflow | - -Only report findings with **>80% confidence** of exploitability. Do not -flag theoretical risks that require implausible attack vectors. - ---- - -### Pass 2: ✅ Accuracy & Reliability Review - -Trace the changed behavior from inputs and state through outputs and failure -paths. Look for demonstrable defects: - -| Area | What to check | -| ------ | --------------- | -| Logic | Incorrect conditions, calculations, ordering, or control flow | -| Edge cases | Empty, null, boundary, overflow, timeout, and partial-success behavior | -| State | Invalid transitions, stale state, race conditions, or non-idempotent retries | -| Error handling | Swallowed errors, incorrect recovery, misleading success, or missing cleanup | -| Data integrity | Lost, duplicated, corrupted, or partially committed data | -| Contracts | Implementations that violate explicit API, type, or behavioral contracts | - -Only report a finding when you can explain a concrete input/state sequence and -the incorrect result it produces. Do not speculate about hypothetical runtime -conditions without a credible path through the changed code. - ---- - -### Pass 3: 🧰 Quality & Maintainability Review - -Evaluate whether the change remains understandable, modifiable, and testable: - -| Area | What to check | -| ------ | --------------- | -| Cohesion | Functions or modules with unrelated responsibilities | -| Coupling | Hidden dependencies, brittle cross-module knowledge, or leaky abstractions | -| Duplication | Independently maintained logic that can drift or contradict itself | -| Complexity | Control flow or state handling that obscures behavior and increases defect risk | -| API clarity | Ambiguous contracts, surprising side effects, or inconsistent interfaces | -| Testability | Design choices that prevent important behavior from being isolated or verified | - -Do **not** report formatting, import ordering, bracket placement, comment style, -or naming that follows repository conventions. Do not calculate or estimate -CRAP, coverage, scorecard, or other numeric scores without measured repository -artifacts. - -### Conditional Evidence Checks - -Use these only when the changed files provide direct evidence: - -- **Tests**: Flag a gap only when changed behavior can be mapped to an existing - test seam and a concrete regression case is missing. -- **Compatibility**: Check only when the PR changes a public API, schema, - configuration key, serialized format, or persisted data contract. -- **Performance**: Report only concrete static problems such as N+1 queries, - unbounded work, blocking I/O on a known hot path, or avoidable large - allocations. -- **Migrations/rollout**: Check only when migration or deployment artifacts are - changed and a specific forward/rollback failure is visible. - -Classify applicable findings under Accuracy or Quality & Maintainability. If -evidence is unavailable, do not create a finding. - ---- - -## Communication quality - -Run the embedded Unslop pass on every user-facing sentence before each -`create_pull_request_review_comment` or `submit_pull_request_review` call. This -includes inline findings, the verdict reason, key findings, and any explanation -of the self-approval exception. Preserve the technical meaning, evidence, -severity, location, remediation, counts, and verdict. - -Protocol literals override the Unslop rules. Keep the required severity -prefixes, verdict headings, summary heading, table labels, placeholders, run -markers, and footer exactly as specified, including their emoji, bold text, and -em dashes. Apply the rules below only to the natural-language prose around -those literals. - -### Embedded Unslop pass - -1. Scan the draft for the patterns below. -2. Rewrite it without changing technical meaning or evidence. -3. Add a direct human voice where it helps clarity. -4. Ask, "What makes this obviously AI generated?" Fix any remaining tells. - -Use these writing rules: - -- State what happened. Cut puffery, promotional language, vague claims, and - formulaic framing. -- Name the source of a claim or remove the attribution. Do not write "experts - believe" or "reports suggest" without a source. -- Remove superficial `-ing` phrases that add no evidence. -- Prefer plain words. Avoid AI stock words such as "additionally", "crucial", - "delve", "enhance", "foster", "intricate", "pivotal", "showcase", - "tapestry", "testament", "underscore", and abstract uses of "landscape". -- Use "is" and "has" instead of inflated substitutes such as "serves as", - "stands as", "boasts", or "features". -- Do not use "not just X, but Y", forced groups of three, synonym cycling, or - false "from X to Y" ranges. -- Do not use em dashes in free-form prose. Split the sentence or use a comma. -- Use colons only when they introduce a real list or example. -- Avoid decorative bold text, decorative emoji, title-case headings, and curly - quotes in free-form prose. -- Remove chatbot filler, cutoff disclaimers, sycophancy, generic conclusions, - and phrases such as "I hope this helps", "Of course", or "Great question". -- Cut filler. Use "to" instead of "in order to" and "because" instead of "due - to the fact that". Remove "it is important to note that". -- Reduce hedging to the uncertainty the evidence requires. -- Replace abstract metaphor jargon such as "substrate", "wedge", "vector", - "locus", "nexus", "primitive", "harness", "surface", "bedrock", - "scaffolding", "modality", "paradigm", "gold-plating", "ratchet", - "evacuate", "endgame", "north star", and "flywheel" with concrete terms. -- Say what the code does and what fails. Do not substitute mood or feeling for - a mechanism, condition, path, command, or measured result. -- Keep one idea per sentence. Split sentences that require rereading. -- Prefer active voice. Name the actor when it matters. -- Cut adverbs that prop up weak verbs. -- Be specific and opinionated when the evidence supports it. Use "I" only when - it sounds natural in a review comment. -- Vary sentence length, but do not make the prose chatty or theatrical. - -Run this pass after the technical content is complete. It is a communication -filter, not permission to soften, omit, invent, or reclassify a finding. - ---- - -## Summary - -This is a hard completion contract. Do not end the run with prose-only output. -Before exiting, make exactly one successful `submit_pull_request_review` call. -If a defensible verdict cannot be produced because required evidence is -unavailable, call `missing_data` with the specific blocker instead; never call -`noop` and never silently stop. - -After all 3 passes, submit a **pull request review** using -`submit_pull_request_review` with: - -- **Event**: - - Use `APPROVE` when the verdict is `APPROVE` and the pull request author is - not `sfl-app[bot]`. - - Use `COMMENT` when the verdict is `NEEDS WORK`. - - Use `COMMENT` when the verdict is `APPROVE` but the pull request was authored - by `sfl-app[bot]`, because GitHub does not allow an app to approve - its own pull request. Explain this exception in the verdict reason. -- **Body**: A structured summary: - -```markdown -## {verdict_icon} Verdict: {VERDICT} - -{verdict_reason} - -## 🔨 Full-Spectrum Review Summary - -> **Review done by:** SFL_MODEL_PENDING
**Review time:** SFL_TIME_PENDING
**Token count:** SFL_AIC_PENDING AIC - -| Pass | 🔴 Critical | 🟠 High | 🟡 Medium | ⚪ Low | -|------|------------:|--------:|----------:|-------:| -| 🔒 Security | {security_critical_or_dash} | {security_high_or_dash} | {security_medium_or_dash} | {security_low_or_dash} | -| ✅ Accuracy | {accuracy_critical_or_dash} | {accuracy_high_or_dash} | {accuracy_medium_or_dash} | {accuracy_low_or_dash} | -| 🧰 Quality & Maintainability | {quality_maintainability_critical_or_dash} | {quality_maintainability_high_or_dash} | {quality_maintainability_medium_or_dash} | {quality_maintainability_low_or_dash} | -| **Total** | **{total_critical_or_dash}** | **{total_high_or_dash}** | **{total_medium_or_dash}** | **{total_low_or_dash}** | - -### Key Findings -{Top 3 most impactful current findings with their severity prefixes, or "No findings."} - ---- -*HemSoft full-spectrum review triggered automatically or by workflow dispatch • 3 evidence-based passes • [Run details](${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }}) • SFL run ID: ${{ github.run_id }}* -``` - -Choose the verdict deterministically: - -- If any new or still-applicable unresolved Critical, High, Medium, or Low - finding exists, use - `## ⚠️ Verdict: NEEDS WORK` and explain that every actionable finding must be - resolved before merge. -- Use `## ✅ Verdict: APPROVE` only when all four severity totals are zero. - -Before submitting, reconcile the ledger against new inline comments and -carried unresolved findings: - -1. Count each successfully posted current-run inline finding exactly once. -2. Count each still-applicable unresolved SFL finding from an earlier run - exactly once without duplicating its inline comment. -3. Resolve every earlier SFL thread that no longer applies and GitHub reports - as outdated. Never request resolution for a live unresolved thread. Never - approve while any SFL finding thread remains unresolved. -4. Never count resolved findings or duplicate reports of the same defect. -5. Every counted finding must have exactly one severity prefix. -6. Each table cell must equal the union of new and carried findings with that - exact pass and severity prefix. -7. The Total row must equal the column sums and the complete current finding - set. -8. Do not count positive observations, failed comment attempts, or summary-only - notes as findings. -9. Render every zero count as an em dash (`—`). Render only nonzero counts as - numbers. Keep the underlying numeric totals for verdict calculation. -10. Copy the `SFL_MODEL_PENDING`, `SFL_TIME_PENDING`, and `SFL_AIC_PENDING` - placeholders exactly. A post-review job replaces them with finalized values. -11. Replace every other `{...}` template field with its actual value. Never - submit literal brace or angle-bracket placeholders. -12. **Match event to verdict**: An `APPROVE` verdict must submit an `APPROVE` - event unless GitHub's self-approval restriction applies. Never submit a - `COMMENT` event for an eligible `APPROVE` verdict. - -## Rules - -1. **Be precise**: Comment on exact lines, not general areas. -2. **Be actionable**: Every finding must include a specific fix or recommendation. -3. **One comment, one finding**: Never combine independently actionable issues. -4. **Severity is mandatory**: Begin every inline finding with exactly one - severity prefix from the Severity Accounting table. -5. **Be honest**: If confidence is below the pass threshold, do not post it. -6. **No false positives**: Better to miss a minor issue than flag a non-issue. -7. **Cross-reference**: If a finding in one pass relates to another, mention it. -8. **Respect context**: Read the full file, not just the diff. Understand the - codebase patterns before flagging "violations." -9. **Pin the review**: All comments and the summary review must target - `${{ inputs.head_sha }}` and apply only to base `${{ inputs.base_sha }}`. - Never target a later pull request head or a different base. -10. **Close obsolete threads**: Resolve prior SFL threads only after verifying - their findings no longer apply to the expected head and GitHub reports the - thread as outdated. Never request resolution for a live unresolved thread. -11. **Accepted gate trust boundary**: The reviewer initializes and finalizes - one `SFL Review Evidence` check on the immutable head with the - repository-scoped `GITHUB_TOKEN` and exact - `sfl-review::::` locator. Because check fields are - caller-controlled, neither App ID nor `external_id` authorizes a merge. The - branch protection requires the GitHub Actions-owned native - immutable-head `SFL Reviewer Approval` check; the default-branch - `sfl-pr-review-auto.yml` gate runner authenticates the exact reviewer - through the Actions API and publishes that check with the - repository-scoped `GITHUB_TOKEN` only when the exact run succeeds. - Maintainers who can modify workflows on the protected default branch are - explicitly trusted under this first-version model. Defending against those - maintainers requires a central publisher or OIDC broker and is deliberately - out of scope. Still file any change that weakens this boundary, such as - trusting check fields without Actions-run validation, dropping PR/base/head - matching, or replacing the required workflow with a reusable head status. diff --git a/.github/workflows/validate-gh-sfl.yml b/.github/workflows/validate-gh-sfl.yml index 42b3adfb..b89cda83 100644 --- a/.github/workflows/validate-gh-sfl.yml +++ b/.github/workflows/validate-gh-sfl.yml @@ -50,7 +50,7 @@ jobs: run: go vet ./... - name: Test - run: go test ./... + run: go test -count=1 ./... - name: Test GitHub App credential bootstrap shell: pwsh diff --git a/CATALOG.md b/CATALOG.md index d57fe9e2..1e670330 100644 --- a/CATALOG.md +++ b/CATALOG.md @@ -20,10 +20,10 @@ Workflows listed here have graduated from staging (`.github/workflows/`) and are | Tier | Workflows | Infrastructure | Use Case | |------|-----------|---------------|----------| -| **review** | sfl-pr-review | sfl-pr-review-auto, sfl-pr-review-recovery | Automatic, recoverable native PR reviews with explicit reruns through `sfl-review` | +| **review** | — | sfl-pr-review-auto | Subscription-backed Codex review with authenticated current-head gating | | **minimal** | daily-repo-status, repo-audit | — | Hygiene-only: daily reports, no AI fixes | | **standard** | minimal + issue-processor, simplisticate | sfl-dispatcher, sfl-auditor | Detect + claim + fix issues automatically | -| **full** | standard + sfl-pr-review + pr-analyzer-general/quality/security/testing, pr-fixer, pr-promoter | sfl-dispatcher, sfl-auditor, sfl-pr-review-auto, sfl-pr-review-recovery | Complete autonomous loop with automatic full-spectrum and label-requested specialty review | +| **full** | standard + pr-analyzer-general/quality/security/testing, pr-fixer, pr-promoter | sfl-dispatcher, sfl-auditor, sfl-pr-review-auto | Complete loop with subscription-backed Codex and label-requested specialty review | --- @@ -35,7 +35,7 @@ Workflows listed here have graduated from staging (`.github/workflows/`) and are | [repo-audit](#repo-audit) | quality | Schedule — daily | low | `type:report` issue with findings + recommendations | 1.1.0 | | [issue-processor](#issue-processor) | automation | Dispatched — when `agent:fixable` issues exist | low | Draft PR with scoped fix for the oldest fixable issue | 1.0.0 | | [simplisticate](#simplisticate) | quality | Schedule — daily | low | Summary report issue + up to 3 `agent:fixable` issues | 1.0.0 | -| [sfl-pr-review](#sfl-pr-review) | review | Internal PR lifecycle + `sfl-review` | high | Immutable inline findings, bounded recovery, and fresh approval gate | 2.0.0 | +| [sfl-pr-review](#sfl-pr-review) | review | `gh sfl review` | low | Native Codex findings or a clean immutable-head gate | 2.1.0 | | [pr-analyzer-general](#pr-analyzer-general) | review | Dispatched — when draft PRs with `agent:pr` exist | trivial | PR comment with full-spectrum review | 1.1.0 | | [pr-analyzer-quality](#pr-analyzer-quality) | review | Dispatched — when draft PRs have `pr-analyzer-quality` | trivial | PR comment with quality-focused review | 1.1.0 | | [pr-analyzer-security](#pr-analyzer-security) | review | Dispatched — when draft PRs have `pr-analyzer-security` | trivial | PR comment with security-focused review | 1.1.0 | @@ -49,8 +49,7 @@ Workflows listed here have graduated from staging (`.github/workflows/`) and are |------|------|----------|---------| | [sfl-dispatcher](#sfl-dispatcher) | Standard YAML | Every 30 min | Gates gh-aw runs; only dispatches when work exists | | [sfl-auditor](#sfl-auditor) | Standard YAML | :15, :45 every hour | Repairs label/PR state discrepancies | -| `sfl-pr-review-auto` | Standard YAML | Internal PR lifecycle | Deduplicates immutable-head review dispatch and publishes the native head approval check | -| `sfl-pr-review-recovery` | Standard YAML | Failed reviewer run | Performs at most one provenance-validated missing-output retry | +| `sfl-pr-review-auto` | Standard YAML | Codex result events | Authenticates Codex output and publishes the immutable-head reviewer gate | --- @@ -142,22 +141,16 @@ Workflows listed here have graduated from staging (`.github/workflows/`) and are ### sfl-pr-review -**File**: [`deployment/workflows/sfl-pr-review.md`](deployment/workflows/sfl-pr-review.md) +**File**: [`deployment/infrastructure/sfl-pr-review-auto.yml`](deployment/infrastructure/sfl-pr-review-auto.yml) -**What it does**: Runs a three-pass full-spectrum review automatically when an -eligible non-draft internal pull request is opened, reopened, marked ready, or -updated. The `sfl-review` label requests an explicit rerun. It posts one native -inline thread per Critical, High, Medium, or Low finding, safely retires obsolete -SFL-owned threads, and publishes immutable review evidence for the native -`SFL Reviewer Approval` head check. A recovery workflow performs at most one retry when -formal review output is missing or partial. -The HemSoft deployment policy runs this workflow with -`moonshotai/kimi-k3` through OpenRouter; work-account SFL uses its own provider -policy independently. +**What it does**: `gh sfl review` posts one `@codex review` request for the +current head using the active HemSoft identity. The observer authenticates the +Codex App or bot artifact, resolves its reviewed-commit prefix to a full SHA, +and publishes `SFL Reviewer Gate Runner` on that exact head. -**Output**: Current-head GitHub review, inline review threads, structured -severity table, and an approval check that fails while any Critical, High, -Medium, or Low SFL finding remains unresolved. +**Output**: Native Codex review output plus a successful gate for a clean +current-head result or a failed gate when Codex reports findings. Stale and +spoofed artifacts are ignored. **Deploy command**: ```powershell @@ -165,11 +158,11 @@ Medium, or Low SFL finding remains unresolved. ``` **Acceptance criteria met**: -- [x] Eligible internal PR lifecycle events dispatch automatically -- [x] Trigger label is consumed during explicit activation -- [x] Security, correctness/reliability, and quality/maintainability passes run -- [x] Every finding is a severity-classified native review thread -- [x] Review, evidence, recovery, and approval are pinned to immutable PR context +- [x] One review request is posted per pull request head +- [x] Clean comments and finding reviews are authenticated separately +- [x] Reviewed commit prefixes resolve to the current full head SHA +- [x] Clean passes; findings and malformed authentic output fail +- [x] Stale and spoofed artifacts cannot publish the required gate --- diff --git a/README.md b/README.md index d4d2d226..3d34ca40 100644 --- a/README.md +++ b/README.md @@ -64,7 +64,7 @@ See [SOLVING-SOFTWARE-ENGINEERING.md](SOLVING-SOFTWARE-ENGINEERING.md) for the f # Deploy the full autonomous loop .\deployment\scripts\deploy-workflow.ps1 -Tier full -Repos "HemSoft/repository" -# Or deploy the automatic, recoverable SFL pull request reviewer +# Or deploy the subscription-backed Codex pull request reviewer .\deployment\scripts\deploy-workflow.ps1 -Tier review -Repos "HemSoft/repository" # Or start with hygiene-only @@ -77,7 +77,7 @@ See [SOLVING-SOFTWARE-ENGINEERING.md](SOLVING-SOFTWARE-ENGINEERING.md) for the f ### 3. Review the catalog See [CATALOG.md](CATALOG.md) for all available workflows, tiers, and expected outputs. -Reviewer installation, recovery, and optional gating are documented in +Reviewer installation, current-head evidence, and optional gating are documented in [docs/SFL-REVIEWER.md](docs/SFL-REVIEWER.md). For the private HemSoft CLI path, install a checksum-verified release with @@ -92,9 +92,8 @@ defaults to the reviewer-only tier and opens a deployment pull request. The SFL uses [Semantic Versioning](https://semver.org/). The single source of truth is the [`VERSION`](VERSION) file in this repo. The synchronized -[`deployment/release-metadata.json`](deployment/release-metadata.json) keeps -that HemSoft distribution version separate from the pinned Relias reviewer -release and immutable reviewed commit. +[`deployment/release-metadata.json`](deployment/release-metadata.json) records +the HemSoft distribution version and native Codex reviewer identity. ### Private release contract diff --git a/TODO.md b/TODO.md index 216d512e..dd866f9e 100644 --- a/TODO.md +++ b/TODO.md @@ -4,33 +4,48 @@ Keep the HemSoft reviewer reliable and finish one milestone at a time. -Core reviewer parity is complete. SFL automatically reviews the current head, -creates inline findings, submits App-authored reviews, publishes immutable -evidence, enforces a required gate, retries bounded operational failures, -cleans up obsolete threads, and filters reviewer communication through Unslop. +Issue #125 replaces the HemSoft reviewer model runtime with native Codex review +from Franz's ChatGPT subscription. SFL requests one current-head review, +authenticates Codex output, and preserves the required immutable-head gate. ## Working baseline - SFL is working on `HemSoft/hs-buddy`. It is the only repository approved for live HemSoft validation. -- `v2.1.0-rc.13` is the current immutable private release at +- `v2.1.0-rc.13` is the last immutable private release at `076325cd78119442dd0922abab2023451cff5189`. It is deployed to `hs-buddy` through [PR #454](https://github.com/HemSoft/hs-buddy/pull/454). HemSoft - intentionally uses Kimi K3 through its private OpenRouter route. + uses the retired Kimi/OpenRouter reviewer. It must be superseded before the + native Codex observer can be deployed. - `hs-buddy` PR #415 proved the full finding lifecycle on 2026-08-16. SFL found real issues, approved the fixes on the current head, recovered automatically from one failed evidence attempt, and finished with zero unresolved threads and a green required gate. -- The working repository rule requires `SFL Reviewer Gate Runner`. That runner - authenticates the App review plus `SFL Reviewer Approval` and - `SFL Review Evidence`. -- The personal-account App installation currently selects all repositories and - works. Do not change its authentication, permissions, ownership, or scope as - part of the current milestone. +- The working repository rule requires `SFL Reviewer Gate Runner`. The new + observer publishes that same Actions-owned check after authenticating Codex + and proving the reviewed commit is the current head. +- The Codex GitHub App is connected for the approved pilot repository. Do not + change its installation scope as part of this milestone. - Run `gh sfl init` or `gh sfl sync` only for an explicitly approved repository. Never fan either command out across the account. If approval is withdrawn, close the pending deployment PR and delete its branch. +## Active milestone: issue #125 + +- [x] Remove the OpenRouter/Kimi reviewer, compiled lock, recovery wrapper, and + reviewer credential requirements from the HemSoft package. +- [x] Make `gh sfl review --repo OWNER/REPO --pr NUMBER` post one head-bound + `@codex review` request and deduplicate retries. +- [x] Authenticate clean comments and finding reviews, resolve reviewed commit + prefixes, and reject stale, spoofed, or malformed evidence. +- [x] Preserve the strict `SFL Reviewer Gate Runner` branch-rule contract. +- [ ] Land the source PR after current-head Copilot and SFL review. +- [ ] Publish the next prerelease and pilot only on `HemSoft/hs-buddy`. +- [ ] Prove one clean result and one finding result through the deployed gate. + +This milestone explicitly excludes developer-documentation and account-wide +deployment. + ## Completed milestone: issue #73 PR #74 delivered the transactional repository-scoped gate. Issue #73 aligns diff --git a/deployment/engine-policy.json b/deployment/engine-policy.json index cce91cd6..c158601f 100644 --- a/deployment/engine-policy.json +++ b/deployment/engine-policy.json @@ -10,25 +10,7 @@ "CODEX_API_KEY", "OPENAI_API_KEY" ] - }, - "openrouter-kimi-k3-high": { - "provider": "copilot", - "model": "moonshotai/kimi-k3", - "requiredSecretsAnyOf": [ - "OPENROUTER_API_KEY" - ], - "environment": { - "COPILOT_PROVIDER_BASE_URL": "https://openrouter.ai/api/v1", - "COPILOT_PROVIDER_API_KEY": "${{ secrets.OPENROUTER_API_KEY }}", - "COPILOT_PROVIDER_TYPE": "openai", - "COPILOT_PROVIDER_WIRE_API": "responses", - "COPILOT_MODEL": "moonshotai/kimi-k3" - } } }, - "workflows": { - "sfl-pr-review": { - "profile": "openrouter-kimi-k3-high" - } - } + "workflows": {} } diff --git a/deployment/governance/labels.json b/deployment/governance/labels.json index 57ab9582..3312ab57 100644 --- a/deployment/governance/labels.json +++ b/deployment/governance/labels.json @@ -9,7 +9,7 @@ { "name": "pr-analyzer-quality", "color": "0e8a16", "description": "Request the optional PR quality analyzer" }, { "name": "pr-analyzer-security", "color": "e11d48", "description": "Request the optional PR security analyzer" }, { "name": "pr-analyzer-testing", "color": "5319e7", "description": "Request the optional PR testing analyzer" }, - { "name": "sfl-review", "color": "6f42c1", "description": "One-shot command that requests a standalone SFL full-spectrum PR review" }, + { "name": "sfl-review", "color": "6f42c1", "description": "Deprecated: use gh sfl review; applying this label does not trigger a review" }, { "name": "risk:trivial", "color": "cfd3d7", "description": "Zero-risk: formatting, typos, doc updates" }, { "name": "risk:low", "color": "0e8a16", "description": "Low-risk: dependency bumps, safe refactors" }, { "name": "risk:medium", "color": "fbca04", "description": "Medium-risk: logic changes, new features" }, diff --git a/deployment/governance/setup-labels.ps1 b/deployment/governance/setup-labels.ps1 index 80a75cd8..34607988 100644 --- a/deployment/governance/setup-labels.ps1 +++ b/deployment/governance/setup-labels.ps1 @@ -51,7 +51,7 @@ $Labels = @( @{ name = "pr-analyzer-quality"; color = "0e8a16"; description = "Request the optional PR quality analyzer" } @{ name = "pr-analyzer-security"; color = "e11d48"; description = "Request the optional PR security analyzer" } @{ name = "pr-analyzer-testing"; color = "5319e7"; description = "Request the optional PR testing analyzer" } - @{ name = "sfl-review"; color = "6f42c1"; description = "One-shot command that requests a standalone SFL full-spectrum PR review" } + @{ name = "sfl-review"; color = "6f42c1"; description = "Deprecated: use gh sfl review; applying this label does not trigger a review" } # ── Risk-class labels ───────────────────────────────────────────────────── @{ name = "risk:trivial"; color = "cfd3d7"; description = "Zero-risk: formatting, typos, doc updates" } diff --git a/deployment/infrastructure/sfl-auditor.yml b/deployment/infrastructure/sfl-auditor.yml index 8eeda042..574cfc8c 100644 --- a/deployment/infrastructure/sfl-auditor.yml +++ b/deployment/infrastructure/sfl-auditor.yml @@ -341,11 +341,18 @@ jobs: set -euo pipefail EXPECTED=0 - if MANIFEST=$(gh api "repos/$REPO/contents/sfl.json" --jq '.content' 2>/dev/null); then - if echo "$MANIFEST" | base64 --decode | jq -e \ - '.components | index("sfl-pr-review") != null' >/dev/null; then - EXPECTED=1 - fi + MANIFEST="" + MANIFEST_JSON="" + if ! MANIFEST=$(gh api "repos/$REPO/contents/.sfl/sfl.json" --jq '.content' 2>/dev/null); then + MANIFEST=$(gh api "repos/$REPO/contents/sfl.json" --jq '.content' 2>/dev/null || true) + fi + if [ -n "$MANIFEST" ]; then + MANIFEST_JSON=$(echo "$MANIFEST" | base64 --decode) + fi + if [ -n "$MANIFEST_JSON" ] && echo "$MANIFEST_JSON" | jq -e \ + '(.components | index("sfl-pr-review-auto") != null) or + ((.addons // []) | any(. == "pr-review"))' >/dev/null; then + EXPECTED=1 fi if [ "$EXPECTED" -eq 0 ]; then @@ -356,44 +363,46 @@ jobs: MISSING=0 MISSING_ITEMS="" - if ! gh api "repos/$REPO/actions/workflows/sfl-pr-review.lock.yml" >/dev/null 2>&1; then - echo "::warning::Missing active workflow .github/workflows/sfl-pr-review.lock.yml" - MISSING=$((MISSING + 1)) - MISSING_ITEMS="${MISSING_ITEMS}- [ ] Deploy \`.github/workflows/sfl-pr-review.lock.yml\`"$'\n' - fi - - REVIEW_LOCK="" - if REVIEW_LOCK_B64=$(gh api "repos/$REPO/contents/.github/workflows/sfl-pr-review.lock.yml" --jq '.content' 2>/dev/null); then - REVIEW_LOCK=$(echo "$REVIEW_LOCK_B64" | base64 --decode) - fi - if [ -n "$REVIEW_LOCK" ] && { \ - ! grep -q '"allow-only"' <<< "$REVIEW_LOCK" || \ - ! grep -q '"write-sink"' <<< "$REVIEW_LOCK"; \ - }; then - echo "::warning::Malformed SFL review guard policies in .github/workflows/sfl-pr-review.lock.yml" + OBSERVER_STATE=$(gh api "repos/$REPO/actions/workflows/sfl-pr-review-auto.yml" --jq '.state' 2>/dev/null || true) + if [ "$OBSERVER_STATE" != "active" ]; then + echo "::warning::Missing or disabled active workflow .github/workflows/sfl-pr-review-auto.yml (state: ${OBSERVER_STATE:-missing})" MISSING=$((MISSING + 1)) - MISSING_ITEMS="${MISSING_ITEMS}- [ ] Redeploy \`.github/workflows/sfl-pr-review.lock.yml\` with generated \`allow-only\` and \`write-sink\` guard policies"$'\n' + MISSING_ITEMS="${MISSING_ITEMS}- [ ] Deploy and enable \`.github/workflows/sfl-pr-review-auto.yml\`"$'\n' fi - if ! gh api "repos/$REPO/labels/sfl-review" >/dev/null 2>&1; then - echo "::warning::Missing required label sfl-review" - MISSING=$((MISSING + 1)) - MISSING_ITEMS="${MISSING_ITEMS}- [ ] Create the \`sfl-review\` label"$'\n' + OBSERVER="" + if OBSERVER_B64=$(gh api "repos/$REPO/contents/.github/workflows/sfl-pr-review-auto.yml" --jq '.content' 2>/dev/null); then + OBSERVER=$(echo "$OBSERVER_B64" | base64 --decode) fi - - REVIEW_SOURCE="" - if REVIEW_SOURCE_B64=$(gh api "repos/$REPO/contents/.github/workflows/sfl-pr-review.md" --jq '.content' 2>/dev/null); then - REVIEW_SOURCE=$(echo "$REVIEW_SOURCE_B64" | base64 --decode) + EXPECTED_SOURCE_SHA=$(jq -r '.sourceSha // empty' <<< "$MANIFEST_JSON") + OBSERVER_SOURCE_SHA=$(grep -F '# Source: HemSoft/set-it-free-loop/deployment/infrastructure/sfl-pr-review-auto.yml@' \ + <<< "$OBSERVER" | head -n 1 | cut -d@ -f2) || true + DEFAULT_BRANCH=$(gh api "repos/$REPO" --jq '.default_branch' 2>/dev/null || true) + DEFAULT_BRANCH_YAML=$(printf '%s' "$DEFAULT_BRANCH" | sed "s/'/''/g") + PUSH_FILTER_VALID=0 + BASE_ENV_VALID=0 + if [ -n "$OBSERVER" ] && [ -n "$DEFAULT_BRANCH" ]; then + if grep -Fqx " branches: ['$DEFAULT_BRANCH_YAML']" <<< "$OBSERVER" || \ + grep -Fqx " branches: [$DEFAULT_BRANCH]" <<< "$OBSERVER"; then + PUSH_FILTER_VALID=1 + fi + if grep -Fqx " SFL_REVIEW_BASE_BRANCH: '$DEFAULT_BRANCH_YAML'" <<< "$OBSERVER" || \ + grep -Fqx " SFL_REVIEW_BASE_BRANCH: $DEFAULT_BRANCH" <<< "$OBSERVER"; then + BASE_ENV_VALID=1 + fi fi - if [ -z "$REVIEW_SOURCE" ]; then - echo "::warning::Missing source workflow .github/workflows/sfl-pr-review.md" - MISSING=$((MISSING + 1)) - MISSING_ITEMS="${MISSING_ITEMS}- [ ] Deploy \`.github/workflows/sfl-pr-review.md\`"$'\n' - elif ! grep -q '^Deployed from: HemSoft/set-it-free-loop/' <<< "$REVIEW_SOURCE" || \ - grep -Eq '^# (Deployed from|To upgrade):' <<< "$REVIEW_SOURCE"; then - echo "::warning::Malformed SFL source provenance in .github/workflows/sfl-pr-review.md" + if [ -z "$OBSERVER" ] || { \ + ! grep -q 'name: SFL Codex Review Observer' <<< "$OBSERVER" || \ + ! grep -q 'github.event.sender.id == 199175422' <<< "$OBSERVER" || \ + ! grep -q 'name: "SFL Reviewer Gate Runner"' <<< "$OBSERVER" || \ + [ -z "$EXPECTED_SOURCE_SHA" ] || \ + [ "$OBSERVER_SOURCE_SHA" != "$EXPECTED_SOURCE_SHA" ] || \ + [ "$PUSH_FILTER_VALID" -ne 1 ] || \ + [ "$BASE_ENV_VALID" -ne 1 ]; \ + }; then + echo "::warning::Malformed subscription-backed Codex observer or stale default-branch deployment" MISSING=$((MISSING + 1)) - MISSING_ITEMS="${MISSING_ITEMS}- [ ] Redeploy \`.github/workflows/sfl-pr-review.md\` with non-heading source provenance"$'\n' + MISSING_ITEMS="${MISSING_ITEMS}- [ ] Redeploy \`.github/workflows/sfl-pr-review-auto.yml\` from the pinned SFL release"$'\n' fi ISSUE_TITLE="[SFL Auditor] Missing SFL review prerequisites" @@ -404,7 +413,7 @@ jobs: if [ "$MISSING" -gt 0 ] && [ -z "$EXISTING_ISSUE" ]; then ISSUE_BODY=$(printf '%s\n\n## Missing prerequisites\n\n%s## Recovery\n\n%s' \ - "The repository declares \`sfl-pr-review\` in \`sfl.json\`, but the standalone reviewer is missing or malformed." \ + "The repository declares standalone SFL review in \`sfl.json\`, but the standalone reviewer is missing or malformed." \ "$MISSING_ITEMS" \ "Run \`deployment/scripts/deploy-workflow.ps1 -Tier review\` and \`deployment/governance/setup-labels.ps1\` from \`HemSoft/set-it-free-loop\`.") gh issue create --repo "$REPO" \ diff --git a/deployment/infrastructure/sfl-pr-review-auto.yml b/deployment/infrastructure/sfl-pr-review-auto.yml index fa7065fb..9badb5aa 100644 --- a/deployment/infrastructure/sfl-pr-review-auto.yml +++ b/deployment/infrastructure/sfl-pr-review-auto.yml @@ -1,857 +1,1168 @@ -# HemSoft SFL reviewer platform v2 -name: SFL PR Review Auto Trigger +# Deployed from HemSoft/set-it-free-loop. The deployment command replaces this +# source pin with the immutable source SHA. +# Source: HemSoft/set-it-free-loop/deployment/infrastructure/sfl-pr-review-auto.yml@main +name: SFL Codex Review Observer +run-name: >- + ${{ github.event_name == 'issue_comment' && + startsWith(github.event.comment.body, '@codex review') && + format('SFL Codex review request #{0}', github.event.issue.number) || + format('SFL {0} event', github.event_name) }} on: + push: + branches: [main] issue_comment: types: [created] - pull_request_target: - types: [opened, synchronize, reopened, ready_for_review, edited, review_requested, labeled] pull_request_review: types: [submitted] + pull_request_target: + types: [opened, reopened, edited, synchronize] + +permissions: {} -concurrency: - group: sfl-pr-review-auto-${{ github.event.pull_request.number || github.event.issue.number }}-${{ github.event.pull_request.head.sha || github.event.comment.id }}-${{ github.event_name }}-${{ github.event.action }}-${{ github.event.action == 'labeled' && github.event.label.name == 'sfl-review' && 'explicit' || 'standard' }} - cancel-in-progress: false +env: + SFL_REVIEW_BASE_BRANCH: main jobs: - comment-command: + invalidate-review-request: + name: Invalidate prior gate for registered review request if: >- vars.SFL_ENABLED != 'false' && github.event_name == 'issue_comment' && - github.event.action == 'created' && github.event.issue.pull_request && - github.event.comment.body == '@sfl-app review' && - contains(fromJSON('["OWNER","MEMBER","COLLABORATOR"]'), github.event.comment.author_association) + github.event.sender.login == github.repository_owner && + startsWith(github.event.comment.body, '@codex review') runs-on: ubuntu-latest - timeout-minutes: 10 - outputs: - base-sha: ${{ steps.pull-request.outputs.base-sha }} - expected-review-run-id: ${{ steps.request-review.outputs.review-run-id }} - head-sha: ${{ steps.pull-request.outputs.head-sha }} - pr-number: ${{ github.event.issue.number }} - reviewer-installed: 'true' + timeout-minutes: 5 permissions: + checks: write contents: read + issues: read pull-requests: read + statuses: read steps: - - name: Configure GH_HOST for enterprise compatibility - shell: bash - run: | - GH_HOST="${GITHUB_SERVER_URL#https://}" - GH_HOST="${GH_HOST#http://}" - echo "GH_HOST=${GH_HOST}" >> "$GITHUB_ENV" - - - name: Validate command and seal pull request state - id: pull-request - env: - DEFAULT_BRANCH: ${{ github.event.repository.default_branch }} - GH_TOKEN: ${{ github.token }} - PR_NUMBER: ${{ github.event.issue.number }} - REPOSITORY: ${{ github.repository }} - TRUSTED_REF: ${{ github.event.repository.default_branch }} - run: | - set -euo pipefail - REVIEWER_WORKFLOW=$(gh api \ - --method GET \ - "repos/${REPOSITORY}/contents/.github/workflows/sfl-pr-review.lock.yml" \ - -f "ref=${TRUSTED_REF}" \ - -H "Accept: application/vnd.github.raw+json") - if ! grep -qE '^\s+dispatch_id:\s*$' <<< "$REVIEWER_WORKFLOW"; then - echo "::error::The installed reviewer does not support exact dispatch correlation" - exit 1 - fi - if ! grep -qE '^\s+review_effort:\s*$' <<< "$REVIEWER_WORKFLOW"; then - echo "::error::The installed reviewer does not support selected review effort" - exit 1 - fi - - PR=$(gh api "repos/${REPOSITORY}/pulls/${PR_NUMBER}") - BASE_REF=$(jq -r '.base.ref // ""' <<< "$PR") - BASE_SHA=$(jq -r '.base.sha // ""' <<< "$PR") - DRAFT=$(jq -r 'if .draft == false then false else true end' <<< "$PR") - HEAD_REPOSITORY=$(jq -r '.head.repo.full_name // ""' <<< "$PR") - HEAD_SHA=$(jq -r '.head.sha // ""' <<< "$PR") - REVIEW_EFFORT=$(jq -r ' - [.labels[]?.name] as $labels - | if ($labels | index("sfl-effort:high")) then "high" - elif ($labels | index("sfl-effort:medium")) then "medium" - else "low" - end - ' <<< "$PR") - STATE=$(jq -r '.state // ""' <<< "$PR") - - if [ "$STATE" != "open" ] || [ "$DRAFT" = "true" ]; then - echo "::error::SFL comment review requires an open, ready pull request" - exit 1 - fi - if [ "$HEAD_REPOSITORY" != "$REPOSITORY" ]; then - echo "::error::SFL comment review does not execute untrusted fork code" - exit 1 - fi - if [ "$BASE_REF" != "$DEFAULT_BRANCH" ]; then - echo "::error::SFL comment review only validates the default branch" - exit 1 - fi - if ! [[ "$BASE_SHA" =~ ^[0-9a-f]{40}$ ]] || ! [[ "$HEAD_SHA" =~ ^[0-9a-f]{40}$ ]]; then - echo "::error::GitHub returned an invalid pull request commit" - exit 1 - fi - - echo "base-sha=${BASE_SHA}" >> "$GITHUB_OUTPUT" - echo "head-sha=${HEAD_SHA}" >> "$GITHUB_OUTPUT" - echo "review-effort=${REVIEW_EFFORT}" >> "$GITHUB_OUTPUT" - - - name: Mint SFL GitHub App token - id: app-token - uses: actions/create-github-app-token@bcd2ba49218906704ab6c1aa796996da409d3eb1 # v3.2.0 + - name: Fail the gate while a registered Codex review is pending + uses: actions/github-script@3a2844b7e9c422d3c10d287c895573f7108da1b3 # v9.0.0 with: - client-id: ${{ vars.SFL_APP_CLIENT_ID }} - private-key: ${{ secrets.SFL_APP_PRIVATE_KEY }} - owner: ${{ github.repository_owner }} - repositories: ${{ github.event.repository.name }} - permission-actions: write - permission-contents: read - permission-pull-requests: write - - - name: Request review and acknowledge the exact run - id: request-review - env: - BASE_SHA: ${{ steps.pull-request.outputs.base-sha }} - COMMAND_COMMENT_ID: ${{ github.event.comment.id }} - GH_TOKEN: ${{ steps.app-token.outputs.token }} - HEAD_SHA: ${{ steps.pull-request.outputs.head-sha }} - PR_NUMBER: ${{ github.event.issue.number }} - REPOSITORY: ${{ github.repository }} - REVIEW_EFFORT: ${{ steps.pull-request.outputs.review-effort }} - TRUSTED_REF: ${{ github.event.repository.default_branch }} - run: | - set -euo pipefail - DISPATCH_ID="command-${COMMAND_COMMENT_ID}" - MARKER="" - EXPECTED_TITLE="SFL PR Review #${PR_NUMBER} ${BASE_SHA}:${HEAD_SHA} retry=" - - # BEGIN TESTABLE COMMENT COMMAND LOOKUP - find_command_acknowledgement() { - local marker="$1" - jq -s -r \ - --arg marker "$marker" ' - [add[] - | select((.user.login // "") | test("^sfl-app\\[bot\\]$"; "i")) - | select((.body // "") | contains($marker))] - | sort_by(.id) - | last - | select(. != null) - | @base64 - ' - } - - find_command_run() { - local expected_title="$1" - local dispatch_id="$2" - jq -r \ - --arg dispatch_id "$dispatch_id" \ - --arg title "$expected_title" ' - [.workflow_runs[] - | select(.display_title | startswith($title)) - | select(.display_title | endswith(" dispatch=" + $dispatch_id))] - | sort_by(.id) - | last - | .id // empty - ' - } - - command_acknowledgement_complete() { - jq -e '.body | test("\\[Actions run [1-9][0-9]*\\]\\(")' > /dev/null - } - - command_acknowledgement_run_id() { - jq -r '.body | (capture("\\[Actions run (?[1-9][0-9]*)\\]\\(")? | .id) // empty' - } - - command_run_matches() { - local expected_title="$1" - local dispatch_id="$2" - jq -e \ - --arg dispatch_id "$dispatch_id" \ - --arg title "$expected_title" ' - .path == ".github/workflows/sfl-pr-review.lock.yml" - and .event == "workflow_dispatch" - and ((.display_title // "") | startswith($title)) - and ((.display_title // "") | endswith(" dispatch=" + $dispatch_id)) - ' > /dev/null - } - # END TESTABLE COMMENT COMMAND LOOKUP - - ACKNOWLEDGEMENT=$(gh api \ - "repos/${REPOSITORY}/issues/${PR_NUMBER}/comments?per_page=100" \ - --paginate \ - | find_command_acknowledgement "$MARKER") - ACKNOWLEDGEMENT_ID="" - if [ -n "$ACKNOWLEDGEMENT" ]; then - ACKNOWLEDGEMENT_JSON=$(base64 --decode <<< "$ACKNOWLEDGEMENT") - ACKNOWLEDGEMENT_ID=$(jq -r '.id // empty' <<< "$ACKNOWLEDGEMENT_JSON") - if command_acknowledgement_complete <<< "$ACKNOWLEDGEMENT_JSON"; then - ACKNOWLEDGED_RUN_ID=$(command_acknowledgement_run_id <<< "$ACKNOWLEDGEMENT_JSON") - if ! [[ "$ACKNOWLEDGED_RUN_ID" =~ ^[1-9][0-9]*$ ]]; then - echo "::error::Completed SFL acknowledgement does not identify its exact run" - exit 1 - fi - if ACKNOWLEDGED_RUN=$(gh api \ - "repos/${REPOSITORY}/actions/runs/${ACKNOWLEDGED_RUN_ID}") \ - && command_run_matches \ - "$EXPECTED_TITLE" \ - "$DISPATCH_ID" <<< "$ACKNOWLEDGED_RUN"; then - echo "review-run-id=${ACKNOWLEDGED_RUN_ID}" >> "$GITHUB_OUTPUT" - echo "Command comment ${COMMAND_COMMENT_ID} was already acknowledged for ${HEAD_SHA}" - exit 0 - fi - echo "Ignoring stale SFL acknowledgement run ${ACKNOWLEDGED_RUN_ID}; it does not match ${BASE_SHA}:${HEAD_SHA}" - fi - else - PENDING_BODY=$(printf '%s\nSFL review request accepted for `%s`. Resolving the Actions run…' \ - "$MARKER" "$HEAD_SHA") - ACKNOWLEDGEMENT_ID=$(jq -nc --arg body "$PENDING_BODY" '{body: $body}' \ - | gh api --method POST \ - "repos/${REPOSITORY}/issues/${PR_NUMBER}/comments" \ - --input - \ - | jq -r '.id // empty') - fi - if ! [[ "$ACKNOWLEDGEMENT_ID" =~ ^[1-9][0-9]*$ ]]; then - echo "::error::GitHub did not return the SFL acknowledgement comment ID" - exit 1 - fi - - REVIEW_RUN_ID=$(gh api \ - "repos/${REPOSITORY}/actions/workflows/sfl-pr-review.lock.yml/runs?event=workflow_dispatch&per_page=100" \ - | find_command_run "$EXPECTED_TITLE" "$DISPATCH_ID") - if ! [[ "$REVIEW_RUN_ID" =~ ^[1-9][0-9]*$ ]]; then - AW_CONTEXT=$(jq -cn \ - --arg base_sha "${BASE_SHA}" \ - --arg head_sha "${HEAD_SHA}" \ - --argjson item_number "${PR_NUMBER}" \ - '{item_type: "pull_request", item_number: $item_number, base_sha: $base_sha, head_sha: $head_sha}') - gh api --method POST \ - "repos/${REPOSITORY}/actions/workflows/sfl-pr-review.lock.yml/dispatches" \ - -f "ref=${TRUSTED_REF}" \ - -f "inputs[item_number]=${PR_NUMBER}" \ - -f "inputs[aw_context]=${AW_CONTEXT}" \ - -f "inputs[base_sha]=${BASE_SHA}" \ - -f "inputs[head_sha]=${HEAD_SHA}" \ - -f "inputs[retry_count]=0" \ - -f "inputs[review_effort]=${REVIEW_EFFORT}" \ - -f "inputs[dispatch_id]=${DISPATCH_ID}" - echo "Requested SFL review for PR #${PR_NUMBER} at ${HEAD_SHA} with dispatch ${DISPATCH_ID}" - - for ATTEMPT in $(seq 1 120); do - REVIEW_RUN_ID=$(gh api \ - "repos/${REPOSITORY}/actions/workflows/sfl-pr-review.lock.yml/runs?event=workflow_dispatch&per_page=100" \ - | find_command_run "$EXPECTED_TITLE" "$DISPATCH_ID") - if [[ "$REVIEW_RUN_ID" =~ ^[1-9][0-9]*$ ]]; then - break - fi - sleep 2 - done - fi - if ! [[ "$REVIEW_RUN_ID" =~ ^[1-9][0-9]*$ ]]; then - COMMAND_RUN_URL="${GITHUB_SERVER_URL}/${REPOSITORY}/actions/runs/${GITHUB_RUN_ID}" - FAILURE_BODY=$(printf '%s\nSFL review was requested for `%s`, but its Actions run could not be correlated. [Command run](%s)' \ - "$MARKER" "$HEAD_SHA" "$COMMAND_RUN_URL") - jq -nc --arg body "$FAILURE_BODY" '{body: $body}' \ - | gh api --method PATCH \ - "repos/${REPOSITORY}/issues/comments/${ACKNOWLEDGEMENT_ID}" \ - --input - > /dev/null - echo "::error::Could not resolve the SFL review run requested by comment ${COMMAND_COMMENT_ID}" - exit 1 - fi - echo "review-run-id=${REVIEW_RUN_ID}" >> "$GITHUB_OUTPUT" + github-token: ${{ github.token }} + script: | + const {owner, repo} = context.repo; + const pullNumber = context.payload.issue.number; + const commentId = context.payload.comment.id; + const comment = await github.rest.issues.getComment({ + owner, repo, comment_id: commentId, + }); + if (comment.data.updated_at !== comment.data.created_at) { + core.info("Edited Codex review requests are not accepted"); + return; + } + const pull = await github.rest.pulls.get({ + owner, repo, pull_number: pullNumber, + }); + const defaultBranch = context.payload.repository.default_branch; + const headRepo = pull.data.head.repo && pull.data.head.repo.full_name; + if (pull.data.state !== "open" || + pull.data.base.ref !== defaultBranch || + !headRepo || + headRepo.toLowerCase() !== `${owner}/${repo}`.toLowerCase()) { + core.info("Codex review request does not target a supported open pull request"); + return; + } + const currentHead = pull.data.head.sha; + const currentBase = pull.data.base.sha; + const reviewerChecks = async () => github.paginate( + github.rest.checks.listForRef, + { + owner, + repo, + ref: currentHead, + check_name: "SFL Reviewer Gate Runner", + filter: "all", + per_page: 100, + }, + response => response.data.check_runs, + ); + const reviewContextToken = checks => { + const invalidation = checks.filter( + check => check.app && check.app.id === 15368 && + ((check.external_id || "").startsWith("sfl-codex-review:pull-context:") || + (check.external_id || "").startsWith("sfl-codex-review:base-advance:")), + ).sort((left, right) => right.id - left.id)[0]; + return invalidation ? invalidation.external_id : "none"; + }; + const contextToken = reviewContextToken(await reviewerChecks()); + const marker = ``; + if ((comment.data.body || "").trim() !== `@codex review\n\n${marker}`) { + core.info("Codex review request does not contain the exact current SFL marker"); + return; + } + const targetURL = (comment.data.html_url || "").toLowerCase(); + let registered = false; + for (let attempt = 0; attempt < 12 && !registered; attempt += 1) { + const statuses = await github.paginate( + github.rest.repos.listCommitStatusesForRef, + {owner, repo, ref: currentHead, per_page: 100}, + ); + registered = statuses.some(status => + status.context === "SFL Codex Review Request Registry" && + ((status.creator && status.creator.login) || "").toLowerCase() === owner.toLowerCase() && + (status.target_url || "").toLowerCase() === targetURL, + ); + if (!registered && attempt < 11) { + await new Promise(resolve => setTimeout(resolve, 5000)); + } + } + if (!registered) { + core.info("Codex review request was not registered by gh sfl review"); + return; + } + const confirmedPull = await github.rest.pulls.get({ + owner, repo, pull_number: pullNumber, + }); + const confirmedChecks = await reviewerChecks(); + if (confirmedPull.data.state !== "open" || + confirmedPull.data.head.sha !== currentHead || + confirmedPull.data.base.sha !== currentBase || + reviewContextToken(confirmedChecks) !== contextToken) { + core.info("Pull request context changed before the review request could invalidate the gate"); + return; + } + const terminalRequestIdentity = `:request:${commentId}:at:`; + if (confirmedChecks.some( + check => check.app && check.app.id === 15368 && + check.status === "completed" && + (check.external_id || "").includes(terminalRequestIdentity), + )) { + core.info("This registered Codex review request already has a terminal SFL gate"); + return; + } + const externalId = `sfl-codex-review:request-pending:${commentId}`; + if (confirmedChecks.some( + check => check.app && check.app.id === 15368 && check.external_id === externalId, + )) { + core.info("This registered Codex review request already invalidated the prior gate"); + return; + } + const reason = `Registered Codex review request ${commentId} is pending for pull request #${pullNumber}`; + await github.rest.checks.create({ + owner, + repo, + name: "SFL Reviewer Gate Runner", + head_sha: currentHead, + status: "completed", + conclusion: "failure", + completed_at: new Date().toISOString(), + details_url: comment.data.html_url, + external_id: externalId, + output: {title: "SFL Codex review pending", summary: reason}, + }); + core.setFailed(reason); - RUN_URL="${GITHUB_SERVER_URL}/${REPOSITORY}/actions/runs/${REVIEW_RUN_ID}" - FINAL_BODY=$(printf '%s\nSFL review requested for `%s`: [Actions run %s](%s)' \ - "$MARKER" "$HEAD_SHA" "$REVIEW_RUN_ID" "$RUN_URL") - jq -nc --arg body "$FINAL_BODY" '{body: $body}' \ - | gh api --method PATCH \ - "repos/${REPOSITORY}/issues/comments/${ACKNOWLEDGEMENT_ID}" \ - --input - > /dev/null - dispatch: + observe: + name: Observe authenticated Codex review if: >- - github.event_name != 'issue_comment' && vars.SFL_ENABLED != 'false' && - (github.event.action != 'labeled' - || github.event.label.name == 'sfl-review') + github.event.sender.id == 199175422 && + ((github.event_name == 'issue_comment' && github.event.issue.pull_request) || + github.event_name == 'pull_request_review') + concurrency: + group: sfl-codex-review-serialize-${{ github.repository }}-${{ github.event.issue.number || github.event.pull_request.number }} + cancel-in-progress: false runs-on: ubuntu-latest + timeout-minutes: 5 permissions: + actions: read + checks: write contents: read - outputs: - expected-review-run-id: ${{ steps.dispatch-review.outputs.review-run-id }} - reviewer-installed: ${{ steps.reviewer-contract.outputs.installed }} + issues: read + pull-requests: read + statuses: read steps: - - name: Configure GH_HOST for enterprise compatibility - shell: bash - run: | - GH_HOST="${GITHUB_SERVER_URL#https://}" - GH_HOST="${GH_HOST#http://}" - echo "GH_HOST=${GH_HOST}" >> "$GITHUB_ENV" - - - name: Detect installed reviewer contract - id: reviewer-contract - env: - GH_TOKEN: ${{ github.token }} - REPOSITORY: ${{ github.repository }} - TRUSTED_REF: ${{ github.event.repository.default_branch }} - run: | - set -euo pipefail - error_file=$(mktemp) - if ! base_workflow="$(gh api \ - --method GET \ - "repos/${REPOSITORY}/contents/.github/workflows/sfl-pr-review.lock.yml" \ - -f "ref=${TRUSTED_REF}" \ - -H "Accept: application/vnd.github.raw+json" 2>"$error_file")"; then - error_text=$(cat "$error_file") - rm -f "$error_file" - if grep -q 'HTTP 404' <<< "$error_text"; then - echo "installed=false" >> "$GITHUB_OUTPUT" - echo "Reviewer lock is absent from ${TRUSTED_REF}; treating this as bootstrap deployment" - exit 0 - fi - echo "::error::Could not inspect the default-branch reviewer contract: ${error_text}" >&2 - exit 1 - fi - rm -f "$error_file" - if grep -qE '^\s+dispatch_id:\s*$' <<< "${base_workflow}"; then - echo "installed=true" >> "$GITHUB_OUTPUT" - exit 0 - fi - echo "installed=false" >> "$GITHUB_OUTPUT" - echo "Compatible reviewer lock is not on ${TRUSTED_REF}; treating this as bootstrap deployment" - - - name: Validate review eligibility - if: steps.reviewer-contract.outputs.installed == 'true' - env: - BASE_REF: ${{ github.event.pull_request.base.ref }} - DEFAULT_BRANCH: ${{ github.event.repository.default_branch }} - DRAFT: ${{ github.event.pull_request.draft }} - HEAD_REPOSITORY: ${{ github.event.pull_request.head.repo.full_name }} - REPOSITORY: ${{ github.repository }} - run: | - set -euo pipefail - if [ "$HEAD_REPOSITORY" != "$REPOSITORY" ]; then - echo "::error::SFL required review does not execute untrusted fork code" - exit 1 - fi - if [ "$DRAFT" = "true" ]; then - echo "::error::SFL required review waits until the pull request is ready" - exit 1 - fi - if [ "$BASE_REF" != "$DEFAULT_BRANCH" ]; then - echo "::error::SFL required review only validates the default branch" - exit 1 - fi - - - name: Mint SFL GitHub App token - if: steps.reviewer-contract.outputs.installed == 'true' - id: app-token - uses: actions/create-github-app-token@bcd2ba49218906704ab6c1aa796996da409d3eb1 # v3.2.0 + - name: Validate Codex result and publish immutable-head gate + uses: actions/github-script@3a2844b7e9c422d3c10d287c895573f7108da1b3 # v9.0.0 with: - client-id: ${{ vars.SFL_APP_CLIENT_ID }} - private-key: ${{ secrets.SFL_APP_PRIVATE_KEY }} - owner: ${{ github.repository_owner }} - repositories: ${{ github.event.repository.name }} - permission-actions: write - permission-contents: read - permission-pull-requests: write - - - name: Reset stale SFL review state after a push - if: >- - steps.reviewer-contract.outputs.installed == 'true' - && github.event.action == 'synchronize' - && contains(github.event.pull_request.labels.*.name, 'sfl-pr') - env: - GH_TOKEN: ${{ steps.app-token.outputs.token }} - PR_NUMBER: ${{ github.event.pull_request.number }} - REPOSITORY: ${{ github.repository }} - run: | - set -euo pipefail - LIVE_LABELS=$(gh api "repos/${REPOSITORY}/issues/${PR_NUMBER}/labels?per_page=100" \ - --paginate \ - --jq '.[].name') - if ! grep -Eq '^(sfl-done|sfl-needs-work)$' <<< "$LIVE_LABELS"; then - echo "PR #${PR_NUMBER} has no stale terminal review state to reset" - exit 0 - fi - gh api --method POST "repos/${REPOSITORY}/issues/${PR_NUMBER}/labels" \ - -f "labels[]=sfl-ready-for-review" > /dev/null - gh api --method DELETE \ - "repos/${REPOSITORY}/issues/${PR_NUMBER}/labels/sfl-done" 2>/dev/null || true - gh api --method DELETE \ - "repos/${REPOSITORY}/issues/${PR_NUMBER}/labels/sfl-needs-work" 2>/dev/null || true - echo "PR #${PR_NUMBER} received a new head — reset stale review state" + github-token: ${{ github.token }} + script: | + // BEGIN TESTABLE CODEX OBSERVER + function classifyCodexArtifact(input) { + const botId = 199175422; + const botLogin = "chatgpt-codex-connector[bot]"; + const appId = 1144995; + const appSlug = "chatgpt-codex-connector"; + const appOwner = "openai"; + const artifact = input.artifact || {}; + const user = artifact.user || {}; + const app = artifact.performed_via_github_app; + const body = artifact.body || ""; - - name: Dispatch or resolve full-spectrum review - id: dispatch-review - if: steps.reviewer-contract.outputs.installed == 'true' - env: - BASE_SHA: ${{ github.event.pull_request.base.sha }} - EVENT_ACTION: ${{ github.event.action }} - EVENT_NAME: ${{ github.event_name }} - GH_TOKEN: ${{ steps.app-token.outputs.token }} - HEAD_SHA: ${{ github.event.pull_request.head.sha }} - PR_NUMBER: ${{ github.event.pull_request.number }} - REPOSITORY: ${{ github.repository }} - REQUESTED_REVIEWER: ${{ github.event.requested_reviewer.login }} - REVIEW_AUTHOR: ${{ github.event.review.user.login }} - REVIEW_BODY: ${{ github.event.review.body }} - TRUSTED_REF: ${{ github.event.repository.default_branch }} - run: | - set -euo pipefail - - normalize_login() { - printf '%s' "$1" | sed -E 's/\[bot\]$//I' | tr '[:upper:]' '[:lower:]' - } + if (user.id !== botId || user.login !== botLogin) { + return { action: "ignore", reason: "artifact author is not the Codex connector bot" }; + } - # BEGIN TESTABLE REVIEW EFFORT - resolve_review_effort() { - local labels="$1" - local effort - for effort in high medium low; do - if grep -Fxq "sfl-effort:${effort}" <<< "$labels"; then - printf '%s\n' "$effort" - return - fi - done - printf 'low\n' - } - # END TESTABLE REVIEW EFFORT + if (input.eventName === "issue_comment") { + if (!app || app.id !== appId || app.slug !== appSlug || + !app.owner || app.owner.login !== appOwner) { + return { action: "ignore", reason: "comment lacks the expected Codex GitHub App provenance" }; + } + } else if (input.eventName === "pull_request_review") { + // GitHub's review REST object currently omits performed_via_github_app. + // If GitHub begins returning it, require the same immutable App identity. + if (app && (app.id !== appId || app.slug !== appSlug || + !app.owner || app.owner.login !== appOwner)) { + return { action: "ignore", reason: "review has conflicting GitHub App provenance" }; + } + if (artifact.state !== "COMMENTED") { + return { action: "failure", reason: "Codex review has an unexpected state" }; + } + } else { + return { action: "ignore", reason: "unsupported event" }; + } - consume_review_label() { - local labels="$1" - local error_file error_text - if ! grep -Fxq "sfl-review" <<< "$labels"; then - return 0 - fi - error_file=$(mktemp) - if gh api --method DELETE \ - "repos/${REPOSITORY}/issues/${PR_NUMBER}/labels/sfl-review" \ - > /dev/null 2>"$error_file"; then - rm -f "$error_file" - echo "Consumed sfl-review trigger label from PR #${PR_NUMBER}" - return 0 - fi - error_text=$(cat "$error_file") - rm -f "$error_file" - if grep -q 'HTTP 404' <<< "$error_text"; then - echo "sfl-review trigger label is already absent from PR #${PR_NUMBER}" - return 0 - fi - echo "::error::Could not consume sfl-review trigger label: ${error_text}" >&2 - return 1 - } + const currentHead = (input.currentHead || "").toLowerCase(); + const resolvedSha = (input.resolvedSha || "").toLowerCase(); + const reviewCommit = (input.reviewCommit || "").toLowerCase(); + if (reviewCommit && reviewCommit !== currentHead) { + return { action: "ignore", reason: "Codex artifact is stale for the current pull request head" }; + } + if (!resolvedSha) { + if (input.eventName === "issue_comment") { + return { action: "ignore", reason: "Codex comment cannot be bound to a reviewed commit" }; + } + return { action: "failure", reason: "Codex artifact has a malformed or unresolvable reviewed commit" }; + } + if (resolvedSha !== currentHead) { + return { action: "ignore", reason: "Codex artifact is stale for the current pull request head" }; + } + if (!input.requestMatchesCurrentBase) { + return { action: "ignore", reason: "Codex artifact is not bound to a review request for the current head and base" }; + } + if (input.openPullCount !== 1) { + return { action: "failure", reason: "The current head is shared by multiple open pull requests; use a separate branch before requesting review" }; + } - if [ "$EVENT_NAME" = "pull_request_review" ]; then - REVIEW_RUN_ID="" - if [ "$(normalize_login "$REVIEW_AUTHOR")" = "sfl-app" ]; then - REVIEW_RUN_ID=$(printf '%s' "$REVIEW_BODY" \ - | sed -nE 's#.*actions/runs/([0-9]+).*#\1#p' \ - | tail -n 1) - if ! [[ "$REVIEW_RUN_ID" =~ ^[1-9][0-9]*$ ]]; then - echo "::error::SFL review does not identify its workflow run" - exit 1 - fi - fi - echo "review-run-id=${REVIEW_RUN_ID}" >> "$GITHUB_OUTPUT" - exit 0 - fi + if (input.eventName === "issue_comment" && + /^Codex Review: Didn't find any major issues\./.test(body)) { + return { action: "success", reason: "Codex found no major issues on the current head" }; + } + if (input.eventName === "pull_request_review" && + (input.inlineCount > 0 || /automated review suggestions/i.test(body))) { + return { action: "failure", reason: "Codex reported review findings on the current head" }; + } + return { action: "failure", reason: "authenticated Codex artifact has an unknown result shape" }; + } - if [ "$EVENT_ACTION" = "review_requested" ] \ - && [ "$(normalize_login "$REQUESTED_REVIEWER")" != "sfl-app" ]; then - echo "review-run-id=" >> "$GITHUB_OUTPUT" - exit 0 - fi + function isActiveInvalidationRun(run, pullNumber, owner) { + if (!run || run.status === "completed") return false; + if (run.event === "push") return true; + if (run.event === "issue_comment") { + return ((run.actor && run.actor.login) || "").toLowerCase() === owner.toLowerCase() && + run.display_title === `SFL Codex review request #${pullNumber}`; + } + return run.event === "pull_request_target" && + (run.pull_requests || []).some(candidate => candidate.number === pullNumber); + } - PR_LABELS=$(gh api \ - "repos/${REPOSITORY}/issues/${PR_NUMBER}/labels?per_page=100" \ - --paginate --jq '.[].name') - REVIEW_EFFORT=$(resolve_review_effort "$PR_LABELS") + function requestGateExternalIdPrefix(pullNumber, currentBase, contextToken, requestId, requestTime) { + return `sfl-codex-review:pull:${pullNumber}:base:${currentBase.toLowerCase()}:context:${encodeURIComponent(contextToken)}:request:${requestId}:at:${requestTime}`; + } - # Dedup: never dispatch a second standard review for the same sealed - # base/head. Rapid event flurries (labeled/edited/synchronize - # bursts) and recovery workflow dispatches otherwise race into - # duplicate paid runs; review-side concurrency cancels the loser - # only after agent time is already spent. - # - # This check is deliberately not backed by a shared concurrency - # group: a group holds at most one running plus one pending job, so - # a trigger burst could evict a pending recovery transition — a - # correctness hazard worse than an occasional duplicate. The - # residual check-then-dispatch window is sub-second, and its worst - # case is one duplicate run that sealed provenance rejects. - EXPECTED_TITLE="SFL PR Review #${PR_NUMBER} ${BASE_SHA}:${HEAD_SHA} retry=" - # Bounded lookup: active statuses plus successful completions, one - # page each — never paginate the workflow's full run history on every - # pull request event. Reusing a successful exact-state run also - # closes the queue-delay window where an edited event starts after - # the original reviewer has already completed. - # BEGIN TESTABLE DISPATCH DEDUP - reusable_review_statuses() { - printf '%s\n' queued in_progress success - } + function requestGateExternalId(pullNumber, currentBase, contextToken, requestId, requestTime, artifactIdentity) { + return `${requestGateExternalIdPrefix(pullNumber, currentBase, contextToken, requestId, requestTime)}:artifact:${artifactIdentity}`; + } + // END TESTABLE CODEX OBSERVER - find_reusable_review_run() { - local expected_title="$1" - local review_label_present="${2:-false}" - if [ "$review_label_present" = "true" ]; then - cat > /dev/null - return 0 - fi - jq --arg title "${expected_title}" ' - [.workflow_runs[] | select(.display_title | startswith($title))] - | sort_by(.id) - | last - | .id // empty - ' - } - # END TESTABLE DISPATCH DEDUP - REUSABLE_RUN_ID="" - REVIEW_LABEL_PRESENT="false" - if grep -Fxq "sfl-review" <<< "$PR_LABELS"; then - REVIEW_LABEL_PRESENT="true" - fi - # Whichever concurrent event first observes a live explicit request - # must dispatch with the effort labels selected at request time, even - # if another review is already active or successfully completed. A - # stale queued labeled event whose trigger was consumed by another - # event uses ordinary deduplication instead. - for STATUS in $(reusable_review_statuses); do - MATCHING_RUN_ID=$(gh api \ - "repos/${REPOSITORY}/actions/workflows/sfl-pr-review.lock.yml/runs?status=${STATUS}&per_page=100" \ - | find_reusable_review_run \ - "${EXPECTED_TITLE}" "$REVIEW_LABEL_PRESENT") - if [[ "$MATCHING_RUN_ID" =~ ^[1-9][0-9]*$ ]]; then - REUSABLE_RUN_ID="$MATCHING_RUN_ID" - break - fi - done - if [ -n "$REUSABLE_RUN_ID" ]; then - consume_review_label "$PR_LABELS" - echo "Current-state review already active or successfully completed for ${BASE_SHA}:${HEAD_SHA} — skipping duplicate dispatch" - echo "review-run-id=${REUSABLE_RUN_ID}" >> "$GITHUB_OUTPUT" - exit 0 - fi + const {owner, repo} = context.repo; + const eventName = context.eventName; + const pullNumber = eventName === "issue_comment" + ? context.payload.issue.number + : context.payload.pull_request.number; + const artifactId = eventName === "issue_comment" + ? context.payload.comment.id + : context.payload.review.id; + let artifact; + let inlineCount = 0; + let reviewCommit = ""; + if (eventName === "issue_comment") { + const response = await github.rest.issues.getComment({ + owner, repo, comment_id: artifactId, + }); + artifact = response.data; + } else { + const response = await github.rest.pulls.getReview({ + owner, repo, pull_number: pullNumber, review_id: artifactId, + }); + artifact = response.data; + reviewCommit = artifact.commit_id || ""; + const comments = await github.paginate( + "GET /repos/{owner}/{repo}/pulls/{pull_number}/reviews/{review_id}/comments", + {owner, repo, pull_number: pullNumber, review_id: artifactId, per_page: 100}, + ); + inlineCount = comments.length; + } - AW_CONTEXT=$(jq -cn \ - --arg base_sha "${BASE_SHA}" \ - --arg head_sha "${HEAD_SHA}" \ - --argjson item_number "${PR_NUMBER}" \ - '{item_type: "pull_request", item_number: $item_number, base_sha: $base_sha, head_sha: $head_sha}') + const pull = await github.rest.pulls.get({ + owner, repo, pull_number: pullNumber, + }); + if (pull.data.state !== "open") { + core.info("The pull request is no longer open; no SFL gate was published"); + return; + } + const defaultBranch = context.payload.repository && context.payload.repository.default_branch; + if (!defaultBranch) throw new Error("Could not determine the repository default branch"); + if (pull.data.base.ref !== defaultBranch) { + core.info(`Pull requests targeting ${pull.data.base.ref} are unsupported; SFL reviews require the default branch ${defaultBranch}`); + return; + } + const headRepo = pull.data.head.repo && pull.data.head.repo.full_name; + if (!headRepo || headRepo.toLowerCase() !== `${owner}/${repo}`.toLowerCase()) { + core.info("Fork pull requests are unsupported; no SFL gate was published"); + return; + } + const currentBase = pull.data.base.sha; + const currentHead = pull.data.head.sha; + const configuredBaseBranch = process.env.SFL_REVIEW_BASE_BRANCH || ""; + if (!configuredBaseBranch) throw new Error("The deployed SFL review base branch is empty"); + if (defaultBranch !== configuredBaseBranch) { + const reason = `Repository default branch ${defaultBranch} differs from deployed SFL review base ${configuredBaseBranch}; redeploy SFL before accepting review results`; + await github.rest.checks.create({ + owner, + repo, + name: "SFL Reviewer Gate Runner", + head_sha: currentHead, + status: "completed", + conclusion: "failure", + completed_at: new Date().toISOString(), + details_url: artifact.html_url || pull.data.html_url, + external_id: `sfl-codex-review:default-branch-mismatch:${encodeURIComponent(configuredBaseBranch)}:${encodeURIComponent(defaultBranch)}`, + output: {title: "SFL Codex review blocked", summary: reason}, + }); + core.setFailed(reason); + return; + } + const match = (artifact.body || "").match( + /\*\*Reviewed commit:\*\*\s+`([0-9a-f]{7,40})`/i, + ); + let resolvedSha = ""; + if (match) { + try { + const commit = await github.rest.repos.getCommit({ + owner, repo, ref: match[1], + }); + resolvedSha = commit.data.sha; + } catch (error) { + if (error.status !== 404 && error.status !== 422) throw error; + } + } + const artifactBindsCurrentHead = eventName === "issue_comment" + ? Boolean(match) && resolvedSha.toLowerCase() === currentHead.toLowerCase() + : reviewCommit.toLowerCase() === currentHead.toLowerCase() && + (!match || resolvedSha.toLowerCase() === currentHead.toLowerCase()); + const artifactIdentity = `${eventName === "issue_comment" ? "c" : "r"}${artifactId}`; + const artifactSuffix = `:artifact:${artifactIdentity}`; + const artifactChecks = await github.paginate( + github.rest.checks.listForRef, + { + owner, + repo, + ref: currentHead, + check_name: "SFL Reviewer Gate Runner", + filter: "all", + per_page: 100, + }, + response => response.data.check_runs, + ); + const artifactAlreadyConsumed = artifactChecks.some( + check => check.app && check.app.id === 15368 && + (check.external_id || "").endsWith(artifactSuffix), + ); + if (artifactAlreadyConsumed) { + core.info("This exact Codex artifact already completed a review request"); + return; + } + const artifactTime = Date.parse(artifact.created_at || artifact.submitted_at || ""); + const isOwnerRequest = comment => + ((comment.user && comment.user.login) || "").toLowerCase() === owner.toLowerCase(); + const headMarker = `/.exec(comment.body || ""); + return isOwnerRequest(comment) && contextMatch && contextMatch[1] && + (comment.body || "") === `@codex review\n\n${baseMarker}context=${contextMatch[1]} -->` && + Number.isFinite(requestTime) && requestTime <= artifactTime && + Number.isFinite(updatedTime) && updatedTime === requestTime; + }).sort((left, right) => + left.requestTime - right.requestTime || left.comment.id - right.comment.id); + latestPotentialRequest = potentialRequests[potentialRequests.length - 1]; + const visibleRegistrations = registeredRequestIdsFromStatuses(requestStatuses); + latestPotentialRequestRegistered = Boolean(latestPotentialRequest) && + visibleRegistrations.has(latestPotentialRequest.comment.id); + if (latestPotentialRequestRegistered) break; + if (attempt < 11) await new Promise(resolve => setTimeout(resolve, 5000)); + } + if (!latestPotentialRequestRegistered) { + core.info("No registered Codex review request materialized for this artifact"); + return; + } + const registeredRequestIds = registeredRequestIdsFromStatuses(requestStatuses); + // BEGIN TESTABLE REQUEST ELIGIBILITY + const requestContextFromBody = body => { + const contextMatch = /;context=(.*?) -->/.exec(body || ""); + return contextMatch && contextMatch[1] ? contextMatch[1] : ""; + }; + const requestOrder = (left, right) => + left.requestTime - right.requestTime || left.comment.id - right.comment.id; + const requestCandidates = (comments, beforeTime, registrations = registeredRequestIds) => comments.map( + comment => ({ + comment, + requestTime: Date.parse(comment.created_at || ""), + updatedTime: Date.parse(comment.updated_at || ""), + }), + ).filter( + ({comment, requestTime, updatedTime}) => isOwnerRequest(comment) && + registrations.has(comment.id) && + (comment.body || "").includes(baseMarker) && + (comment.body || "") === `@codex review\n\n${baseMarker}context=${requestContextFromBody(comment.body)} -->` && + Number.isFinite(requestTime) && requestTime <= beforeTime && + Number.isFinite(updatedTime) && updatedTime === requestTime, + ).sort(requestOrder); + const hasTerminalGateBefore = (request, contextToken, nextRequestTime, checkRuns) => { + const prefix = requestGateExternalIdPrefix( + pullNumber, + currentBase, + contextToken, + request.comment.id, + request.requestTime, + ); + return checkRuns.some(check => { + const completedTime = Date.parse(check.completed_at || ""); + return check.app && check.app.id === 15368 && + check.status === "completed" && + (check.external_id === prefix || + (check.external_id || "").startsWith(`${prefix}:artifact:`)) && + Number.isFinite(completedTime) && completedTime < nextRequestTime; + }); + }; + const eligibleReviewRequests = ( + comments, checkRuns, beforeTime, registrations = registeredRequestIds, + ) => { + const previousByContext = new Map(); + const eligible = []; + for (const candidate of requestCandidates(comments, beforeTime, registrations)) { + const requestContext = requestContextFromBody(candidate.comment.body); + if (!requestContext) continue; + const previous = previousByContext.get(requestContext); + if (!previous || + hasTerminalGateBefore(previous, requestContext, candidate.requestTime, checkRuns)) { + eligible.push({...candidate, contextToken: requestContext}); + previousByContext.set(requestContext, candidate); + } + } + return eligible; + }; + // END TESTABLE REQUEST ELIGIBILITY + const candidateRequests = Number.isFinite(artifactTime) + ? eligibleReviewRequests(triggerComments, artifactChecks, artifactTime) + .sort((left, right) => -requestOrder(left, right)) + : []; + const pendingRequest = candidateRequests[0]; + if (pendingRequest) { + const pendingRequestPrefix = requestGateExternalIdPrefix( + pullNumber, + currentBase, + pendingRequest.contextToken, + pendingRequest.comment.id, + pendingRequest.requestTime, + ); + const pendingRequestAlreadyTerminal = artifactChecks.some( + check => check.app && check.app.id === 15368 && + check.status === "completed" && + (check.external_id === pendingRequestPrefix || + (check.external_id || "").startsWith(`${pendingRequestPrefix}:artifact:`)), + ); + if (pendingRequestAlreadyTerminal) { + core.info("This Codex review request already has a terminal SFL gate"); + return; + } + } + const activeInvalidationRuns = async () => { + const activeStatuses = ["requested", "queued", "in_progress", "waiting", "pending"]; + const events = ["pull_request_target", "push", "issue_comment"]; + const pages = await Promise.all(activeStatuses.map(status => github.paginate( + github.rest.actions.listWorkflowRuns, + { + owner, + repo, + workflow_id: "sfl-pr-review-auto.yml", + status, + per_page: 100, + }, + response => response.data.workflow_runs, + ))); + return pages.flat().filter( + run => run.id !== context.runId && events.includes(run.event) && + isActiveInvalidationRun(run, pullNumber, owner), + ); + }; + const failForPendingInvalidation = async pendingRun => { + if (!artifactBindsCurrentHead) { + core.info("The Codex artifact does not bind to the current head; it cannot complete the pending request"); + return; + } + const reason = `${pendingRun.event} invalidation run ${pendingRun.id} is still active; the Codex result cannot supersede its invalidation`; + let externalId = `sfl-codex-review:pending-invalidation:${pendingRun.id}:artifact:${artifactIdentity}`; + if (pendingRequest) { + const contextMatch = /;context=(.*?) -->/.exec(pendingRequest.comment.body || ""); + if (contextMatch && contextMatch[1]) { + externalId = requestGateExternalId( + pullNumber, + currentBase, + contextMatch[1], + pendingRequest.comment.id, + pendingRequest.requestTime, + artifactIdentity, + ); + } + } + await github.rest.checks.create({ + owner, + repo, + name: "SFL Reviewer Gate Runner", + head_sha: currentHead, + status: "completed", + conclusion: "failure", + completed_at: new Date().toISOString(), + details_url: artifact.html_url || pull.data.html_url, + external_id: externalId, + output: {title: "SFL Codex review blocked", summary: reason}, + }); + core.setFailed(reason); + }; + const pullLifecycleToken = async () => { + const events = await github.paginate( + "GET /repos/{owner}/{repo}/issues/{issue_number}/events", + {owner, repo, issue_number: pullNumber, per_page: 100}, + ); + return events.filter(event => event.event === "closed" || event.event === "reopened") + .reduce((latest, event) => Math.max(latest, event.id || 0), 0); + }; + const initialLifecycleToken = await pullLifecycleToken(); + let pendingInvalidationRuns = []; + let observedInvalidationRun = false; + for (let attempt = 0; attempt < 12; attempt += 1) { + pendingInvalidationRuns = await activeInvalidationRuns(); + if (pendingInvalidationRuns.length > 0) { + observedInvalidationRun = true; + core.info(`Waiting for ${pendingInvalidationRuns[0].event} invalidation run ${pendingInvalidationRuns[0].id} before publishing the Codex result`); + } else if (observedInvalidationRun || attempt >= 5) { + break; + } else { + core.info("Allowing an invalidation run time to materialize before publishing the Codex result"); + } + await new Promise(resolve => setTimeout(resolve, 5000)); + } + if (pendingInvalidationRuns.length > 0) { + await failForPendingInvalidation(pendingInvalidationRuns[0]); + return; + } + const reviewContextToken = checkRuns => { + const invalidation = checkRuns.filter( + check => check.app && check.app.id === 15368 && + ((check.external_id || "").startsWith("sfl-codex-review:pull-context:") || + (check.external_id || "").startsWith("sfl-codex-review:base-advance:")), + ).sort((left, right) => right.id - left.id)[0]; + return invalidation ? invalidation.external_id : "none"; + }; + const initialChecks = await github.paginate( + github.rest.checks.listForRef, + { + owner, + repo, + ref: currentHead, + check_name: "SFL Reviewer Gate Runner", + filter: "all", + per_page: 100, + }, + response => response.data.check_runs, + ); + const contextToken = reviewContextToken(initialChecks); + const marker = ``; + const conflictingBaseRequest = triggerComments.some( + comment => isOwnerRequest(comment) && + (comment.body || "").startsWith("@codex review") && + (comment.body || "").includes(headMarker) && + !(comment.body || "").includes(baseMarker), + ); + const matchingRequests = candidateRequests.filter( + ({comment}) => (comment.body || "").includes(marker), + ); + const request = matchingRequests[0]; + const requestMatchesCurrentBase = Boolean(request) && !conflictingBaseRequest; + const openPullsForCurrentHead = async () => { + const candidates = await github.paginate(github.rest.pulls.list, { + owner, repo, state: "open", per_page: 100, + }); + return candidates.filter( + candidate => candidate.head && candidate.head.sha === currentHead, + ); + }; + const openPulls = await openPullsForCurrentHead(); + const openPullCount = openPulls.length; - dispatch_args=( - --method POST - "repos/${REPOSITORY}/actions/workflows/sfl-pr-review.lock.yml/dispatches" - -f "ref=${TRUSTED_REF}" - -f "inputs[item_number]=${PR_NUMBER}" - -f "inputs[aw_context]=${AW_CONTEXT}" - ) + let result = classifyCodexArtifact({ + eventName, + currentHead, + resolvedSha, + reviewCommit, + requestMatchesCurrentBase, + openPullCount, + inlineCount, + artifact, + }); + if (request && request.requestTime === artifactTime) { + result = { + action: "failure", + reason: "Codex artifact and registered request have ambiguous same-second ordering; retry the review request", + }; + } + core.info(result.reason); + if (result.action === "ignore") return; - base_workflow="$(gh api \ - --method GET \ - "repos/${REPOSITORY}/contents/.github/workflows/sfl-pr-review.lock.yml" \ - -f "ref=${TRUSTED_REF}" \ - -H "Accept: application/vnd.github.raw+json")" - if grep -qE '^\s+head_sha:\s*$' <<< "${base_workflow}"; then - dispatch_args+=(-f "inputs[head_sha]=${HEAD_SHA}") - fi - if grep -qE '^\s+base_sha:\s*$' <<< "${base_workflow}"; then - dispatch_args+=(-f "inputs[base_sha]=${BASE_SHA}") - fi - if grep -qE '^\s+retry_count:\s*$' <<< "${base_workflow}"; then - dispatch_args+=(-f "inputs[retry_count]=0") - fi - if grep -qE '^\s+review_effort:\s*$' <<< "${base_workflow}"; then - dispatch_args+=(-f "inputs[review_effort]=${REVIEW_EFFORT}") - fi - DISPATCH_ID="auto-${GITHUB_RUN_ID}" - if ! grep -qE '^\s+dispatch_id:\s*$' <<< "${base_workflow}"; then - echo "::error::The installed reviewer does not support exact dispatch correlation" - exit 1 - fi - dispatch_args+=(-f "inputs[dispatch_id]=${DISPATCH_ID}") + const publishPull = await github.rest.pulls.get({ + owner, repo, pull_number: pullNumber, + }); + const publishOpenPulls = await openPullsForCurrentHead(); + if (publishPull.data.state !== "open" || + publishPull.data.head.sha !== currentHead || + publishPull.data.base.sha !== currentBase || + publishOpenPulls.length !== 1 || + publishOpenPulls[0].number !== pullNumber) { + core.info("The pull request context changed before the Codex result could be published"); + return; + } - BEFORE_RUN_ID=$(gh api \ - "repos/${REPOSITORY}/actions/workflows/sfl-pr-review.lock.yml/runs?event=workflow_dispatch&per_page=100" \ - | jq -r \ - --arg title "$EXPECTED_TITLE" ' - [.workflow_runs[] - | select(.display_title | startswith($title))] - | sort_by(.id) - | last - | .id // 0 - ') - gh api "${dispatch_args[@]}" - consume_review_label "$PR_LABELS" - for ATTEMPT in $(seq 1 15); do - REVIEW_RUN_ID=$(gh api \ - "repos/${REPOSITORY}/actions/workflows/sfl-pr-review.lock.yml/runs?event=workflow_dispatch&per_page=100" \ - | jq -r \ - --arg title "$EXPECTED_TITLE" \ - --arg dispatch_id "$DISPATCH_ID" \ - --argjson before_run_id "$BEFORE_RUN_ID" ' - [.workflow_runs[] - | select(.display_title | startswith($title)) - | select(.display_title | endswith(" dispatch=" + $dispatch_id)) - | select(.id > $before_run_id)] - | sort_by(.id) - | last - | .id // empty - ') - if [[ "$REVIEW_RUN_ID" =~ ^[1-9][0-9]*$ ]]; then - echo "review-run-id=${REVIEW_RUN_ID}" >> "$GITHUB_OUTPUT" - exit 0 - fi - sleep 2 - done - echo "::error::Could not resolve the dispatched SFL review run" - exit 1 + const title = result.action === "success" + ? "SFL Codex review passed" + : "SFL Codex review failed"; + const checks = await github.paginate( + github.rest.checks.listForRef, + { + owner, + repo, + ref: currentHead, + check_name: "SFL Reviewer Gate Runner", + filter: "all", + per_page: 100, + }, + response => response.data.check_runs, + ); + const requestTime = request && request.requestTime; + const hasNewerMatchingRequest = (comments, checkRuns, statuses) => + eligibleReviewRequests( + comments, + checkRuns, + Number.POSITIVE_INFINITY, + registeredRequestIdsFromStatuses(statuses), + ).some( + candidate => candidate.contextToken === contextToken && + (candidate.requestTime > requestTime || + (candidate.requestTime === requestTime && + candidate.comment.id > request.comment.id)), + ); + if (reviewContextToken(checks) !== contextToken) { + core.info("The matching Codex request is not bound to the latest pull context"); + return; + } + const finalInvalidationRuns = await activeInvalidationRuns(); + if (finalInvalidationRuns.length > 0) { + await failForPendingInvalidation(finalInvalidationRuns[0]); + return; + } + const latestTriggerComments = await github.paginate(github.rest.issues.listComments, { + owner, repo, issue_number: pullNumber, per_page: 100, + }); + const latestRequestChecks = await github.paginate( + github.rest.checks.listForRef, + { + owner, + repo, + ref: currentHead, + check_name: "SFL Reviewer Gate Runner", + filter: "all", + per_page: 100, + }, + response => response.data.check_runs, + ); + const latestRequestStatuses = await github.paginate( + github.rest.repos.listCommitStatusesForRef, + {owner, repo, ref: currentHead, per_page: 100}, + ); + if (hasNewerMatchingRequest( + latestTriggerComments, latestRequestChecks, latestRequestStatuses, + )) { + core.info("A newer Codex review request superseded this artifact before publication"); + return; + } + const externalIdPrefix = requestGateExternalIdPrefix( + pullNumber, currentBase, contextToken, request.comment.id, requestTime, + ); + const externalId = requestGateExternalId( + pullNumber, currentBase, contextToken, request.comment.id, requestTime, artifactIdentity, + ); + const existing = latestRequestChecks.find( + check => check.app && check.app.id === 15368 && + check.status === "completed" && + (check.external_id === externalIdPrefix || + (check.external_id || "").startsWith(`${externalIdPrefix}:artifact:`)), + ); + if (existing) { + core.info("This Codex review request already has a terminal SFL gate"); + return; + } + const detailsURL = artifact.html_url || pull.data.html_url; + if (result.action !== "success") { + const publishedCompletedAt = new Date().toISOString(); + await github.rest.checks.create({ + owner, + repo, + name: "SFL Reviewer Gate Runner", + head_sha: currentHead, + status: "completed", + conclusion: "failure", + completed_at: publishedCompletedAt, + details_url: detailsURL, + external_id: externalId, + output: {title, summary: result.reason}, + }); + core.setFailed(result.reason); + return; + } + const published = await github.rest.checks.create({ + owner, + repo, + name: "SFL Reviewer Gate Runner", + head_sha: currentHead, + status: "in_progress", + started_at: new Date().toISOString(), + details_url: detailsURL, + external_id: externalId, + output: {title: "SFL Codex review validating", summary: result.reason}, + }); + const publicationState = async () => { + const [ + currentPull, + openPulls, + comments, + checkRuns, + statuses, + lifecycleToken, + invalidationRuns, + ] = await Promise.all([ + github.rest.pulls.get({owner, repo, pull_number: pullNumber}), + openPullsForCurrentHead(), + github.paginate(github.rest.issues.listComments, { + owner, repo, issue_number: pullNumber, per_page: 100, + }), + github.paginate( + github.rest.checks.listForRef, + { + owner, + repo, + ref: currentHead, + check_name: "SFL Reviewer Gate Runner", + filter: "all", + per_page: 100, + }, + response => response.data.check_runs, + ), + github.paginate( + github.rest.repos.listCommitStatusesForRef, + {owner, repo, ref: currentHead, per_page: 100}, + ), + pullLifecycleToken(), + activeInvalidationRuns(), + ]); + return { + pull: currentPull.data, + openPulls, + contextToken: reviewContextToken(checkRuns), + supersededRequest: hasNewerMatchingRequest(comments, checkRuns, statuses), + lifecycleChanged: lifecycleToken !== initialLifecycleToken, + invalidationRuns, + }; + }; + const publicationChanged = state => state.pull.state !== "open" || + state.pull.head.sha !== currentHead || + state.pull.base.sha !== currentBase || + state.openPulls.length !== 1 || + state.openPulls[0].number !== pullNumber || + state.supersededRequest || + state.lifecycleChanged || + state.invalidationRuns.length > 0 || + state.contextToken !== contextToken; + const publicationChangeReason = state => state.supersededRequest + ? "A newer Codex review request superseded this result while it was being published" + : state.lifecycleChanged + ? "The pull request was closed or reopened while the Codex result was being published" + : state.invalidationRuns.length > 0 + ? "An SFL invalidation started while the Codex result was being published" + : "The pull request context changed while the Codex result was being published"; + const failPublishedSuccess = async changedReason => { + await github.rest.checks.update({ + owner, + repo, + check_run_id: published.data.id, + status: "completed", + conclusion: "failure", + completed_at: new Date().toISOString(), + details_url: detailsURL, + external_id: externalId, + output: {title: "SFL Codex review invalidated", summary: changedReason}, + }); + core.setFailed(changedReason); + }; + const confirmedState = await publicationState(); + if (publicationChanged(confirmedState)) { + await failPublishedSuccess(publicationChangeReason(confirmedState)); + return; + } + await github.rest.checks.update({ + owner, + repo, + check_run_id: published.data.id, + status: "completed", + conclusion: "success", + completed_at: new Date().toISOString(), + details_url: detailsURL, + external_id: externalId, + output: {title, summary: result.reason}, + }); + const postSuccessState = await publicationState(); + if (publicationChanged(postSuccessState)) { + await failPublishedSuccess(publicationChangeReason(postSuccessState)); + } - approval: - name: SFL Reviewer Gate Runner - needs: [comment-command, dispatch] + invalidate-pull-context: + name: Invalidate Codex review for changed pull context if: >- - always() && - (github.event.action != 'labeled' || - github.event.label.name == 'sfl-review') && - (github.event_name != 'issue_comment' || - (github.event.action == 'created' && - github.event.issue.pull_request && - github.event.comment.body == '@sfl-app review' && - contains(fromJSON('["OWNER","MEMBER","COLLABORATOR"]'), github.event.comment.author_association))) + github.event_name == 'pull_request_target' && + (github.event.action == 'opened' || + github.event.action == 'reopened' || + github.event.action == 'synchronize' || + github.event.changes.base) runs-on: ubuntu-latest - timeout-minutes: 120 + timeout-minutes: 5 + concurrency: + group: sfl-codex-review-pull-context-${{ github.repository }}-${{ github.event.pull_request.number }} + cancel-in-progress: true permissions: actions: read checks: write contents: read pull-requests: read steps: - - name: Configure GH_HOST for enterprise compatibility - shell: bash - run: | - GH_HOST="${GITHUB_SERVER_URL#https://}" - GH_HOST="${GH_HOST#http://}" - echo "GH_HOST=${GH_HOST}" >> "$GITHUB_ENV" - - - name: Resolve immutable approval context - id: approval-context - env: - BASE_SHA: ${{ github.event.pull_request.base.sha || needs.comment-command.outputs.base-sha }} - EVENT_NAME: ${{ github.event_name }} - GH_TOKEN: ${{ github.token }} - HEAD_SHA: ${{ github.event.pull_request.head.sha || needs.comment-command.outputs.head-sha }} - PR_NUMBER: ${{ github.event.pull_request.number || needs.comment-command.outputs.pr-number || github.event.issue.number }} - REPOSITORY: ${{ github.repository }} - run: | - set -euo pipefail - if [ "$EVENT_NAME" = "issue_comment" ] \ - && { [ -z "$BASE_SHA" ] || [ -z "$HEAD_SHA" ]; }; then - PR=$(gh api "repos/${REPOSITORY}/pulls/${PR_NUMBER}") - BASE_SHA=$(jq -r '.base.sha // ""' <<< "$PR") - HEAD_SHA=$(jq -r '.head.sha // ""' <<< "$PR") - fi - if ! [[ "$PR_NUMBER" =~ ^[1-9][0-9]*$ ]] \ - || ! [[ "$BASE_SHA" =~ ^[0-9a-f]{40}$ ]] \ - || ! [[ "$HEAD_SHA" =~ ^[0-9a-f]{40}$ ]]; then - echo "::error::Could not resolve immutable SFL approval context" - exit 1 - fi - echo "base-sha=${BASE_SHA}" >> "$GITHUB_OUTPUT" - echo "head-sha=${HEAD_SHA}" >> "$GITHUB_OUTPUT" - echo "pr-number=${PR_NUMBER}" >> "$GITHUB_OUTPUT" - - - name: Initialize head approval check - id: approval-check - env: - BASE_SHA: ${{ steps.approval-context.outputs.base-sha }} - GH_TOKEN: ${{ github.token }} - HEAD_SHA: ${{ steps.approval-context.outputs.head-sha }} - PR_NUMBER: ${{ steps.approval-context.outputs.pr-number }} - REPOSITORY: ${{ github.repository }} - run: | - set -euo pipefail - payload=$(jq -nc \ - --arg details_url "${GITHUB_SERVER_URL}/${GITHUB_REPOSITORY}/actions/runs/${GITHUB_RUN_ID}" \ - --arg external_id "sfl-review-approval:${PR_NUMBER}:${BASE_SHA}:${HEAD_SHA}:${GITHUB_RUN_ID}" \ - --arg head_sha "$HEAD_SHA" \ - --arg name "SFL Reviewer Approval" \ - '{ - name: $name, - head_sha: $head_sha, - status: "in_progress", - details_url: $details_url, - external_id: $external_id, - output: { - title: "SFL reviewer gate is running", - summary: "Validating the exact SFL reviewer run for the immutable pull request head." - } - }') - response=$(printf '%s' "$payload" | gh api \ - --method POST \ - "repos/${REPOSITORY}/check-runs" \ - --header "Accept: application/vnd.github+json" \ - --input -) - check_run_id=$(jq -r '.id // empty' <<< "$response") - if ! [[ "$check_run_id" =~ ^[1-9][0-9]*$ ]]; then - echo "::error::GitHub did not return the immutable-head approval check ID" - exit 1 - fi - echo "check-run-id=${check_run_id}" >> "$GITHUB_OUTPUT" - - - name: Wait for exact SFL review run - id: wait-review - continue-on-error: true - env: - BASE_SHA: ${{ steps.approval-context.outputs.base-sha }} - COMMENT_COMMAND_RESULT: ${{ needs.comment-command.result }} - DEFAULT_BRANCH: ${{ github.event.repository.default_branch }} - DISPATCH_RESULT: ${{ needs.dispatch.result }} - EVENT_NAME: ${{ github.event_name }} - EXPECTED_REVIEW_RUN_ID: ${{ needs.dispatch.outputs.expected-review-run-id || needs.comment-command.outputs.expected-review-run-id }} - GH_TOKEN: ${{ github.token }} - HEAD_SHA: ${{ steps.approval-context.outputs.head-sha }} - PR_NUMBER: ${{ steps.approval-context.outputs.pr-number }} - REPOSITORY: ${{ github.repository }} - REVIEWER_INSTALLED: ${{ needs.dispatch.outputs.reviewer-installed || needs.comment-command.outputs.reviewer-installed }} - SFL_ENABLED: ${{ vars.SFL_ENABLED }} - run: | - set -euo pipefail - if [ "$SFL_ENABLED" = "false" ]; then - echo "::error::SFL reviewer maintenance mode is active; required reviewer gate fails closed" - exit 1 - fi - if [ "$EVENT_NAME" = "issue_comment" ] \ - && [ "$COMMENT_COMMAND_RESULT" != "success" ]; then - echo "::error::SFL comment command job concluded ${COMMENT_COMMAND_RESULT}; refusing to bootstrap" - exit 1 - fi - if [ "$EVENT_NAME" != "issue_comment" ] \ - && [ "$DISPATCH_RESULT" != "success" ]; then - echo "::error::SFL dispatch job concluded ${DISPATCH_RESULT}; refusing to bootstrap" - exit 1 - fi - if [ "$REVIEWER_INSTALLED" != "true" ]; then - echo "Reviewer bootstrap deployment detected; gate activates after the compatible lock reaches the default branch" - exit 0 - fi - EXPECTED_TITLE="SFL PR Review #${PR_NUMBER} ${BASE_SHA}:${HEAD_SHA} retry=" - - # BEGIN TESTABLE AUTO GATE RUN VALIDATION - validate_review_run() { - local repository="$1" - local default_branch="$2" - local expected_title="$3" - jq -e \ - --arg repository "$repository" \ - --arg default_branch "$default_branch" \ - --arg expected_title "$expected_title" ' - .path == ".github/workflows/sfl-pr-review.lock.yml" - and .event == "workflow_dispatch" - and .head_repository.full_name == $repository - and .head_branch == $default_branch - and ((.display_title // .name // "") | - startswith($expected_title)) - ' >/dev/null - } - # END TESTABLE AUTO GATE RUN VALIDATION - - if [ -z "$EXPECTED_REVIEW_RUN_ID" ]; then - if [ "$EVENT_NAME" = "issue_comment" ]; then - echo "::error::Could not resolve the exact comment-dispatched SFL review run" - exit 1 - fi - EXPECTED_REVIEW_RUN_ID=$(gh api \ - "repos/${REPOSITORY}/actions/workflows/sfl-pr-review.lock.yml/runs?event=workflow_dispatch&per_page=100" \ - --paginate --jq '.workflow_runs[]' \ - | jq -s -r \ - --arg title "$EXPECTED_TITLE" ' - [.[] | select(.display_title | startswith($title))] - | sort_by(.id) - | last - | .id // empty - ') - fi - if ! [[ "$EXPECTED_REVIEW_RUN_ID" =~ ^[1-9][0-9]*$ ]]; then - echo "::error::Could not resolve the exact SFL review run" - exit 1 - fi - - # 440 attempts at 15 seconds leaves roughly 110 minutes for the - # review, preserving headroom under the 120-minute wrapper ceiling. - for ATTEMPT in $(seq 1 440); do - if ! REVIEW_RUN=$(gh api \ - "repos/${REPOSITORY}/actions/runs/${EXPECTED_REVIEW_RUN_ID}"); then - echo "::warning::Actions run lookup failed (attempt ${ATTEMPT}/440)" - sleep 15 - continue - fi - if ! printf '%s' "$REVIEW_RUN" \ - | validate_review_run \ - "$REPOSITORY" \ - "$DEFAULT_BRANCH" \ - "$EXPECTED_TITLE"; then - echo "::error::Rejected untrusted SFL review run ${EXPECTED_REVIEW_RUN_ID}" - exit 1 - fi + - name: Fail the immutable-head gate until the new diff is reviewed + uses: actions/github-script@3a2844b7e9c422d3c10d287c895573f7108da1b3 # v9.0.0 + with: + github-token: ${{ github.token }} + script: | + // BEGIN TESTABLE INVALIDATION ORDER + function supersedesInvalidation(externalId, eventTime, runId) { + const match = /^sfl-codex-review:(?:pull-context|base-advance):at:(\d+):(\d+)/.exec(externalId || ""); + if (!match) return false; + const candidateTime = Number(match[1]); + const candidateRunId = Number(match[2]); + if (!Number.isSafeInteger(candidateTime) || !Number.isSafeInteger(candidateRunId)) return false; + return candidateTime > eventTime || + (candidateTime === eventTime && candidateRunId > runId); + } + // END TESTABLE INVALIDATION ORDER - STATUS=$(echo "$REVIEW_RUN" | jq -r '.status // ""') - CONCLUSION=$(echo "$REVIEW_RUN" | jq -r '.conclusion // ""') - if [ "$STATUS" != "completed" ]; then - sleep 15 - continue - fi - if [ "$CONCLUSION" = "success" ]; then - echo "Exact SFL review run approved PR #${PR_NUMBER}" - exit 0 - fi - echo "::error::Exact SFL review run concluded ${CONCLUSION:-missing}" - exit 1 - done + const {owner, repo} = context.repo; + const pull = context.payload.pull_request; + const workflowRun = await github.rest.actions.getWorkflowRun({ + owner, repo, run_id: context.runId, + }); + const contextChangeTime = Date.parse(workflowRun.data.created_at || ""); + if (!Number.isFinite(contextChangeTime)) { + throw new Error("Could not determine the pull-context change event time"); + } + const invalidationId = `sfl-codex-review:pull-context:at:${contextChangeTime}:${context.runId}`; + const reviewerChecks = await github.paginate( + github.rest.checks.listForRef, + { + owner, + repo, + ref: pull.head.sha, + check_name: "SFL Reviewer Gate Runner", + filter: "all", + per_page: 100, + }, + response => response.data.check_runs, + ); + const existingInvalidation = reviewerChecks.some( + check => check.app && check.app.id === 15368 && check.external_id === invalidationId, + ); + if (existingInvalidation) { + core.info(`Pull request #${pull.number} already has this exact invalidation; skipping rerun`); + return; + } + const supersedingContext = reviewerChecks.find( + check => check.app && check.app.id === 15368 && + supersedesInvalidation(check.external_id, contextChangeTime, context.runId), + ); + if (supersedingContext) { + core.info(`Pull request #${pull.number} has a newer invalidation context; skipping stale invalidation`); + return; + } + const newerSuccess = reviewerChecks.some( + check => check.app && check.app.id === 15368 && + check.conclusion === "success" && + check.external_id && check.external_id.startsWith( + `sfl-codex-review:pull:${pull.number}:base:${pull.base.sha.toLowerCase()}:`, + ) && + check.external_id.includes(`:context:${encodeURIComponent(invalidationId)}:`), + ); + if (newerSuccess) { + core.info(`Pull request #${pull.number} already has a successful Codex gate completed after this context change`); + return; + } + const livePull = await github.rest.pulls.get({ + owner, repo, pull_number: pull.number, + }); + if (livePull.data.state !== "open" || + livePull.data.head.sha !== pull.head.sha || + livePull.data.base.sha !== pull.base.sha) { + core.info(`Pull request #${pull.number} changed again after this context event; skipping stale invalidation`); + return; + } + const reason = context.payload.action === "edited" + ? `Pull request base changed to ${pull.base.ref} at ${pull.base.sha}; request a new Codex review with gh sfl review --retry.` + : context.payload.action === "synchronize" + ? `Pull request head changed to ${pull.head.sha}; request a Codex review for this exact head and base.` + : `Pull request #${pull.number} opened or reopened for ${pull.base.ref} at ${pull.base.sha}; request a Codex review for this exact pull request context.`; + const finalChecks = await github.paginate( + github.rest.checks.listForRef, + { + owner, + repo, + ref: pull.head.sha, + check_name: "SFL Reviewer Gate Runner", + filter: "all", + per_page: 100, + }, + response => response.data.check_runs, + ); + const finalContextExists = finalChecks.some( + check => check.app && check.app.id === 15368 && + (check.external_id === invalidationId || + supersedesInvalidation(check.external_id, contextChangeTime, context.runId)), + ); + const finalSuccess = finalChecks.some( + check => check.app && check.app.id === 15368 && + check.conclusion === "success" && + check.external_id && check.external_id.startsWith( + `sfl-codex-review:pull:${pull.number}:base:${pull.base.sha.toLowerCase()}:`, + ) && + check.external_id.includes(`:context:${encodeURIComponent(invalidationId)}:`), + ); + if (finalContextExists || finalSuccess) { + core.info(`Pull request #${pull.number} received a newer context or successful Codex gate while invalidation was running`); + return; + } + await github.rest.checks.create({ + owner, + repo, + name: "SFL Reviewer Gate Runner", + head_sha: pull.head.sha, + status: "completed", + conclusion: "failure", + completed_at: new Date().toISOString(), + details_url: pull.html_url, + external_id: invalidationId, + output: {title: "SFL Codex review invalidated", summary: reason}, + }); + core.setFailed(reason); - echo "::error::Timed out waiting for exact SFL review run" - exit 1 + invalidate-base-advance: + name: Invalidate Codex reviews after base advance + if: github.event_name == 'push' + runs-on: ubuntu-latest + timeout-minutes: 30 + concurrency: + group: sfl-codex-review-base-advance-${{ github.repository }}-${{ github.ref }} + cancel-in-progress: true + permissions: + actions: read + checks: write + contents: read + pull-requests: read + steps: + - name: Fail gates for pull requests whose base branch advanced + uses: actions/github-script@3a2844b7e9c422d3c10d287c895573f7108da1b3 # v9.0.0 + with: + github-token: ${{ github.token }} + script: | + // BEGIN TESTABLE INVALIDATION ORDER + function supersedesInvalidation(externalId, eventTime, runId) { + const match = /^sfl-codex-review:(?:pull-context|base-advance):at:(\d+):(\d+)/.exec(externalId || ""); + if (!match) return false; + const candidateTime = Number(match[1]); + const candidateRunId = Number(match[2]); + if (!Number.isSafeInteger(candidateTime) || !Number.isSafeInteger(candidateRunId)) return false; + return candidateTime > eventTime || + (candidateTime === eventTime && candidateRunId > runId); + } + // END TESTABLE INVALIDATION ORDER - - name: Finalize head approval check - if: always() && steps.approval-check.outputs.check-run-id != '' - env: - APPROVAL_CHECK_ID: ${{ steps.approval-check.outputs.check-run-id }} - GH_TOKEN: ${{ github.token }} - HEAD_SHA: ${{ steps.approval-context.outputs.head-sha }} - PR_NUMBER: ${{ steps.approval-context.outputs.pr-number }} - REPOSITORY: ${{ github.repository }} - WAIT_OUTCOME: ${{ steps.wait-review.outcome }} - run: | - set -euo pipefail - conclusion=failure - title="SFL Reviewer Approval failed" - summary="The exact SFL reviewer run did not approve PR #${PR_NUMBER} at ${HEAD_SHA}." - if [ "$WAIT_OUTCOME" = "success" ]; then - conclusion=success - title="SFL Reviewer Approval passed" - summary="The exact SFL reviewer run approved PR #${PR_NUMBER} at ${HEAD_SHA}." - fi - payload=$(jq -nc \ - --arg completed_at "$(date -u +%Y-%m-%dT%H:%M:%SZ)" \ - --arg conclusion "$conclusion" \ - --arg summary "$summary" \ - --arg title "$title" \ - '{ - status: "completed", - conclusion: $conclusion, - completed_at: $completed_at, - output: {title: $title, summary: $summary} - }') - printf '%s' "$payload" | gh api \ - --method PATCH \ - "repos/${REPOSITORY}/check-runs/${APPROVAL_CHECK_ID}" \ - --header "Accept: application/vnd.github+json" \ - --input - > /dev/null - if [ "$conclusion" != "success" ]; then - exit 1 - fi + const {owner, repo} = context.repo; + const configuredBaseBranch = process.env.SFL_REVIEW_BASE_BRANCH || ""; + let baseRef = context.ref.replace(/^refs\/heads\//, ""); + let renamedBase = false; + if (context.payload.deleted) { + const repository = await github.rest.repos.get({owner, repo}); + const defaultBranch = repository.data.default_branch || ""; + if (!defaultBranch) throw new Error("Could not determine the repository default branch after base deletion"); + baseRef = defaultBranch; + renamedBase = defaultBranch !== configuredBaseBranch; + } + const workflowRun = await github.rest.actions.getWorkflowRun({ + owner, repo, run_id: context.runId, + }); + const baseAdvanceTime = Date.parse(workflowRun.data.created_at || ""); + if (!Number.isFinite(baseAdvanceTime)) { + throw new Error("Could not determine the base-advance event time"); + } + const openPulls = await github.paginate(github.rest.pulls.list, { + owner, repo, state: "open", base: baseRef, per_page: 100, + }); + const invalidatePull = async pull => { + if (pull.base.ref !== baseRef) return; + const invalidationId = `sfl-codex-review:base-advance:at:${baseAdvanceTime}:${context.runId}:${pull.number}`; + const reviewerChecks = await github.paginate( + github.rest.checks.listForRef, + { + owner, + repo, + ref: pull.head.sha, + check_name: "SFL Reviewer Gate Runner", + filter: "all", + per_page: 100, + }, + response => response.data.check_runs, + ); + const existingInvalidation = reviewerChecks.some( + check => check.app && check.app.id === 15368 && check.external_id === invalidationId, + ); + if (existingInvalidation) { + core.info(`Pull request #${pull.number} already has this exact invalidation; skipping rerun`); + return; + } + const supersedingContext = reviewerChecks.find( + check => check.app && check.app.id === 15368 && + supersedesInvalidation(check.external_id, baseAdvanceTime, context.runId), + ); + if (supersedingContext) { + core.info(`Pull request #${pull.number} has a newer invalidation context; skipping stale invalidation`); + return; + } + const newerSuccess = reviewerChecks.some( + check => check.app && check.app.id === 15368 && + check.conclusion === "success" && + check.external_id && check.external_id.startsWith( + `sfl-codex-review:pull:${pull.number}:base:${pull.base.sha.toLowerCase()}:`, + ) && + check.external_id.includes(`:context:${encodeURIComponent(invalidationId)}:`), + ); + if (newerSuccess) { + core.info(`Pull request #${pull.number} already has a successful Codex gate completed after this base advance`); + return; + } + const reason = renamedBase + ? `Repository default branch changed from deployed SFL review base ${configuredBaseBranch} to ${baseRef}; redeploy SFL before requesting a new Codex review.` + : `Pull request base ${baseRef} advanced to ${pull.base.sha}; request a new Codex review with gh sfl review --retry.`; + const finalChecks = await github.paginate( + github.rest.checks.listForRef, + { + owner, + repo, + ref: pull.head.sha, + check_name: "SFL Reviewer Gate Runner", + filter: "all", + per_page: 100, + }, + response => response.data.check_runs, + ); + const finalContextExists = finalChecks.some( + check => check.app && check.app.id === 15368 && + (check.external_id === invalidationId || + supersedesInvalidation(check.external_id, baseAdvanceTime, context.runId)), + ); + const finalSuccess = finalChecks.some( + check => check.app && check.app.id === 15368 && + check.conclusion === "success" && + check.external_id && check.external_id.startsWith( + `sfl-codex-review:pull:${pull.number}:base:${pull.base.sha.toLowerCase()}:`, + ) && + check.external_id.includes(`:context:${encodeURIComponent(invalidationId)}:`), + ); + if (finalContextExists || finalSuccess) { + core.info(`Pull request #${pull.number} received a newer context or successful Codex gate while invalidation was running`); + return; + } + await github.rest.checks.create({ + owner, + repo, + name: "SFL Reviewer Gate Runner", + head_sha: pull.head.sha, + status: "completed", + conclusion: "failure", + completed_at: new Date().toISOString(), + details_url: pull.html_url, + external_id: invalidationId, + output: {title: "SFL Codex review invalidated", summary: reason}, + }); + }; + const failures = []; + const batchSize = 10; + for (let offset = 0; offset < openPulls.length; offset += batchSize) { + const batch = openPulls.slice(offset, offset + batchSize); + const results = await Promise.allSettled(batch.map(invalidatePull)); + results.forEach((result, index) => { + if (result.status === "rejected") { + failures.push(`PR #${batch[index].number}: ${result.reason}`); + } + }); + } + if (failures.length > 0) { + throw new Error(`Base-advance invalidation failed after attempting every pull request:\n${failures.join("\n")}`); + } diff --git a/deployment/infrastructure/sfl-pr-review-recovery.yml b/deployment/infrastructure/sfl-pr-review-recovery.yml deleted file mode 100644 index f39ff86d..00000000 --- a/deployment/infrastructure/sfl-pr-review-recovery.yml +++ /dev/null @@ -1,577 +0,0 @@ -# HemSoft SFL reviewer platform v2 -name: SFL PR Review Recovery - -on: - workflow_run: - workflows: ["SFL PR Review"] - types: [completed] - -permissions: - contents: read - -jobs: - recover: - if: >- - vars.SFL_ENABLED != 'false' && - (github.event.workflow_run.conclusion == 'failure' || - github.event.workflow_run.conclusion == 'timed_out' || - github.event.workflow_run.conclusion == 'cancelled') && - github.event.workflow_run.path == '.github/workflows/sfl-pr-review.lock.yml' && - github.event.workflow_run.event == 'workflow_dispatch' && - github.event.workflow_run.head_repository.full_name == github.repository && - github.event.workflow_run.head_branch == github.event.repository.default_branch - runs-on: ubuntu-slim - permissions: - checks: write - contents: read - steps: - - name: Configure GH_HOST for enterprise compatibility - shell: bash - run: | - GH_HOST="${GITHUB_SERVER_URL#https://}" - GH_HOST="${GH_HOST#http://}" - echo "GH_HOST=${GH_HOST}" >> "$GITHUB_ENV" - - - name: Finalize interrupted review evidence - if: always() - uses: actions/github-script@3a2844b7e9c422d3c10d287c895573f7108da1b3 # v9.0.0 - env: - FAILED_RUN_CONCLUSION: ${{ github.event.workflow_run.conclusion }} - FAILED_RUN_ID: ${{ github.event.workflow_run.id }} - FAILED_RUN_TITLE: ${{ github.event.workflow_run.display_title }} - with: - github-token: ${{ github.token }} - script: | - const runId = Number(process.env.FAILED_RUN_ID); - const conclusion = process.env.FAILED_RUN_CONCLUSION; - const title = process.env.FAILED_RUN_TITLE || ''; - const match = title.match( - /^SFL PR Review #([1-9][0-9]*) ([0-9a-f]{40}):([0-9a-f]{40}) retry=[01] dispatch=[A-Za-z0-9._-]{1,64}$/ - ); - if (!Number.isSafeInteger(runId) || runId <= 0 || !match) { - core.setFailed('Cannot resolve immutable review evidence from the interrupted run'); - return; - } - - const pullNumber = Number(match[1]); - const baseSha = match[2]; - const headSha = match[3]; - const externalId = - `sfl-review:${pullNumber}:${baseSha}:${headSha}:${runId}`; - const checkRuns = await github.paginate( - github.rest.checks.listForRef, - { - owner: context.repo.owner, - repo: context.repo.repo, - ref: headSha, - check_name: 'SFL Review Evidence', - filter: 'all', - per_page: 100, - } - ); - const matches = checkRuns.filter(check => - check.external_id === externalId && check.app?.id === 15368 - ); - if (matches.length === 0) { - core.info(`Run ${runId} did not initialize an SFL Review Evidence check`); - return; - } - if (matches.length !== 1) { - core.setFailed( - `Run ${runId} has ${matches.length} matching SFL Review Evidence checks` - ); - return; - } - - const evidence = matches[0]; - if (evidence.status === 'completed') { - core.info(`SFL Review Evidence check ${evidence.id} is already complete`); - return; - } - await github.rest.checks.update({ - owner: context.repo.owner, - repo: context.repo.repo, - check_run_id: evidence.id, - name: 'SFL Review Evidence', - external_id: externalId, - status: 'completed', - conclusion: 'failure', - completed_at: new Date().toISOString(), - output: { - title: 'SFL review did not complete', - summary: - `Reviewer run ${runId} concluded ${conclusion} before ` + - 'review evidence could be finalized.', - text: 'replacement_retry_eligible=false', - }, - }); - core.info(`Finalized interrupted SFL Review Evidence check ${evidence.id}`); - - - name: Mint SFL GitHub App token - if: github.event.workflow_run.conclusion != 'cancelled' - id: app-token - uses: actions/create-github-app-token@bcd2ba49218906704ab6c1aa796996da409d3eb1 # v3.2.0 - with: - client-id: ${{ vars.SFL_APP_CLIENT_ID }} - private-key: ${{ secrets.SFL_APP_PRIVATE_KEY }} - owner: ${{ github.repository_owner }} - repositories: ${{ github.event.repository.name }} - permission-actions: write - permission-contents: read - permission-pull-requests: read - - - name: Retry missing review output once - if: github.event.workflow_run.conclusion != 'cancelled' - env: - DEFAULT_BRANCH: ${{ github.event.repository.default_branch }} - FAILED_RUN_CREATED_AT: ${{ github.event.workflow_run.created_at }} - FAILED_RUN_ID: ${{ github.event.workflow_run.id }} - GH_TOKEN: ${{ steps.app-token.outputs.token }} - REPOSITORY: ${{ github.repository }} - SFL_APP_SLUG: ${{ steps.app-token.outputs.app-slug }} - run: | - set -euo pipefail - - # BEGIN TESTABLE REVIEW RECOVERY POLICY - reconcile_review_count() { - local manifest_review_count="$1" - local published_review_count="$2" - - if [ "$manifest_review_count" -gt "$published_review_count" ]; then - echo "$manifest_review_count" - else - echo "$published_review_count" - fi - } - - decide_review_recovery() { - local terminal_signal_count="$1" - local review_count="$2" - local retry_count="$3" - local expected_head="$4" - local live_head="$5" - local expected_base="$6" - local live_base="$7" - local eligible="$8" - - if [ "$review_count" -eq 1 ]; then - echo "complete" - return 0 - fi - if [ "$review_count" -ne 0 ]; then - echo "invalid" - return 1 - fi - if [ "$terminal_signal_count" -ne 0 ]; then - echo "reported" - return 0 - fi - if [ "$eligible" != "true" ] \ - || [ "$live_head" != "$expected_head" ] \ - || [ "$live_base" != "$expected_base" ]; then - echo "stale" - return 0 - fi - if [ "$retry_count" -eq 0 ]; then - echo "retry" - return 0 - fi - echo "exhausted" - return 1 - } - - newer_run_suppresses_retry() { - local run_status="$1" - local provenance_loaded="$2" - local provenance_match="$3" - local title_match="$4" - local run_conclusion="${5:-}" - - # A newer run whose provenance matches this exact PR/base/head - # owns the current-state review, regardless of requested effort. - if [ "$provenance_match" = "true" ]; then - echo "suppress" - return 0 - fi - # A maintainer-cancelled newer run is an intentional terminal - # signal for this sealed PR/base/head, even when cancellation - # happened before its provenance artifact was uploaded. - if [ "$run_conclusion" = "cancelled" ] \ - && [ "$title_match" = "true" ]; then - echo "suppress" - return 0 - fi - # An active newer run whose provenance has not materialized yet - # suppresses only when its sealed display title identifies this - # exact PR/base/head. A completed run needs matching provenance: - # otherwise it may have failed before uploading the artifact and - # still needs this older run's recovery attempt. - if [ "$run_status" != "completed" ] \ - && [ "$provenance_loaded" != "true" ] \ - && [ "$title_match" = "true" ]; then - echo "suppress" - return 0 - fi - echo "allow" - return 0 - } - # END TESTABLE REVIEW RECOVERY POLICY - - ARTIFACT_DIR=$(mktemp -d) - trap 'rm -rf "$ARTIFACT_DIR"' EXIT - mkdir -p \ - "$ARTIFACT_DIR/agent" \ - "$ARTIFACT_DIR/provenance" \ - "$ARTIFACT_DIR/safe-outputs" - - PROVENANCE_LOADED="false" - for PROVENANCE_ATTEMPT in 1 2 3; do - rm -rf "$ARTIFACT_DIR/provenance" - mkdir -p "$ARTIFACT_DIR/provenance" - if gh run download "$FAILED_RUN_ID" \ - --repo "$REPOSITORY" \ - --name sfl-review-run-provenance \ - --dir "$ARTIFACT_DIR/provenance"; then - PROVENANCE_LOADED="true" - break - fi - if [ "$PROVENANCE_ATTEMPT" -lt 3 ]; then - sleep $((PROVENANCE_ATTEMPT * 5)) - fi - done - if [ "$PROVENANCE_LOADED" != "true" ]; then - echo "::error::Could not load exact review provenance for run ${FAILED_RUN_ID}" - exit 1 - fi - - AGENT_OUTPUT="$ARTIFACT_DIR/agent/agent_output.json" - PROVENANCE="$ARTIFACT_DIR/provenance/sfl-review-run-provenance.json" - SAFE_OUTPUT_ITEMS="$ARTIFACT_DIR/safe-outputs/safe-output-items.jsonl" - for AGENT_ATTEMPT in 1 2 3; do - rm -rf "$ARTIFACT_DIR/agent" - mkdir -p "$ARTIFACT_DIR/agent" - if gh run download "$FAILED_RUN_ID" \ - --repo "$REPOSITORY" \ - --name agent \ - --dir "$ARTIFACT_DIR/agent"; then - break - fi - if [ "$AGENT_ATTEMPT" -lt 3 ]; then - sleep $((AGENT_ATTEMPT * 5)) - fi - done - if ! gh run download "$FAILED_RUN_ID" \ - --repo "$REPOSITORY" \ - --name safe-outputs-items \ - --dir "$ARTIFACT_DIR/safe-outputs"; then - echo "No durable safe-output artifact exists for run ${FAILED_RUN_ID}; reconciling published reviews" - : > "$SAFE_OUTPUT_ITEMS" - fi - if [ ! -f "$PROVENANCE" ]; then - echo "::error::Review recovery provenance is incomplete" - exit 1 - fi - if ! jq -e \ - --arg repository "$REPOSITORY" \ - --arg workflow_run_id "$FAILED_RUN_ID" \ - ' - .repository == $repository and - .workflow_run_id == $workflow_run_id and - (.pr_number | type) == "number" and - (.pr_number | floor) == .pr_number and - .pr_number > 0 and - (.base_sha | test("^[0-9a-f]{40}$")) and - (.head_sha | test("^[0-9a-f]{40}$")) and - (.review_effort | test("^(low|medium|high)$")) and - (.retry_count == 0 or .retry_count == 1) - ' "$PROVENANCE" >/dev/null; then - echo "::error::Review recovery provenance is invalid or mismatched" - exit 1 - fi - - PR_NUMBER=$(jq -r '.pr_number' "$PROVENANCE") - BASE_SHA=$(jq -r '.base_sha' "$PROVENANCE") - HEAD_SHA=$(jq -r '.head_sha' "$PROVENANCE") - REVIEW_EFFORT=$(jq -r '.review_effort' "$PROVENANCE") - RETRY_COUNT=$(jq -r '.retry_count' "$PROVENANCE") - if ! MANIFEST_REVIEW_COUNT=$(jq -s \ - '[ - .[] | - select(.type == "submit_pull_request_review") - ] | length' \ - "$SAFE_OUTPUT_ITEMS"); then - echo "::error::Review safe-output manifest is malformed" - exit 1 - fi - - if [ -z "$SFL_APP_SLUG" ]; then - echo "::error::Could not identify the trusted SFL GitHub App" - exit 1 - fi - APP_LOGIN="${SFL_APP_SLUG}[bot]" - PUBLISHED_REVIEWS=$(gh api \ - "repos/${REPOSITORY}/pulls/${PR_NUMBER}/reviews?per_page=100" \ - --paginate \ - --slurp) - if ! PUBLISHED_REVIEW_COUNT=$(jq -r \ - --arg app_login "$APP_LOGIN" \ - --arg head_sha "$HEAD_SHA" \ - --arg workflow_run_id "$FAILED_RUN_ID" \ - ' - [ - .[] | - .[] | - select( - ((.user.login // "") | ascii_downcase) == - ($app_login | ascii_downcase) and - .commit_id == $head_sha and - ( - (.body // "") | - contains("/actions/runs/" + $workflow_run_id) - ) - ) - ] | length - ' <<< "$PUBLISHED_REVIEWS"); then - echo "::error::Could not reconcile published reviews for run ${FAILED_RUN_ID}" - exit 1 - fi - REVIEW_COUNT=$(reconcile_review_count \ - "$MANIFEST_REVIEW_COUNT" \ - "$PUBLISHED_REVIEW_COUNT") - - if [ "$REVIEW_COUNT" -eq 1 ]; then - echo "Run ${FAILED_RUN_ID} produced its formal review; no missing-output retry is allowed" - exit 0 - fi - if [ "$REVIEW_COUNT" -gt 1 ]; then - echo "::error::Run ${FAILED_RUN_ID} produced multiple formal reviews" - exit 1 - fi - - # gh-aw omits metadata-only signals from the durable manifest. A - # retry is safe only after proving the agent emitted no terminal - # missing-data/tool/incomplete-report signal. - if [ ! -f "$AGENT_OUTPUT" ]; then - echo "::error::Review agent output is unavailable; refusing an unproven retry" - exit 1 - fi - if ! TERMINAL_SIGNAL_COUNT=$(jq -r \ - ' - ( - if type == "array" then - . - elif type == "object" and (.items | type) == "array" then - .items - else - error("agent output must be an array or contain an items array") - end - ) as $items | - [ - $items[] | - select( - .type == "missing_data" or - .type == "missing_tool" or - .type == "report_incomplete" - ) - ] | length - ' "$AGENT_OUTPUT"); then - echo "::error::Review agent output is malformed; refusing an unproven retry" - exit 1 - fi - - PULL_REQUEST=$(gh api "repos/${REPOSITORY}/pulls/${PR_NUMBER}") - LIVE_HEAD=$(jq -r '.head.sha // empty' <<< "$PULL_REQUEST") - LIVE_BASE=$(jq -r '.base.sha // empty' <<< "$PULL_REQUEST") - ELIGIBLE=$(jq -r \ - --arg default_branch "$DEFAULT_BRANCH" \ - --arg repository "$REPOSITORY" \ - ' - ( - .state == "open" and - .draft == false and - .base.ref == $default_branch and - .head.repo.full_name == $repository - ) | tostring - ' <<< "$PULL_REQUEST") - - DECISION_STATUS=0 - DECISION=$(decide_review_recovery \ - "$TERMINAL_SIGNAL_COUNT" \ - "$REVIEW_COUNT" \ - "$RETRY_COUNT" \ - "$HEAD_SHA" \ - "$LIVE_HEAD" \ - "$BASE_SHA" \ - "$LIVE_BASE" \ - "$ELIGIBLE") || DECISION_STATUS=$? - case "$DECISION" in - complete) - exit 0 - ;; - reported) - echo "Run ${FAILED_RUN_ID} produced an explicit non-review safe output; preserving its failed gate without retry" - exit 0 - ;; - stale) - echo "PR #${PR_NUMBER} is no longer eligible at ${HEAD_SHA}; suppressing stale retry" - exit 0 - ;; - exhausted) - echo "::error::PR #${PR_NUMBER} produced no formal review after its one trusted retry" - exit "$DECISION_STATUS" - ;; - invalid) - echo "::error::PR #${PR_NUMBER} produced an invalid formal-review count" - exit "$DECISION_STATUS" - ;; - retry) - ;; - *) - echo "::error::Unknown review recovery decision: ${DECISION}" - exit 1 - ;; - esac - - WORKFLOW_RUNS=$(gh api \ - "repos/${REPOSITORY}/actions/workflows/sfl-pr-review.lock.yml/runs?event=workflow_dispatch&per_page=100" \ - --paginate \ - --slurp) - EXPECTED_REVIEW_TITLE_PREFIX="SFL PR Review #${PR_NUMBER} ${BASE_SHA}:${HEAD_SHA} retry=" - NEWER_RUNS=$(jq -r \ - --arg created_at "$FAILED_RUN_CREATED_AT" \ - ' - .[] | - .workflow_runs[] | - select(.created_at > $created_at) | - [.id, .status, (.display_title // ""), (.conclusion // "")] | - @tsv - ' <<< "$WORKFLOW_RUNS") - while IFS=$'\t' read -r NEWER_RUN_ID NEWER_RUN_STATUS NEWER_RUN_TITLE NEWER_RUN_CONCLUSION; do - [ -n "$NEWER_RUN_ID" ] || continue - NEWER_PROVENANCE_DIR="$ARTIFACT_DIR/newer-${NEWER_RUN_ID}" - # The sealed title is computed first: an unrelated newer run - # provably targets a different PR/base/head, so it earns a - # single provenance attempt instead of the full in-flight - # polling window — 12 attempts (~330s) per unrelated run could - # outlast the recovery grace and hand the failed PR - # a premature sfl-human-required escalation. - NEWER_TITLE_MATCH="false" - case "$NEWER_RUN_TITLE" in - "$EXPECTED_REVIEW_TITLE_PREFIX"*) NEWER_TITLE_MATCH="true" ;; - esac - NEWER_PROVENANCE_ATTEMPTS=1 - if [ "$NEWER_RUN_STATUS" != "completed" ] \ - && [ "$NEWER_TITLE_MATCH" = "true" ]; then - NEWER_PROVENANCE_ATTEMPTS=12 - fi - NEWER_PROVENANCE_LOADED="false" - NEWER_PROVENANCE_MATCH="false" - for NEWER_PROVENANCE_ATTEMPT in $(seq 1 "$NEWER_PROVENANCE_ATTEMPTS"); do - rm -rf "$NEWER_PROVENANCE_DIR" - mkdir -p "$NEWER_PROVENANCE_DIR" - if gh run download "$NEWER_RUN_ID" \ - --repo "$REPOSITORY" \ - --name sfl-review-run-provenance \ - --dir "$NEWER_PROVENANCE_DIR" \ - >/dev/null 2>&1; then - NEWER_PROVENANCE_LOADED="true" - if jq -e \ - --argjson pr_number "$PR_NUMBER" \ - --arg base_sha "$BASE_SHA" \ - --arg head_sha "$HEAD_SHA" \ - ' - .pr_number == $pr_number and - .base_sha == $base_sha and - .head_sha == $head_sha - ' "$NEWER_PROVENANCE_DIR/sfl-review-run-provenance.json" \ - >/dev/null; then - NEWER_PROVENANCE_MATCH="true" - fi - break - fi - if [ "$NEWER_PROVENANCE_ATTEMPT" -lt "$NEWER_PROVENANCE_ATTEMPTS" ]; then - sleep $((NEWER_PROVENANCE_ATTEMPT * 5)) - fi - done - if [ "$(newer_run_suppresses_retry \ - "$NEWER_RUN_STATUS" \ - "$NEWER_PROVENANCE_LOADED" \ - "$NEWER_PROVENANCE_MATCH" \ - "$NEWER_TITLE_MATCH" \ - "$NEWER_RUN_CONCLUSION")" != "suppress" ]; then - continue - fi - if [ "$NEWER_PROVENANCE_MATCH" = "true" ]; then - echo "A newer review run already owns PR #${PR_NUMBER} at ${HEAD_SHA}; suppressing duplicate retry" - else - echo "A newer matching review for PR #${PR_NUMBER} at ${BASE_SHA}:${HEAD_SHA} suppresses duplicate retry" - fi - exit 0 - done <<< "$NEWER_RUNS" - - AW_CONTEXT=$(jq -cn \ - --arg base_sha "$BASE_SHA" \ - --arg head_sha "$HEAD_SHA" \ - --argjson item_number "$PR_NUMBER" \ - '{item_type: "pull_request", item_number: $item_number, base_sha: $base_sha, head_sha: $head_sha}') - dispatch_args=( - --method POST - "repos/${REPOSITORY}/actions/workflows/sfl-pr-review.lock.yml/dispatches" - -f "ref=${DEFAULT_BRANCH}" - -f "inputs[item_number]=${PR_NUMBER}" - -f "inputs[base_sha]=${BASE_SHA}" - -f "inputs[head_sha]=${HEAD_SHA}" - -f "inputs[retry_count]=1" - -f "inputs[aw_context]=${AW_CONTEXT}" - ) - base_workflow="$(gh api \ - --method GET \ - "repos/${REPOSITORY}/contents/.github/workflows/sfl-pr-review.lock.yml" \ - -f "ref=${DEFAULT_BRANCH}" \ - -H "Accept: application/vnd.github.raw+json")" - if grep -qE '^\s+review_effort:\s*$' <<< "${base_workflow}"; then - dispatch_args+=(-f "inputs[review_effort]=${REVIEW_EFFORT}") - fi - if grep -qE '^\s+dispatch_id:\s*$' <<< "${base_workflow}"; then - dispatch_args+=(-f "inputs[dispatch_id]=recovery-${GITHUB_RUN_ID}") - fi - - # Last-moment interlock: the artifact/provenance checks above can be - # slow. Revalidate both immutable PR state and newer-run ownership - # immediately before dispatch so a stale retry cannot cancel a newer - # current-state review through the shared reviewer concurrency group. - FINAL_PULL_REQUEST=$(gh api "repos/${REPOSITORY}/pulls/${PR_NUMBER}") - if ! jq -e \ - --arg base_sha "$BASE_SHA" \ - --arg default_branch "$DEFAULT_BRANCH" \ - --arg head_sha "$HEAD_SHA" \ - --arg repository "$REPOSITORY" \ - ' - .state == "open" and - .draft == false and - .base.ref == $default_branch and - .base.sha == $base_sha and - .head.repo.full_name == $repository and - .head.sha == $head_sha - ' <<< "$FINAL_PULL_REQUEST" >/dev/null; then - echo "PR #${PR_NUMBER} changed before recovery dispatch; suppressing stale retry" - exit 0 - fi - FINAL_NEWER_RUN_ID=$(gh api \ - "repos/${REPOSITORY}/actions/workflows/sfl-pr-review.lock.yml/runs?event=workflow_dispatch&per_page=100" \ - | jq -r \ - --arg expected_title "$EXPECTED_REVIEW_TITLE_PREFIX" \ - --argjson failed_run_id "$FAILED_RUN_ID" ' - [.workflow_runs[] - | select(.id > $failed_run_id) - | select((.display_title // "") | startswith($expected_title))] - | sort_by(.id) - | last - | .id // empty - ') - if [[ "$FINAL_NEWER_RUN_ID" =~ ^[1-9][0-9]*$ ]]; then - echo "Newer review run ${FINAL_NEWER_RUN_ID} acquired PR #${PR_NUMBER} before recovery dispatch; suppressing duplicate retry" - exit 0 - fi - gh api "${dispatch_args[@]}" - echo "Dispatched one trusted missing-output retry for PR #${PR_NUMBER} at ${HEAD_SHA}" diff --git a/deployment/release-metadata.json b/deployment/release-metadata.json index 016cfd6d..5f9c5609 100644 --- a/deployment/release-metadata.json +++ b/deployment/release-metadata.json @@ -1,5 +1,5 @@ { - "schemaVersion": 1, + "schemaVersion": 2, "reviewedAt": "2026-08-18", "distribution": { "repository": "HemSoft/set-it-free-loop", @@ -8,98 +8,43 @@ "tag": "v2.1.0-rc.13", "prerelease": true }, - "reviewerBaseline": { - "repository": "relias-engineering/set-it-free-loop", - "releaseVersion": "6.5.7", - "releaseTag": "v6.5.7", - "releaseCommit": "8d5e30714fa6cc61a89189266f8eb463132abde9", - "reviewedCommit": "669d4d84ef37ebab107c5931d6727846626062e5", - "postReleaseChanges": [ - "TODO.md" + "reviewerRuntime": { + "type": "subscription-backed-codex-github-app", + "trigger": "@codex review", + "app": { + "id": 1144995, + "slug": "chatgpt-codex-connector", + "owner": "openai", + "botUserId": 199175422 + }, + "requiredGate": { + "name": "SFL Reviewer Gate Runner", + "appId": 15368, + "strictRequiredStatusChecksPolicy": true + }, + "evidence": [ + "https://github.com/HemSoft/hs-buddy/pull/427#issuecomment-5336227425", + "https://github.com/HemSoft/hs-buddy/pull/427#issuecomment-5336243021", + "https://github.com/HemSoft/hs-buddy/pull/415#pullrequestreview-4947117457" ] }, "cliSource": { "repository": "HemSoft/set-it-free-loop", "path": "gh-sfl", "module": "github.com/HemSoft/set-it-free-loop/gh-sfl", - "upstreamRepository": "relias-engineering/set-it-free-loop", - "upstreamRelease": "v6.5.7", - "upstreamCommit": "8d5e30714fa6cc61a89189266f8eb463132abde9", "buildScript": "deployment/scripts/install-gh-sfl-hemsoft.ps1" }, - "allowedDifferenceClasses": [ - "provider-configuration", - "private-distribution-boundary", - "tested-hemsoft-hardening" - ], "artifacts": [ { - "name": "reviewer-source", - "upstreamPath": "workflows/sfl-pr-review.md", - "upstreamSha256": "35b1d066abd0df5498a9ece6937be0c92c8dbfc04f3adf1c43b22dc7d2dc3a5a", - "hemsoftPath": "deployment/workflows/sfl-pr-review.md", - "hemsoftSha256": "4c096f82ab8a3f60848fae9bc5cdcd0b26b9a67f915ad389dc1a2fd354fbd499", - "stagedPaths": [ - ".github/workflows/sfl-pr-review.md" - ], - "differenceClasses": [ - "provider-configuration", - "private-distribution-boundary", - "tested-hemsoft-hardening" - ], - "evidenceTests": [ - "deployment/tests/test-sfl-pr-review.ps1", - "deployment/tests/test-sfl-verdict-validator.ps1" - ] - }, - { - "name": "reviewer-lock", - "upstreamPath": "workflows/sfl-pr-review.lock.yml", - "upstreamSha256": "b232c552c59b17edef1bfe3326a9b0eb6d29d379be44a33e861f33a6ae47af02", - "hemsoftPath": ".github/workflows/sfl-pr-review.lock.yml", - "hemsoftSha256": "9940c4476977b18f185d2dc861dc240f2299b42175603ad6cc96dca40efcaa84", - "stagedPaths": [], - "differenceClasses": [ - "provider-configuration", - "private-distribution-boundary", - "tested-hemsoft-hardening" - ], - "evidenceTests": [ - "deployment/tests/test-sfl-pr-review.ps1" - ] - }, - { - "name": "auto-trigger-gate", - "upstreamPath": "workflows/sfl-pr-review-auto.yml", - "upstreamSha256": "abbb6615ec2c598e42ef242596c0e6f05e3b616641da7c656d74b7bbb5487bf6", + "name": "codex-review-observer", "hemsoftPath": "deployment/infrastructure/sfl-pr-review-auto.yml", - "hemsoftSha256": "faa06d4e3c843e219cdadd6a227216aa14bcf37c38a34bb8fe7ac54db6095c92", "stagedPaths": [ ".github/workflows/sfl-pr-review-auto.yml" ], - "differenceClasses": [ - "private-distribution-boundary", - "tested-hemsoft-hardening" - ], - "evidenceTests": [ - "deployment/tests/test-sfl-dispatch-dedup.ps1", - "deployment/tests/test-sfl-review-platform.ps1" - ] - }, - { - "name": "recovery", - "upstreamPath": "workflows/sfl-pr-review-recovery.yml", - "upstreamSha256": "d94fb4a788a0f4c39ed45a24d14e44ec6f0a8bcd2027c3357edae2167ae33a25", - "hemsoftPath": "deployment/infrastructure/sfl-pr-review-recovery.yml", - "hemsoftSha256": "2fdc1de7498121afcf38f63d5cf4c2c697009577c5890e4ebbd954414c9714c4", - "stagedPaths": [ - ".github/workflows/sfl-pr-review-recovery.yml" - ], - "differenceClasses": [ - "tested-hemsoft-hardening" - ], "evidenceTests": [ - "deployment/tests/test-sfl-review-platform.ps1" + "deployment/tests/test-sfl-pr-review.ps1", + "deployment/tests/test-sfl-review-platform.ps1", + "gh-sfl/reviewer_contract_test.go" ] } ] diff --git a/deployment/scripts/add-sfl-source-pin.ps1 b/deployment/scripts/add-sfl-source-pin.ps1 index 8b3262c6..6ac33ff4 100644 --- a/deployment/scripts/add-sfl-source-pin.ps1 +++ b/deployment/scripts/add-sfl-source-pin.ps1 @@ -30,7 +30,10 @@ function Add-SflYamlSourcePin { [string] $Content, [Parameter(Mandatory)] - [string] $SourceRef + [string] $SourceRef, + + [Parameter(Mandatory)] + [string] $DefaultBranch ) $withoutExistingPin = [regex]::Replace( @@ -38,5 +41,25 @@ function Add-SflYamlSourcePin { '\A# Deployed from: HemSoft/set-it-free-loop/[^\r\n]+\r?\n# To upgrade: re-run deploy-workflow\.ps1 at the desired SHA\r?\n', '' ) + $withoutExistingPin = $withoutExistingPin.Replace("`r`n", "`n") + $sourcePattern = '(?m)^# Source: HemSoft/set-it-free-loop/deployment/infrastructure/sfl-pr-review-auto\.yml@(?:main|[0-9a-f]{40})$' + $withoutExistingPin = ([regex]::new($sourcePattern)).Replace( + $withoutExistingPin, + "# Source: $SourceRef", + 1 + ) + $escapedDefaultBranch = $DefaultBranch.Replace("'", "''") + $pushBranchPattern = "(?m)^ branches: \[(?:main|'(?:[^']|'')*')\]$" + $withoutExistingPin = ([regex]::new($pushBranchPattern)).Replace( + $withoutExistingPin, + " branches: ['$escapedDefaultBranch']", + 1 + ) + $reviewBasePattern = "(?m)^ SFL_REVIEW_BASE_BRANCH: (?:main|'(?:[^']|'')*')$" + $withoutExistingPin = ([regex]::new($reviewBasePattern)).Replace( + $withoutExistingPin, + " SFL_REVIEW_BASE_BRANCH: '$escapedDefaultBranch'", + 1 + ) return "# Deployed from: $SourceRef`n# To upgrade: re-run deploy-workflow.ps1 at the desired SHA`n$withoutExistingPin" } diff --git a/deployment/scripts/build-release-artifacts.ps1 b/deployment/scripts/build-release-artifacts.ps1 index 1407f86c..8b285028 100644 --- a/deployment/scripts/build-release-artifacts.ps1 +++ b/deployment/scripts/build-release-artifacts.ps1 @@ -57,7 +57,7 @@ try { if ($unformatted.Count -gt 0) { throw "gh-sfl requires gofmt: $($unformatted -join ', ')" } go vet ./... if ($LASTEXITCODE -ne 0) { throw "go vet failed with exit code $LASTEXITCODE" } - go test ./... + go test -count=1 ./... if ($LASTEXITCODE -ne 0) { throw "go test failed with exit code $LASTEXITCODE" } $oldGoOS = $env:GOOS diff --git a/deployment/scripts/deploy-workflow.ps1 b/deployment/scripts/deploy-workflow.ps1 index 339f0521..e1268799 100644 --- a/deployment/scripts/deploy-workflow.ps1 +++ b/deployment/scripts/deploy-workflow.ps1 @@ -37,7 +37,7 @@ Cannot be used with -Workflow. Tiers: - review — Labels, governance, automatic/recoverable SFL full-spectrum PR review + review — Subscription-backed Codex review observer and immutable-head gate minimal — Labels, governance, repo-audit, daily-repo-status standard — Minimal + SFL Auditor, SFL Dispatcher, issue-processor, simplisticate full — Standard + standalone review, focused PR Analyzers, PR Fixer, PR Promoter @@ -139,17 +139,6 @@ if (-not (Test-Path $EnginePolicyPath)) { } $EnginePolicy = Get-Content $EnginePolicyPath -Raw | ConvertFrom-Json -$LabelsPath = Join-Path $RepoRoot "deployment\governance\labels.json" -$SflReviewLabel = @( - Get-Content $LabelsPath -Raw | - ConvertFrom-Json | - Where-Object name -eq "sfl-review" -) | Select-Object -First 1 -if ($null -eq $SflReviewLabel) { - Write-Error "Label definition 'sfl-review' was not found in $LabelsPath." - exit 1 -} - function Get-SflObjectProperty([object]$Object, [string]$Name, [string]$Context) { if ($null -eq $Object) { throw "$Context is null." @@ -316,10 +305,9 @@ function New-SflEnginePolicyManifest([string[]]$WorkflowNames) { $TierComponents = @{ "review" = @{ - Workflows = @("sfl-pr-review") - Infrastructure = @("sfl-pr-review-auto", "sfl-pr-review-recovery") - Components = @("labels", "governance", "sfl-pr-review", - "sfl-pr-review-auto", "sfl-pr-review-recovery") + Workflows = @() + Infrastructure = @("sfl-pr-review-auto") + Components = @("sfl-pr-review-auto") } "minimal" = @{ Workflows = @("daily-repo-status", "repo-audit") @@ -334,14 +322,13 @@ $TierComponents = @{ } "full" = @{ Workflows = @("daily-repo-status", "repo-audit", "issue-processor", "simplisticate", - "sfl-pr-review", "pr-analyzer-general", "pr-analyzer-quality", "pr-analyzer-security", "pr-analyzer-testing", "pr-fixer", "pr-promoter") Infrastructure = @("sfl-dispatcher", "sfl-auditor", - "sfl-pr-review-auto", "sfl-pr-review-recovery") + "sfl-pr-review-auto") Components = @("labels", "governance", "sfl-dispatcher", "sfl-auditor", "daily-repo-status", "repo-audit", "issue-processor", "simplisticate", - "sfl-pr-review", "sfl-pr-review-auto", "sfl-pr-review-recovery", + "sfl-pr-review-auto", "pr-analyzer-general", "pr-analyzer-quality", "pr-analyzer-security", "pr-analyzer-testing", "pr-fixer", "pr-promoter") } @@ -356,11 +343,7 @@ if ($Local -and -not $Workflow -and -not $Tier) { Sort-Object BaseName | ForEach-Object { $_.BaseName } ) - $InfrastructureToDeploy = if ("sfl-pr-review" -in $WorkflowsToDeploy) { - @("sfl-pr-review-auto", "sfl-pr-review-recovery") - } else { - @() - } + $InfrastructureToDeploy = @("sfl-pr-review-auto") $DeployTier = "local" $DeployComponents = @($WorkflowsToDeploy) + @($InfrastructureToDeploy) } elseif ($Workflow) { @@ -370,11 +353,7 @@ if ($Local -and -not $Workflow -and -not $Tier) { exit 1 } $WorkflowsToDeploy = @($Workflow) - $InfrastructureToDeploy = if ($Workflow -eq "sfl-pr-review") { - @("sfl-pr-review-auto", "sfl-pr-review-recovery") - } else { - @() - } + $InfrastructureToDeploy = @() $DeployTier = "custom" $DeployComponents = @($Workflow) + @($InfrastructureToDeploy) } else { @@ -434,56 +413,16 @@ function Assert-HemSoftRepository([string] $TargetRepo) { } } -function Ensure-SflReviewLabel([string]$TargetRepo) { - if ("sfl-pr-review" -notin $DeployComponents) { - return - } - - Write-Status "🏷️ " "Ensuring trigger label sfl-review on $TargetRepo" - if ($DryRun) { - return - } - - $labelExistsOutput = & gh label list --repo $TargetRepo --limit 1000 --json name ` - --jq ".[] | select(.name == `"$($SflReviewLabel.name)`") | .name" 2>&1 - if ($LASTEXITCODE -ne 0) { - throw "Failed to list labels on ${TargetRepo}: $labelExistsOutput" - } - - $labelExists = @($labelExistsOutput) -contains $SflReviewLabel.name - $labelArgs = @( - 'label', 'create', $SflReviewLabel.name, - '--repo', $TargetRepo, - '--color', $SflReviewLabel.color, - '--description', $SflReviewLabel.description - ) - if ($labelExists) { - $labelArgs += '--force' - } - - $labelOutput = & gh @labelArgs 2>&1 - if ($LASTEXITCODE -ne 0) { - throw "Failed to create or update sfl-review on ${TargetRepo}: $labelOutput" - } -} - function Assert-SflReviewCredentials([string]$TargetRepo) { - if ("sfl-pr-review" -notin $DeployComponents) { + if ($WorkflowsToDeploy.Count -eq 0) { return } - Write-Status "🔐" "Verifying SFL App credentials on $TargetRepo" + Write-Status "🔐" "Verifying workflow engine credentials on $TargetRepo" if ($DryRun) { return } - $variableNames = @( - & gh variable list --repo $TargetRepo --json name --jq '.[].name' 2>&1 - ) - if ($LASTEXITCODE -ne 0) { - throw "Failed to list Actions variables on ${TargetRepo}: $($variableNames -join [Environment]::NewLine)" - } - $secretNames = @( & gh secret list --repo $TargetRepo --app actions --json name --jq '.[].name' 2>&1 ) @@ -491,16 +430,6 @@ function Assert-SflReviewCredentials([string]$TargetRepo) { throw "Failed to list Actions secrets on ${TargetRepo}: $($secretNames -join [Environment]::NewLine)" } - $missing = @( - @("SFL_APP_ID", "SFL_APP_CLIENT_ID") | - Where-Object { $_ -notin $variableNames } - @("SFL_APP_PRIVATE_KEY") | - Where-Object { $_ -notin $secretNames } - ) - if ($missing.Count -gt 0) { - throw "Missing SFL App credential metadata on ${TargetRepo}: $($missing -join ', ')" - } - foreach ($workflowName in $WorkflowsToDeploy) { $profile = Resolve-SflEngineProfile $workflowName $requiredSecrets = @($profile.RequiredSecretsAnyOf) @@ -555,7 +484,6 @@ function Deploy-ToRepo([string]$TargetRepo) { Assert-SflReviewCredentials $TargetRepo if ($DryRun) { - Write-Status "🔍" "[DRY RUN] Would ensure trigger label sfl-review on $TargetRepo" Yellow Write-Status "🔍" "[DRY RUN] Would clone $TargetRepo to $ClonePath" Yellow Write-Status "🔍" "[DRY RUN] Would create branch: $BranchName" Yellow foreach ($wf in $WorkflowsToDeploy) { @@ -577,8 +505,6 @@ function Deploy-ToRepo([string]$TargetRepo) { return } - Ensure-SflReviewLabel $TargetRepo - # Clean up any previous clone attempt if (Test-Path $ClonePath) { Remove-Item $ClonePath -Recurse -Force } @@ -609,6 +535,20 @@ function Deploy-ToRepo([string]$TargetRepo) { # 3. Copy workflow files New-Item -ItemType Directory -Force $DestWorkdir | Out-Null + if ("sfl-pr-review-auto" -in $DeployComponents) { + foreach ($retiredReviewerFile in @( + "sfl-pr-review.md", + "sfl-pr-review.lock.yml", + "sfl-pr-review-recovery.yml" + )) { + $retiredPath = Join-Path $DestWorkdir $retiredReviewerFile + if (Test-Path -LiteralPath $retiredPath -PathType Leaf) { + Remove-Item -LiteralPath $retiredPath -Force + Write-Status "🧹" " removed retired $retiredReviewerFile" + } + } + } + foreach ($wf in $WorkflowsToDeploy) { $SourceFile = Join-Path $RepoRoot "deployment\workflows\$wf.md" $DestFile = Join-Path $DestWorkdir "$wf.md" @@ -645,11 +585,12 @@ function Deploy-ToRepo([string]$TargetRepo) { $SourceFile = Join-Path $RepoRoot "deployment\infrastructure\$inf.yml" $DestFile = Join-Path $DestWorkdir "$inf.yml" Copy-Item $SourceFile $DestFile - if ($inf -in @("sfl-pr-review-auto", "sfl-pr-review-recovery")) { + if ($inf -eq "sfl-pr-review-auto") { $sourceRef = "HemSoft/set-it-free-loop/deployment/infrastructure/$inf.yml@$CurrentSha" $content = Add-SflYamlSourcePin ` -Content (Get-Content $DestFile -Raw) ` - -SourceRef $sourceRef + -SourceRef $sourceRef ` + -DefaultBranch $BaseBranch Set-Content $DestFile -Value $content -NoNewline } Write-Status "⚙️ " " $inf.yml" @@ -668,8 +609,14 @@ function Deploy-ToRepo([string]$TargetRepo) { } $manifestPath = Join-Path $ClonePath "sfl.json" - $existingManifest = if (Test-Path -LiteralPath $manifestPath -PathType Leaf) { - Get-Content -LiteralPath $manifestPath -Raw | ConvertFrom-Json + $canonicalManifestPath = Join-Path $ClonePath ".sfl/sfl.json" + $existingManifestPath = if (Test-Path -LiteralPath $canonicalManifestPath -PathType Leaf) { + $canonicalManifestPath + } else { + $manifestPath + } + $existingManifest = if (Test-Path -LiteralPath $existingManifestPath -PathType Leaf) { + Get-Content -LiteralPath $existingManifestPath -Raw | ConvertFrom-Json } else { $null } @@ -678,6 +625,9 @@ function Deploy-ToRepo([string]$TargetRepo) { -IncomingManifest $incomingManifest $manifest = $manifestObject | ConvertTo-Json -Depth 6 Set-Content $manifestPath $manifest + if (Test-Path -LiteralPath $canonicalManifestPath -PathType Leaf) { + Set-Content $canonicalManifestPath $manifest + } Write-Status "📋" " sfl.json (v$SflVersion, tier: $($manifestObject.tier))" # 7. Inject/update SFL badge in README.md @@ -752,17 +702,12 @@ See https://github.com/HemSoft/set-it-free-loop for full documentation." --quiet } $componentList = ($DeployComponents | ForEach-Object { "- ``$_``" }) -join "`n" - - if ($existingPrUrl) { - $prUrl = $existingPrUrl - Write-Status "✅" "Existing PR updated: $prUrl" Green - } else { - $prUrl = gh pr create ` - --repo $TargetRepo ` - --head $BranchName ` - --base $BaseBranch ` - --title $prTitle ` - --body "## Set it Free Loop — Deployment + $compileChecklist = if ($WorkflowsToDeploy.Count -gt 0) { + "- [ ] For each ``.md`` workflow: verify ``gh aw compile .github/workflows/.md`` succeeds" + } else { + "- [ ] Verify the standard Actions observer with ``actionlint .github/workflows/sfl-pr-review-auto.yml``" + } + $prBody = "## Set it Free Loop — Deployment **Version**: $SflVersion **Tier**: $DeployTier @@ -783,10 +728,29 @@ for all available workflows. ### Before merging - [ ] Run ``.\deployment\governance\setup-labels.ps1 -Owner -Repo `` if labels are not yet configured -- [ ] For each ``.md`` workflow: verify ``gh aw compile .github/workflows/.md`` succeeds +$compileChecklist - [ ] Trigger a workflow manually to confirm output - [ ] Review ``sfl.json`` manifest in the repo root -" 2>&1 +" + + if ($existingPrUrl) { + $existingPrNumber = [int] $existingPr.number + gh pr edit $existingPrNumber ` + --repo $TargetRepo ` + --title $prTitle ` + --body $prBody | Out-Null + if ($LASTEXITCODE -ne 0) { + throw "gh pr edit failed for $existingPrUrl" + } + $prUrl = $existingPrUrl + Write-Status "✅" "Existing PR updated: $prUrl" Green + } else { + $prUrl = gh pr create ` + --repo $TargetRepo ` + --head $BranchName ` + --base $BaseBranch ` + --title $prTitle ` + --body $prBody 2>&1 if ($LASTEXITCODE -eq 0) { Write-Status "✅" "PR created: $prUrl" Green diff --git a/deployment/scripts/install-gh-sfl-hemsoft.ps1 b/deployment/scripts/install-gh-sfl-hemsoft.ps1 index bd05ecd9..483e90b0 100644 --- a/deployment/scripts/install-gh-sfl-hemsoft.ps1 +++ b/deployment/scripts/install-gh-sfl-hemsoft.ps1 @@ -126,7 +126,7 @@ else { if ($unformatted.Count -gt 0) { throw "gh-sfl contains files that require gofmt: $($unformatted -join ', ')" } go vet ./... if ($LASTEXITCODE -ne 0) { throw "go vet failed with exit code $LASTEXITCODE" } - go test ./... + go test -count=1 ./... if ($LASTEXITCODE -ne 0) { throw "go test failed with exit code $LASTEXITCODE" } go build -trimpath -buildvcs=false -ldflags "-X main.version=$Version -X main.buildDate=$BuildDate" -o $outputPath . if ($LASTEXITCODE -ne 0) { throw "go build failed with exit code $LASTEXITCODE" } diff --git a/deployment/scripts/merge-sfl-manifest.ps1 b/deployment/scripts/merge-sfl-manifest.ps1 index acb7196b..e8a3f4c5 100644 --- a/deployment/scripts/merge-sfl-manifest.ps1 +++ b/deployment/scripts/merge-sfl-manifest.ps1 @@ -21,7 +21,8 @@ function Merge-SflManifest { $existingComponentsProperty = $ExistingManifest.PSObject.Properties['components'] $existingComponents = if ($null -ne $existingComponentsProperty) { - @($existingComponentsProperty.Value) + @($existingComponentsProperty.Value) | + Where-Object { $_ -notin @('sfl-pr-review', 'sfl-pr-review-recovery') } } else { @() } @@ -49,6 +50,9 @@ function Merge-SflManifest { $workflowMap = [ordered]@{} foreach ($workflow in $existingWorkflows) { + if ([string] $workflow.name -eq 'sfl-pr-review') { + continue + } $workflowMap[[string] $workflow.name] = $workflow } foreach ($workflow in @($IncomingManifest.enginePolicy.workflows)) { diff --git a/deployment/sfl-manifest.schema.json b/deployment/sfl-manifest.schema.json index 6612c2cb..fd534863 100644 --- a/deployment/sfl-manifest.schema.json +++ b/deployment/sfl-manifest.schema.json @@ -55,9 +55,7 @@ "repo-audit", "simplisticate", "issue-processor", - "sfl-pr-review", "sfl-pr-review-auto", - "sfl-pr-review-recovery", "pr-analyzer-general", "pr-analyzer-quality", "pr-analyzer-security", diff --git a/deployment/tests/test-release-metadata.ps1 b/deployment/tests/test-release-metadata.ps1 index cd4b77ad..94870c32 100644 --- a/deployment/tests/test-release-metadata.ps1 +++ b/deployment/tests/test-release-metadata.ps1 @@ -42,22 +42,20 @@ if ($release.distribution.repository -ne 'HemSoft/set-it-free-loop' -or $release.distribution.visibility -ne 'private') { $failures.Add('Release distribution identity is not the private HemSoft repository.') } -if ($release.reviewerBaseline.repository -ne 'relias-engineering/set-it-free-loop' -or - $release.reviewerBaseline.releaseVersion -ne '6.5.7' -or - $release.reviewerBaseline.releaseTag -ne "v$($release.reviewerBaseline.releaseVersion)") { - $failures.Add('Relias reviewer release identity is inconsistent.') -} -foreach ($property in @('releaseCommit', 'reviewedCommit')) { - if ([string] $release.reviewerBaseline.$property -notmatch '^[0-9a-f]{40}$') { - $failures.Add("reviewerBaseline.$property is not an immutable full commit SHA.") - } +if ($release.schemaVersion -ne 2 -or + $release.reviewerRuntime.type -ne 'subscription-backed-codex-github-app' -or + $release.reviewerRuntime.trigger -ne '@codex review' -or + $release.reviewerRuntime.app.id -ne 1144995 -or + $release.reviewerRuntime.app.slug -ne 'chatgpt-codex-connector' -or + $release.reviewerRuntime.app.owner -ne 'openai' -or + $release.reviewerRuntime.app.botUserId -ne 199175422 -or + $release.reviewerRuntime.requiredGate.name -ne 'SFL Reviewer Gate Runner' -or + $release.reviewerRuntime.requiredGate.appId -ne 15368) { + $failures.Add('Subscription-backed Codex reviewer identity is incomplete or inconsistent.') } if ($release.cliSource.repository -ne 'HemSoft/set-it-free-loop' -or $release.cliSource.path -ne 'gh-sfl' -or $release.cliSource.module -ne 'github.com/HemSoft/set-it-free-loop/gh-sfl' -or - $release.cliSource.upstreamRepository -ne 'relias-engineering/set-it-free-loop' -or - $release.cliSource.upstreamRelease -ne 'v6.5.7' -or - [string] $release.cliSource.upstreamCommit -notmatch '^[0-9a-f]{40}$' -or $release.cliSource.buildScript -ne 'deployment/scripts/install-gh-sfl-hemsoft.ps1') { $failures.Add('Repository-owned CLI source provenance is incomplete or inconsistent.') } @@ -208,4 +206,4 @@ if ($failures.Count -gt 0) { throw "Release metadata contract failed:`n - $($failures -join "`n - ")" } -Write-Output "Release metadata contract passed for HemSoft $version and Relias $($release.reviewerBaseline.releaseVersion)." +Write-Output "Release metadata contract passed for HemSoft $version and subscription-backed Codex review." diff --git a/deployment/tests/test-relias-reviewer-parity.ps1 b/deployment/tests/test-relias-reviewer-parity.ps1 deleted file mode 100644 index fcb9af6a..00000000 --- a/deployment/tests/test-relias-reviewer-parity.ps1 +++ /dev/null @@ -1,204 +0,0 @@ -[CmdletBinding()] -param( - [string] $ReliasCheckout -) - -Set-StrictMode -Version Latest -$ErrorActionPreference = 'Stop' - -$repoRoot = (Resolve-Path (Join-Path $PSScriptRoot '..\..')).ProviderPath -$baselinePath = Join-Path $repoRoot 'deployment\release-metadata.json' -$failures = [System.Collections.Generic.List[string]]::new() - -function Get-NormalizedSha256([string] $Text) { - $normalized = $Text.Replace("`r`n", "`n").Replace("`r", "`n") - $bytes = [Text.UTF8Encoding]::new($false).GetBytes($normalized) - $sha256 = [Security.Cryptography.SHA256]::Create() - try { - return -join ($sha256.ComputeHash($bytes) | ForEach-Object { $_.ToString('x2') }) - } - finally { - $sha256.Dispose() - } -} - -function Resolve-RepoFile([string] $RelativePath) { - if ([IO.Path]::IsPathRooted($RelativePath)) { - throw "Repository path must be relative: $RelativePath" - } - - $fullPath = [IO.Path]::GetFullPath((Join-Path $repoRoot $RelativePath)) - $rootPrefix = $repoRoot.TrimEnd([IO.Path]::DirectorySeparatorChar, [IO.Path]::AltDirectorySeparatorChar) + - [IO.Path]::DirectorySeparatorChar - if (-not $fullPath.StartsWith($rootPrefix, [StringComparison]::OrdinalIgnoreCase)) { - throw "Repository path escapes the checkout: $RelativePath" - } - return $fullPath -} - -function Invoke-GitText([string] $Checkout, [string[]] $Arguments) { - $startInfo = [Diagnostics.ProcessStartInfo]::new() - $startInfo.FileName = 'git' - $startInfo.UseShellExecute = $false - $startInfo.RedirectStandardOutput = $true - $startInfo.RedirectStandardError = $true - [void] $startInfo.ArgumentList.Add('-C') - [void] $startInfo.ArgumentList.Add($Checkout) - foreach ($argument in $Arguments) { - [void] $startInfo.ArgumentList.Add($argument) - } - - $process = [Diagnostics.Process]::Start($startInfo) - $stdout = $process.StandardOutput.ReadToEnd() - $stderr = $process.StandardError.ReadToEnd() - $process.WaitForExit() - if ($process.ExitCode -ne 0) { - throw "git $($Arguments -join ' ') failed: $($stderr.Trim())" - } - return $stdout -} - -if (-not (Test-Path -LiteralPath $baselinePath -PathType Leaf)) { - throw "Missing reviewer parity baseline: $baselinePath" -} - -$baseline = Get-Content -LiteralPath $baselinePath -Raw | ConvertFrom-Json -$expectedArtifactNames = @('auto-trigger-gate', 'recovery', 'reviewer-lock', 'reviewer-source') -$expectedClasses = @( - 'private-distribution-boundary', - 'provider-configuration', - 'tested-hemsoft-hardening' -) - -if ($baseline.schemaVersion -ne 1) { - $failures.Add("Unsupported schemaVersion '$($baseline.schemaVersion)'.") -} -if ($baseline.reviewerBaseline.repository -ne 'relias-engineering/set-it-free-loop') { - $failures.Add('The upstream repository is not relias-engineering/set-it-free-loop.') -} -foreach ($property in @('releaseCommit', 'reviewedCommit')) { - if ([string] $baseline.reviewerBaseline.$property -notmatch '^[0-9a-f]{40}$') { - $failures.Add("reviewerBaseline.$property is not a full lowercase Git commit SHA.") - } -} - -$actualClasses = @($baseline.allowedDifferenceClasses | Sort-Object) -if (Compare-Object $expectedClasses $actualClasses) { - $failures.Add('allowedDifferenceClasses does not match the closed parity classification set.') -} -$actualArtifactNames = @($baseline.artifacts.name | Sort-Object) -if (Compare-Object $expectedArtifactNames $actualArtifactNames) { - $failures.Add('The baseline must inventory exactly the four reviewer platform artifacts.') -} - -foreach ($artifact in @($baseline.artifacts)) { - try { - $hemSoftPath = Resolve-RepoFile ([string] $artifact.hemsoftPath) - if (-not (Test-Path -LiteralPath $hemSoftPath -PathType Leaf)) { - $failures.Add("$($artifact.name): missing HemSoft artifact $($artifact.hemsoftPath).") - continue - } - - $actualHash = Get-NormalizedSha256 ([IO.File]::ReadAllText($hemSoftPath)) - if ($artifact.hemsoftSha256 -notmatch '^[0-9a-f]{64}$' -or - $actualHash -ne $artifact.hemsoftSha256) { - $failures.Add("$($artifact.name): HemSoft normalized SHA-256 is $actualHash; baseline records $($artifact.hemsoftSha256).") - } - - if ($artifact.upstreamSha256 -notmatch '^[0-9a-f]{64}$') { - $failures.Add("$($artifact.name): upstreamSha256 is not a lowercase SHA-256 value.") - } - if (@($artifact.differenceClasses).Count -eq 0) { - $failures.Add("$($artifact.name): no intentional difference class is recorded.") - } - foreach ($differenceClass in @($artifact.differenceClasses)) { - if ($differenceClass -notin $expectedClasses) { - $failures.Add("$($artifact.name): unapproved difference class '$differenceClass'.") - } - } - - foreach ($stagedPath in @($artifact.stagedPaths)) { - $resolvedStagedPath = Resolve-RepoFile ([string] $stagedPath) - if (-not (Test-Path -LiteralPath $resolvedStagedPath -PathType Leaf)) { - $failures.Add("$($artifact.name): missing staged artifact $stagedPath.") - continue - } - $stagedHash = Get-NormalizedSha256 ([IO.File]::ReadAllText($resolvedStagedPath)) - if ($stagedHash -ne $actualHash) { - $failures.Add("$($artifact.name): staged artifact $stagedPath differs from $($artifact.hemsoftPath).") - } - } - - foreach ($evidenceTest in @($artifact.evidenceTests)) { - $resolvedEvidenceTest = Resolve-RepoFile ([string] $evidenceTest) - if (-not (Test-Path -LiteralPath $resolvedEvidenceTest -PathType Leaf)) { - $failures.Add("$($artifact.name): missing evidence test $evidenceTest.") - } - } - } - catch { - $failures.Add("$($artifact.name): $($_.Exception.Message)") - } -} - -if ($ReliasCheckout) { - try { - $resolvedReliasCheckout = (Resolve-Path -LiteralPath $ReliasCheckout).ProviderPath - $origin = (Invoke-GitText $resolvedReliasCheckout @('remote', 'get-url', 'origin')).Trim() - if ($origin -notmatch 'relias-engineering[/:]set-it-free-loop(?:\.git)?$') { - $failures.Add("Relias checkout origin is unexpected: $origin") - } - - $tagExpression = "$($baseline.reviewerBaseline.releaseTag)^{commit}" - $tagCommit = (Invoke-GitText $resolvedReliasCheckout @('rev-parse', $tagExpression)).Trim() - if ($tagCommit -ne $baseline.reviewerBaseline.releaseCommit) { - $failures.Add("$($baseline.reviewerBaseline.releaseTag) resolves to $tagCommit, not $($baseline.reviewerBaseline.releaseCommit).") - } - - [void] (Invoke-GitText $resolvedReliasCheckout @('cat-file', '-e', "$($baseline.reviewerBaseline.reviewedCommit)^{commit}")) - $changedPathsText = Invoke-GitText $resolvedReliasCheckout @( - 'diff', '--name-only', - $baseline.reviewerBaseline.releaseCommit, - $baseline.reviewerBaseline.reviewedCommit - ) - $changedPaths = @($changedPathsText -split '\r?\n' | Where-Object { $_ } | Sort-Object) - $expectedChangedPaths = @($baseline.reviewerBaseline.postReleaseChanges | Sort-Object) - if (Compare-Object $expectedChangedPaths $changedPaths) { - $failures.Add('Relias post-release changed paths differ from the recorded baseline.') - } - - foreach ($artifact in @($baseline.artifacts)) { - $reviewedText = Invoke-GitText $resolvedReliasCheckout @( - 'show', - "$($baseline.reviewerBaseline.reviewedCommit):$($artifact.upstreamPath)" - ) - $reviewedHash = Get-NormalizedSha256 $reviewedText - if ($reviewedHash -ne $artifact.upstreamSha256) { - $failures.Add("$($artifact.name): Relias reviewed-commit SHA-256 is $reviewedHash; baseline records $($artifact.upstreamSha256).") - } - - $releaseText = Invoke-GitText $resolvedReliasCheckout @( - 'show', - "$($baseline.reviewerBaseline.releaseCommit):$($artifact.upstreamPath)" - ) - $releaseHash = Get-NormalizedSha256 $releaseText - if ($releaseHash -ne $reviewedHash) { - $failures.Add("$($artifact.name): artifact changed between the release tag and reviewed commit.") - } - } - } - catch { - $failures.Add($_.Exception.Message) - } -} - -if ($failures.Count -gt 0) { - throw "Relias reviewer parity audit failed:`n - $($failures -join "`n - ")" -} - -if ($ReliasCheckout) { - Write-Output "Relias reviewer parity baseline and HemSoft artifacts passed at $($baseline.reviewerBaseline.reviewedCommit)." -} -else { - Write-Output 'HemSoft reviewer parity metadata and artifact hashes passed (Relias checkout not supplied).' -} diff --git a/deployment/tests/test-review-tier-manifest-merge.ps1 b/deployment/tests/test-review-tier-manifest-merge.ps1 index 485893a1..d8426392 100644 --- a/deployment/tests/test-review-tier-manifest-merge.ps1 +++ b/deployment/tests/test-review-tier-manifest-merge.ps1 @@ -13,7 +13,7 @@ $existingFull = [pscustomobject]@{ tier = 'full' source = 'HemSoft/set-it-free-loop' sourceSha = '1111111111111111111111111111111111111111' - components = @('labels', 'governance', 'repo-audit', 'pr-fixer') + components = @('labels', 'governance', 'repo-audit', 'pr-fixer', 'sfl-pr-review-recovery') enginePolicy = [pscustomobject]@{ defaultProfile = 'codex-gpt-55-high' workflows = @( @@ -25,6 +25,14 @@ $existingFull = [pscustomobject]@{ effort = 'high' renderedModel = 'gpt-5.5?effort=high' requiredSecretsAnyOf = @('OPENAI_API_KEY') + }, + [pscustomobject]@{ + name = 'sfl-pr-review' + profile = 'openrouter-kimi-k3-high' + provider = 'copilot' + model = 'moonshotai/kimi-k3' + renderedModel = 'moonshotai/kimi-k3' + requiredSecretsAnyOf = @('OPENROUTER_API_KEY') } ) } @@ -37,25 +45,11 @@ $incomingReview = [pscustomobject]@{ source = 'HemSoft/set-it-free-loop' sourceSha = '2222222222222222222222222222222222222222' components = @( - 'labels', - 'governance', - 'sfl-pr-review', - 'sfl-pr-review-auto', - 'sfl-pr-review-recovery' + 'sfl-pr-review-auto' ) enginePolicy = [pscustomobject]@{ defaultProfile = 'codex-gpt-55-high' - workflows = @( - [pscustomobject]@{ - name = 'sfl-pr-review' - profile = 'openrouter-kimi-k3-high' - provider = 'copilot' - model = 'moonshotai/kimi-k3' - effort = 'high' - renderedModel = 'moonshotai/kimi-k3' - requiredSecretsAnyOf = @('OPENROUTER_API_KEY') - } - ) + workflows = @() } } @@ -70,9 +64,7 @@ $expectedComponents = @( 'labels', 'pr-fixer', 'repo-audit', - 'sfl-pr-review', - 'sfl-pr-review-auto', - 'sfl-pr-review-recovery' + 'sfl-pr-review-auto' ) $actualComponents = @($merged.components | Sort-Object) if (($actualComponents -join ',') -ne ($expectedComponents -join ',')) { @@ -80,7 +72,7 @@ if (($actualComponents -join ',') -ne ($expectedComponents -join ',')) { } $workflowNames = @($merged.enginePolicy.workflows.name | Sort-Object) -if (($workflowNames -join ',') -ne 'repo-audit,sfl-pr-review') { +if (($workflowNames -join ',') -ne 'repo-audit') { throw "Unexpected merged engine workflows: $($workflowNames -join ', ')" } @@ -110,14 +102,27 @@ $legacyMerged = Merge-SflManifest ` if ($legacyMerged.tier -ne 'standard') { throw "Expected legacy standard tier to be preserved; got '$($legacyMerged.tier)'." } -foreach ($component in @('sfl-pr-review', 'sfl-pr-review-auto', 'sfl-pr-review-recovery')) { +foreach ($component in @('sfl-pr-review-auto')) { if ($component -notin @($legacyMerged.components)) { throw "Legacy manifest did not receive the $component component." } } -if (@($legacyMerged.enginePolicy.workflows).Count -ne 1 -or - $legacyMerged.enginePolicy.workflows[0].name -ne 'sfl-pr-review') { - throw 'Legacy manifest did not receive the review engine policy.' +if (@($legacyMerged.enginePolicy.workflows).Count -ne 0) { + throw 'Subscription-backed reviewer unexpectedly added an API engine policy.' +} +if ('sfl-pr-review' -in @($legacyMerged.components)) { + throw 'Legacy manifest retained the retired Markdown reviewer component.' +} + +$deployer = Get-Content -LiteralPath (Join-Path $repoRoot 'deployment\scripts\deploy-workflow.ps1') -Raw +foreach ($required in @( + '$canonicalManifestPath = Join-Path $ClonePath ".sfl/sfl.json"', + '$existingManifestPath = if (Test-Path -LiteralPath $canonicalManifestPath', + 'Set-Content $canonicalManifestPath $manifest' +)) { + if ($deployer -notmatch [regex]::Escape($required)) { + throw "Review-tier deployment does not synchronize the canonical manifest: $required" + } } Write-Output 'Review-tier manifest merge tests passed.' diff --git a/deployment/tests/test-sfl-auditor.ps1 b/deployment/tests/test-sfl-auditor.ps1 index 74d559d0..4f3d47de 100644 --- a/deployment/tests/test-sfl-auditor.ps1 +++ b/deployment/tests/test-sfl-auditor.ps1 @@ -81,6 +81,24 @@ function Invoke-BashScript { } } +$workflowContent = Get-Content -Raw -LiteralPath $workflowPath +Assert-True ($workflowContent -match 'index\("sfl-pr-review-auto"\)') 'Auditor does not activate for the current reviewer component.' +Assert-True ($workflowContent -notmatch 'index\("sfl-pr-review"\)') 'Auditor still activates on the retired reviewer component.' +Assert-True ($workflowContent -match 'any\(\. == "pr-review"\)') 'Auditor does not activate for the string-valued reviewer add-on.' +Assert-True ($workflowContent -notmatch 'any\(\.name == "pr-review"\)') 'Auditor still treats string-valued add-ons as objects.' +Assert-True ($workflowContent -match 'if ! MANIFEST=.*contents/\.sfl/sfl\.json' -and + $workflowContent -match 'MANIFEST=.*contents/sfl\.json') 'Auditor does not use root sfl.json as a canonical-manifest fallback.' +Assert-True ($workflowContent -match 'OBSERVER_STATE.*\.state') 'Auditor does not inspect the observer workflow state.' +Assert-True ($workflowContent -match 'OBSERVER_STATE" != "active"') 'Auditor does not reject disabled observer workflows.' +Assert-True ($workflowContent -match "DEFAULT_BRANCH=.*\.default_branch") 'Auditor does not read the repository default branch.' +Assert-True ($workflowContent -match 'PUSH_FILTER_VALID') 'Auditor does not validate the observer push branch.' +Assert-True ($workflowContent -match 'BASE_ENV_VALID') 'Auditor does not validate the observer base-branch environment.' +Assert-True ($workflowContent -match 'EXPECTED_SOURCE_SHA') 'Auditor does not read the canonical manifest source SHA.' +Assert-True ($workflowContent -match 'OBSERVER_SOURCE_SHA') 'Auditor does not read the observer source pin.' +Assert-True ($workflowContent -match 'OBSERVER_SOURCE_SHA" != "\$EXPECTED_SOURCE_SHA') 'Auditor does not reject an observer source-pin mismatch.' +Assert-True ($workflowContent -match 'declares standalone SFL review in') 'Auditor issue text does not cover both supported reviewer manifest forms.' +Assert-True ($workflowContent -notmatch 'declares \\`sfl-pr-review-auto\\` in') 'Auditor issue text still claims every reviewer manifest uses the component form.' + $mockGh = @' gh() { if [ "$1 $2" = "pr list" ]; then @@ -160,6 +178,154 @@ try { Assert-True ($unrelatedCommentOutput -match 'stalled_prs_found=1') 'Unrelated review text suppressed the Auditor warning.' Assert-True ($unrelatedCommentWarning -match '') 'Auditor warning was not posted after unrelated review text.' + $reviewScript = Get-WorkflowStepScript " - name: `"Check: SFL review prerequisites`"`n" + $manifestPath = Join-Path $temporaryDirectory 'sfl.json' + $rootManifestPath = Join-Path $temporaryDirectory 'root-sfl.json' + $manifestLogPath = Join-Path $temporaryDirectory 'manifest-log' + $observerPath = Join-Path $temporaryDirectory 'observer.yml' + $reviewOutputPath = Join-Path $temporaryDirectory 'review-output' + $issueLogPath = Join-Path $temporaryDirectory 'issue-log' + $sourceSha = 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa' + "{`"components`": [`"sfl-pr-review-auto`"], `"sourceSha`": `"$sourceSha`"}" | Set-Content -LiteralPath $manifestPath + "{`"components`": [`"sfl-pr-review-auto`"], `"sourceSha`": `"$sourceSha`"}" | Set-Content -LiteralPath $rootManifestPath + $reviewMock = @' +gh() { + if [ "$1" = "api" ]; then + case "$2" in + repos/HemSoft/example/contents/.sfl/sfl.json) + printf 'canonical\n' >> "$MANIFEST_LOG" + base64 "$MANIFEST_FIXTURE" + return + ;; + repos/HemSoft/example/contents/sfl.json) + printf 'root\n' >> "$MANIFEST_LOG" + base64 "$ROOT_MANIFEST_FIXTURE" + return + ;; + repos/HemSoft/example/actions/workflows/sfl-pr-review-auto.yml) + printf 'active\n' + return + ;; + repos/HemSoft/example/contents/.github/workflows/sfl-pr-review-auto.yml) + base64 "$OBSERVER_FIXTURE" + return + ;; + repos/HemSoft/example) + printf '%s\n' "$DEFAULT_BRANCH_FIXTURE" + return + ;; + esac + return 1 + fi + if [ "$1 $2" = "issue list" ]; then + return + fi + if [ "$1 $2" = "issue create" ]; then + printf 'created\n' >> "$ISSUE_LOG" + return + fi + if [ "$1 $2" = "issue close" ]; then + printf 'closed\n' >> "$ISSUE_LOG" + return + fi + return 1 +} +'@ + $reviewEnvironment = @{ + GITHUB_OUTPUT = $reviewOutputPath + DEFAULT_BRANCH_FIXTURE = 'main' + ISSUE_LOG = $issueLogPath + MANIFEST_FIXTURE = $manifestPath + MANIFEST_LOG = $manifestLogPath + OBSERVER_FIXTURE = $observerPath + REPO = 'HemSoft/example' + ROOT_MANIFEST_FIXTURE = $rootManifestPath + } + @' +# Source: HemSoft/set-it-free-loop/deployment/infrastructure/sfl-pr-review-auto.yml@aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +name: SFL Codex Review Observer +on: + push: + branches: ['trunk'] +env: + SFL_REVIEW_BASE_BRANCH: 'trunk' +github.event.sender.id == 199175422 +name: "SFL Reviewer Gate Runner" +'@ | Set-Content -LiteralPath $observerPath + $staleBaseRun = Invoke-BashScript -Script "$reviewMock`n$reviewScript" -Environment $reviewEnvironment + $staleBaseOutput = Get-Content -Raw -LiteralPath $reviewOutputPath + $staleBaseIssue = Get-Content -Raw -LiteralPath $issueLogPath + Assert-True ($staleBaseRun.ExitCode -eq 0) "Stale-base prerequisite script failed: $($staleBaseRun.Output)" + Assert-True ($staleBaseOutput -match 'sfl_review_prerequisites_missing=1') 'Auditor accepted a stale observer base branch.' + Assert-True ($staleBaseIssue -match 'created') 'Auditor did not create an issue for a stale observer base branch.' + + Remove-Item -LiteralPath $reviewOutputPath, $issueLogPath -Force + @' +# Source: HemSoft/set-it-free-loop/deployment/infrastructure/sfl-pr-review-auto.yml@aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +name: SFL Codex Review Observer +on: + push: + branches: ['main'] +env: + SFL_REVIEW_BASE_BRANCH: 'main' +github.event.sender.id == 199175422 +name: "SFL Reviewer Gate Runner" +'@ | Set-Content -LiteralPath $observerPath + $currentBaseRun = Invoke-BashScript -Script "$reviewMock`n$reviewScript" -Environment $reviewEnvironment + $currentBaseOutput = Get-Content -Raw -LiteralPath $reviewOutputPath + $currentBaseIssue = if (Test-Path -LiteralPath $issueLogPath) { Get-Content -Raw -LiteralPath $issueLogPath } else { '' } + Assert-True ($currentBaseRun.ExitCode -eq 0) "Current-base prerequisite script failed: $($currentBaseRun.Output)" + Assert-True ($currentBaseOutput -match 'sfl_review_prerequisites_missing=0') 'Auditor rejected an observer deployed for the current default branch.' + Assert-True ([string]::IsNullOrEmpty($currentBaseIssue)) 'Auditor opened or closed an issue for a valid observer base branch.' + + Remove-Item -LiteralPath $reviewOutputPath, $issueLogPath -Force -ErrorAction SilentlyContinue + (Get-Content -Raw -LiteralPath $observerPath).Replace($sourceSha, 'bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb') | + Set-Content -LiteralPath $observerPath + $stalePinRun = Invoke-BashScript -Script "$reviewMock`n$reviewScript" -Environment $reviewEnvironment + $stalePinOutput = Get-Content -Raw -LiteralPath $reviewOutputPath + $stalePinIssue = Get-Content -Raw -LiteralPath $issueLogPath + Assert-True ($stalePinRun.ExitCode -eq 0) "Stale-pin prerequisite script failed: $($stalePinRun.Output)" + Assert-True ($stalePinOutput -match 'sfl_review_prerequisites_missing=1') 'Auditor accepted an observer with a stale source pin.' + Assert-True ($stalePinIssue -match 'created') 'Auditor did not create an issue for a stale observer source pin.' + + Remove-Item -LiteralPath $reviewOutputPath, $issueLogPath -Force -ErrorAction SilentlyContinue + Set-Content -LiteralPath $observerPath -Value '' + $missingObserverRun = Invoke-BashScript -Script "$reviewMock`n$reviewScript" -Environment $reviewEnvironment + $missingObserverOutput = Get-Content -Raw -LiteralPath $reviewOutputPath + $missingObserverIssue = Get-Content -Raw -LiteralPath $issueLogPath + Assert-True ($missingObserverRun.ExitCode -eq 0) "Missing-observer prerequisite script failed: $($missingObserverRun.Output)" + Assert-True ($missingObserverOutput -match 'sfl_review_prerequisites_missing=1') 'Auditor did not report a missing observer file.' + Assert-True ($missingObserverIssue -match 'created') 'Auditor did not create an issue for a missing observer file.' + + Remove-Item -LiteralPath $reviewOutputPath, $issueLogPath -Force -ErrorAction SilentlyContinue + $reviewEnvironment.DEFAULT_BRANCH_FIXTURE = "release/o'brien" + @' +# Source: HemSoft/set-it-free-loop/deployment/infrastructure/sfl-pr-review-auto.yml@aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +name: SFL Codex Review Observer +on: + push: + branches: ['release/o''brien'] +env: + SFL_REVIEW_BASE_BRANCH: 'release/o''brien' +github.event.sender.id == 199175422 +name: "SFL Reviewer Gate Runner" +'@ | Set-Content -LiteralPath $observerPath + $quotedBaseRun = Invoke-BashScript -Script "$reviewMock`n$reviewScript" -Environment $reviewEnvironment + $quotedBaseOutput = Get-Content -Raw -LiteralPath $reviewOutputPath + $quotedBaseIssue = if (Test-Path -LiteralPath $issueLogPath) { Get-Content -Raw -LiteralPath $issueLogPath } else { '' } + Assert-True ($quotedBaseRun.ExitCode -eq 0) "Quoted-base prerequisite script failed: $($quotedBaseRun.Output)" + Assert-True ($quotedBaseOutput -match 'sfl_review_prerequisites_missing=0') 'Auditor rejected a correctly YAML-escaped default branch.' + Assert-True ([string]::IsNullOrEmpty($quotedBaseIssue)) 'Auditor opened or closed an issue for a correctly YAML-escaped default branch.' + + Remove-Item -LiteralPath $reviewOutputPath, $manifestLogPath -Force -ErrorAction SilentlyContinue + '{"components":[]}' | Set-Content -LiteralPath $manifestPath + $canonicalManifestRun = Invoke-BashScript -Script "$reviewMock`n$reviewScript" -Environment $reviewEnvironment + $canonicalManifestOutput = Get-Content -Raw -LiteralPath $reviewOutputPath + $manifestLookups = Get-Content -Raw -LiteralPath $manifestLogPath + Assert-True ($canonicalManifestRun.ExitCode -eq 0) "Canonical-manifest precedence script failed: $($canonicalManifestRun.Output)" + Assert-True ($canonicalManifestOutput -match 'sfl_review_prerequisites_missing=0') 'Auditor used a stale root reviewer declaration after the canonical manifest removed it.' + Assert-True ($manifestLookups -eq "canonical`n") 'Auditor read root sfl.json even though .sfl/sfl.json exists.' + $summaryScript = Get-WorkflowStepScript " - name: Summary`n" $summaryExpressions = @( '${{ steps.orphaned-labels.outputs.orphaned_labels_fixed }}', diff --git a/deployment/tests/test-sfl-dispatch-dedup.ps1 b/deployment/tests/test-sfl-dispatch-dedup.ps1 deleted file mode 100644 index 4de2120d..00000000 --- a/deployment/tests/test-sfl-dispatch-dedup.ps1 +++ /dev/null @@ -1,106 +0,0 @@ -[CmdletBinding()] -param() - -Set-StrictMode -Version Latest -$ErrorActionPreference = 'Stop' - -$repoRoot = (Resolve-Path (Join-Path $PSScriptRoot '..\..')).ProviderPath -. (Join-Path $PSScriptRoot 'line-ending-test-helpers.ps1') -$canonicalPath = Join-Path $repoRoot '.github\workflows\sfl-pr-review-auto.yml' -$deployedPath = Join-Path $repoRoot 'deployment\infrastructure\sfl-pr-review-auto.yml' -$canonical = Get-Content -LiteralPath $canonicalPath -Raw -$deployed = Get-Content -LiteralPath $deployedPath -Raw - -if (-not (Test-NormalizedTextEqual $canonical $deployed)) { - throw 'Canonical and deployable auto-review dispatchers differ.' -} - -$match = [regex]::Match( - $canonical, - '(?ms)^\s*# BEGIN TESTABLE DISPATCH DEDUP\r?\n(?.*?)^\s*# END TESTABLE DISPATCH DEDUP' -) -if (-not $match.Success) { - throw 'Could not extract the testable dispatch deduplication function.' -} - -$functionBody = $match.Groups['body'].Value -replace '(?m)^ ', '' -$isWindowsPlatform = [System.IO.Path]::DirectorySeparatorChar -eq '\' -$bashPath = if ($isWindowsPlatform -and (Test-Path -LiteralPath 'C:\Program Files\Git\bin\bash.exe')) { - 'C:\Program Files\Git\bin\bash.exe' -} else { - (Get-Command bash -ErrorAction Stop).Source -} - -$tempDirectory = Join-Path ([System.IO.Path]::GetTempPath()) ("sfl-dispatch-dedup-{0}" -f [guid]::NewGuid()) -New-Item -ItemType Directory -Path $tempDirectory | Out-Null -$scriptPath = Join-Path $tempDirectory 'test-dispatch-dedup.sh' - -try { - $script = @" -set -euo pipefail -$functionBody -if [ "`$1" = '--statuses' ]; then - reusable_review_statuses - exit 0 -fi -find_reusable_review_run "`$1" "`$2" -"@ - Set-Content -LiteralPath $scriptPath -Value $script -Encoding utf8NoBOM - - $expectedTitle = 'SFL PR Review #49 base:head retry=' - $statuses = @(& $bashPath $scriptPath --statuses) - if ($LASTEXITCODE -ne 0) { - throw 'Reusable review statuses failed to execute.' - } - if (($statuses -join ',') -cne 'queued,in_progress,success') { - throw "Reusable review statuses were '$($statuses -join ',')'; expected queued,in_progress,success." - } - - $fixtures = @( - @{ - Name = 'queued exact-state run' - LabelPresent = 'false' - Expected = '102' - Json = '{"workflow_runs":[{"id":101,"status":"queued","display_title":"SFL PR Review #49 base:head retry=0 dispatch=first"},{"id":102,"status":"queued","display_title":"SFL PR Review #49 base:head retry=0 dispatch=second"}]}' - }, - @{ - Name = 'in-progress exact-state run' - LabelPresent = 'false' - Expected = '201' - Json = '{"workflow_runs":[{"id":201,"status":"in_progress","display_title":"SFL PR Review #49 base:head retry=0 dispatch=active"}]}' - }, - @{ - Name = 'successful completed exact-state run' - LabelPresent = 'false' - Expected = '301' - Json = '{"workflow_runs":[{"id":301,"status":"completed","conclusion":"success","display_title":"SFL PR Review #49 base:head retry=0 dispatch=completed"}]}' - }, - @{ - Name = 'unrelated completed history' - LabelPresent = 'false' - Expected = '' - Json = '{"workflow_runs":[{"id":401,"status":"completed","conclusion":"success","display_title":"SFL PR Review #48 other:head retry=0 dispatch=other"}]}' - }, - @{ - Name = 'live explicit label bypass from any concurrent event' - LabelPresent = 'true' - Expected = '' - Json = '{"workflow_runs":[{"id":501,"status":"completed","conclusion":"success","display_title":"SFL PR Review #49 base:head retry=0 dispatch=completed"}]}' - } - ) - - foreach ($fixture in $fixtures) { - $output = @($fixture.Json | & $bashPath $scriptPath $expectedTitle $fixture.LabelPresent) - if ($LASTEXITCODE -ne 0) { - throw "Dispatch dedup fixture failed to execute: $($fixture.Name)" - } - $actual = $output -join "`n" - if ($actual -cne $fixture.Expected) { - throw "Dispatch dedup fixture '$($fixture.Name)' returned '$actual'; expected '$($fixture.Expected)'." - } - } -} finally { - Remove-Item -LiteralPath $tempDirectory -Recurse -Force -ErrorAction SilentlyContinue -} - -Write-Output 'SFL dispatch deduplication fixtures passed.' diff --git a/deployment/tests/test-sfl-pr-review.ps1 b/deployment/tests/test-sfl-pr-review.ps1 index baaad3cf..51e2788a 100644 --- a/deployment/tests/test-sfl-pr-review.ps1 +++ b/deployment/tests/test-sfl-pr-review.ps1 @@ -6,227 +6,294 @@ $ErrorActionPreference = 'Stop' $repoRoot = (Resolve-Path (Join-Path $PSScriptRoot '..\..')).ProviderPath . (Join-Path $PSScriptRoot 'line-ending-test-helpers.ps1') -$sourcePath = Join-Path $repoRoot 'deployment\workflows\sfl-pr-review.md' -$stagedPath = Join-Path $repoRoot '.github\workflows\sfl-pr-review.md' -$lockPath = Join-Path $repoRoot '.github\workflows\sfl-pr-review.lock.yml' -$actionsLockPath = Join-Path $repoRoot '.github\aw\actions-lock.json' -$failures = [System.Collections.Generic.List[string]]::new() -foreach ($path in @($sourcePath, $stagedPath, $lockPath, $actionsLockPath)) { +$canonicalPath = Join-Path $repoRoot 'deployment\infrastructure\sfl-pr-review-auto.yml' +$stagedPath = Join-Path $repoRoot '.github\workflows\sfl-pr-review-auto.yml' +foreach ($path in @($canonicalPath, $stagedPath)) { if (-not (Test-Path -LiteralPath $path -PathType Leaf)) { - $failures.Add("Missing reviewer artifact: $path") + throw "Missing subscription-backed Codex observer: $path" } } -if ($failures.Count -eq 0) { - $source = Get-Content -LiteralPath $sourcePath -Raw - $staged = Get-Content -LiteralPath $stagedPath -Raw - $lock = Get-Content -LiteralPath $lockPath -Raw - $actionsLock = Get-Content -LiteralPath $actionsLockPath -Raw - - if (-not (Test-NormalizedTextEqual $source $staged)) { - $failures.Add('Canonical and staged reviewer Markdown differ.') - } - - $deployScriptPath = Join-Path $repoRoot 'deployment\scripts\deploy-workflow.ps1' - $deployScript = Get-Content -LiteralPath $deployScriptPath -Raw - $rendererStart = $deployScript.IndexOf('function ConvertTo-SflWorkflowWithEnginePolicy') - $rendererEnd = $deployScript.IndexOf('function New-SflEnginePolicyManifest', $rendererStart) - if ($rendererStart -lt 0 -or $rendererEnd -le $rendererStart) { - $failures.Add('Could not load the PowerShell engine-policy renderer for behavioral testing.') - } else { - Invoke-Expression $deployScript.Substring($rendererStart, $rendererEnd - $rendererStart) - - $reviewerProfile = [pscustomobject]@{ - Provider = 'copilot' - Arguments = @() - Environment = [pscustomobject][ordered]@{ - COPILOT_PROVIDER_WIRE_API = 'responses' - COPILOT_PROVIDER_TYPE = 'openai' - COPILOT_PROVIDER_BASE_URL = 'https://openrouter.ai/api/v1' - COPILOT_PROVIDER_API_KEY = '${{ secrets.OPENROUTER_API_KEY }}' - COPILOT_MODEL = 'moonshotai/kimi-k3' - } - RenderedModel = 'moonshotai/kimi-k3' - } - $renderedReviewer = ConvertTo-SflWorkflowWithEnginePolicy ` - -Content $source ` - -EngineProfile $reviewerProfile ` - -WorkflowName 'sfl-pr-review' - if (-not (Test-NormalizedTextEqual $source $renderedReviewer)) { - $failures.Add('PowerShell engine-policy rendering changes the canonical reviewer and invalidates its generated lock.') - } - - # Go uses sort.Strings, so PowerShell must preserve the same ordinal order. - $mixedCaseProfile = [pscustomobject]@{ - Provider = 'copilot' - Arguments = @() - Environment = [pscustomobject][ordered]@{ - dKey = 'lower-d' - aKey = 'lower-a' - CKey = 'upper-c' - BKey = 'upper-b' - } - RenderedModel = 'example' - } - $mixedCaseRendered = ConvertTo-SflWorkflowWithEnginePolicy ` - -Content "---`nname: Mixed case`nnetwork: defaults`n---`n" ` - -EngineProfile $mixedCaseProfile ` - -WorkflowName 'mixed-case' - $renderedEnvironmentNames = @( - [regex]::Matches($mixedCaseRendered, '(?m)^ (?[A-Za-z]+):') | - ForEach-Object { $_.Groups['name'].Value } - ) - if (($renderedEnvironmentNames -join ',') -cne 'BKey,CKey,aKey,dKey') { - $failures.Add("PowerShell engine environment order is not ordinal: $($renderedEnvironmentNames -join ',')") - } - } +$canonical = Get-Content -LiteralPath $canonicalPath -Raw +$staged = Get-Content -LiteralPath $stagedPath -Raw +if (-not (Test-NormalizedTextEqual $canonical $staged)) { + throw 'Canonical and staged Codex observers differ.' +} - $requiredSourcePatterns = @( - '(?m)^source: HemSoft/set-it-free-loop/deployment/workflows/sfl-pr-review\.md@main\r?$', - '(?m)^ workflow_dispatch:\r?$', - 'Validate trusted review context', - 'Install ripgrep with bounded diagnostics', - 'timeout --signal=TERM --kill-after=15s 180s', - 'Retain ripgrep setup diagnostics', - 'sfl-ripgrep-setup-\$\{\{ github\.run_id \}\}-\$\{\{ github\.run_attempt \}\}', - 'Install threat-detection ripgrep with bounded diagnostics', - 'Retain threat-detection ripgrep setup diagnostics', - 'sfl-threat-detection-ripgrep-setup-\$\{\{ github\.run_id \}\}-\$\{\{ github\.run_attempt \}\}', - 'publish_review_provenance:', - 'Initialize review evidence', - 'sfl-review:\$\{pullNumber\}:\$\{baseSha\}:\$\{headSha\}:\$\{runId\}', - 'Verify pull request base and head before safe outputs', - 'commit-id: "\$\{\{ inputs\.head_sha \}\}"', - 'resolve-sfl-review-thread:', - "if: needs\.safe_outputs\.result == 'success'", - 'isOutdated', - '\(!thread\.isResolved && !thread\.isOutdated\)', - 'const unresolvedOutdatedThreadIds = \[\]', - 'for \(const threadId of unresolvedOutdatedThreadIds\)', - 'if \(thread\.isResolved\) \{\s*core\.info\(`SFL review thread \$\{threadId\} is already resolved`\);\s*continue;', - 'whose thread GitHub reports as outdated', - 'Never request resolution for a live unresolved thread', - 'name: safe-outputs-items', - 'Download reviewer agent output', - 'Expected exactly one immutable agent submitted-review item', - 'Published review manifest does not match the immutable agent verdict', - 'Review body contains unresolved template placeholder', - 'const immutableFindingCount = agentReviewComments\.length', - 'const gateApproved =\s*\r?\n\s*verdictApproved', - 'unresolvedFindingCount === 0', - 'Initialized SFL review evidence does not match this run', - 'COPILOT_PROVIDER_BASE_URL: https://openrouter\.ai/api/v1', - 'COPILOT_MODEL: moonshotai/kimi-k3', - '(?m)^max-turn-cache-misses: 10\r?$', - '(?m)^ providers:\r?$', - '(?m)^ github-copilot:\r?$', - '(?m)^ "moonshotai/kimi-k3":\r?$', - '(?m)^ input: "3e-06"\r?$', - '(?m)^ output: "1\.5e-05"\r?$' - ) - foreach ($pattern in $requiredSourcePatterns) { - if ($source -notmatch $pattern) { - $failures.Add("Reviewer source is missing contract pattern: $pattern") - } +foreach ($pattern in @( + 'name: SFL Codex Review Observer', + "format('SFL Codex review request #{0}', github.event.issue.number)", + 'github.event.sender.id == 199175422', + 'const appId = 1144995', + 'const appSlug = "chatgpt-codex-connector"', + 'const appOwner = "openai"', + 'Date.parse(comment.created_at || "")', + 'Date.parse(comment.updated_at || "")', + 'pull.data.state !== "open"', + 'SFL reviews require the default branch', + 'SFL_REVIEW_BASE_BRANCH: main', + 'differs from deployed SFL review base', + 'types: [opened, reopened, edited, synchronize]', + 'sfl-codex-review:pull:${pullNumber}:base:${currentBase.toLowerCase()}', + 'sfl-codex-review:pull:${pull.number}:base:${pull.base.sha.toLowerCase()}', + 'const baseMarker = `${headMarker}${currentBase.toLowerCase()};`', + 'const isOwnerRequest = comment =>', + 'comment.user && comment.user.login', + 'The pull request context changed while the Codex result was being published', + 'Codex artifact and registered request have ambiguous same-second ordering', + 'invalidate-review-request:', + 'statuses: read', + "vars.SFL_ENABLED != 'false'", + 'SFL Codex Review Request Registry', + 'sfl-codex-review:request-pending:${commentId}', + 'const terminalRequestIdentity = `:request:${commentId}:at:`', + 'This registered Codex review request already has a terminal SFL gate', + 'registrations.has(comment.id)', + 'const publishedCompletedAt = new Date().toISOString()', + 'completed_at: publishedCompletedAt', + 'status: "in_progress"', + 'SFL Codex review validating', + 'conclusion: "success"', + 'GET /repos/{owner}/{repo}/issues/{issue_number}/events', + 'event.event === "closed" || event.event === "reopened"', + 'lifecycleToken !== initialLifecycleToken', + 'state.invalidationRuns.length > 0', + 'const publicationState = async () =>', + 'const postSuccessState = await publicationState()', + 'state.openPulls.length !== 1', + 'registeredRequestIdsFromStatuses(statuses)', + 'let latestPotentialRequestRegistered = false', + 'visibleRegistrations.has(latestPotentialRequest.comment.id)', + 'if (attempt < 11) await new Promise(resolve => setTimeout(resolve, 5000))', + 'No registered Codex review request materialized for this artifact', + 'context=${contextToken}', + 'function requestGateExternalId(', + 'function requestGateExternalIdPrefix(', + 'request:${requestId}:at:${requestTime}', + ':artifact:${artifactIdentity}', + 'const artifactAlreadyConsumed = artifactChecks.some(', + 'pending-invalidation:${pendingRun.id}:artifact:${artifactIdentity}', + 'This exact Codex artifact already completed a review request', + 'const pendingRequest = candidateRequests[0]', + 'contextMatch = /;context=(.*?) -->/.exec', + 'pendingRequest.comment.id', + 'const artifactBindsCurrentHead = eventName === "issue_comment"', + 'it cannot complete the pending request', + 'const eligibleReviewRequests = (', + 'completedTime < nextRequestTime', + 'const hasNewerMatchingRequest = (comments, checkRuns, statuses) =>', + 'comment.id > request.comment.id', + 'A newer Codex review request superseded this artifact before publication', + 'supersededRequest', + 'This Codex review request already has a terminal SFL gate', + 'sfl-codex-review-serialize-${{ github.repository }}-${{ github.event.issue.number || github.event.pull_request.number }}', + 'const pendingRequestAlreadyTerminal = artifactChecks.some(', + 'const externalIdPrefix = requestGateExternalIdPrefix(', + 'pull-context:at:${contextChangeTime}', + 'base-advance:at:${baseAdvanceTime}', + 'sfl-codex-review-pull-context-${{ github.repository }}-${{ github.event.pull_request.number }}', + 'github.rest.actions.listWorkflowRuns', + 'const initialChecks = await github.paginate(', + 'const checks = await github.paginate(', + 'const activeStatuses = ["requested", "queued", "in_progress", "waiting", "pending"]', + 'const events = ["pull_request_target", "push", "issue_comment"]', + 'activeStatuses.map(status => github.paginate(', + 'events.includes(run.event)', + 'run.display_title === `SFL Codex review request #${pullNumber}`', + 'actions: read', + 'if (run.event === "push") return true', + 'run.actor && run.actor.login', + 'run.status === "completed"', + 'Allowing an invalidation run time to materialize before publishing the Codex result', + 'const finalInvalidationRuns = await activeInvalidationRuns()', + 'the Codex result cannot supersede its invalidation', + 'sfl-codex-review-base-advance-${{ github.repository }}-${{ github.ref }}', + 'const batchSize = 10', + 'Promise.allSettled(batch.map(invalidatePull))', + 'Base-advance invalidation failed after attempting every pull request', + 'supersedesInvalidation(check.external_id, contextChangeTime, context.runId)', + 'supersedesInvalidation(check.external_id, baseAdvanceTime, context.runId)', + 'already has this exact invalidation; skipping rerun', + 'received a newer context or successful Codex gate while invalidation was running', + 'conflictingBaseRequest', + 'reviewContextToken(confirmedChecks) !== contextToken', + ':context:${encodeURIComponent(invalidationId)}:', + 'skipping stale invalidation', + 'github.rest.actions.getWorkflowRun', + 'successful Codex gate completed after this context change', + 'if (context.payload.deleted)', + 'repository.data.default_branch', + 'Repository default branch changed from deployed SFL review base', + 'name: "SFL Reviewer Gate Runner"', + 'check.app.id === 15368', + 'action: "success"', + 'action: "failure"', + 'action: "ignore"' +)) { + if ($canonical -notmatch [regex]::Escape($pattern)) { + throw "Codex observer is missing contract text: $pattern" } +} - $requiredLockPatterns = @( - '"compiler_version":"v0\.86\.2"', - '"engine_base_url_customized":true', - '"agent_model":"moonshotai/kimi-k3"', - '\\"maxCacheMisses\\":10', - '"OPENROUTER_API_KEY"', - 'GH_AW_INPUTS_BASE_SHA: \$\{\{ inputs\.base_sha \}\}', - 'GH_AW_INPUTS_HEAD_SHA: \$\{\{ inputs\.head_sha \}\}', - 'Validate trusted review context', - 'Install ripgrep with bounded diagnostics', - 'timeout --signal=TERM --kill-after=15s 180s', - 'Retain ripgrep setup diagnostics', - 'sfl-ripgrep-setup-\$\{\{ github\.run_id \}\}-\$\{\{ github\.run_attempt \}\}', - 'Install threat-detection ripgrep with bounded diagnostics', - 'Retain threat-detection ripgrep setup diagnostics', - 'sfl-threat-detection-ripgrep-setup-\$\{\{ github\.run_id \}\}-\$\{\{ github\.run_attempt \}\}', - 'publish_review_provenance:', - 'Initialize review evidence', - 'Verify pull request base and head before safe outputs', - 'SFL_SAFE_OUTPUT_ITEMS: /tmp/sfl-review-safe-outputs/safe-output-items\.jsonl', - 'Initialized SFL review evidence does not match this run', - 'isOutdated', - '\(!thread\.isResolved && !thread\.isOutdated\)', - 'const unresolvedOutdatedThreadIds = \[\]', - 'for \(const threadId of unresolvedOutdatedThreadIds\)', - '\{\{#runtime-import \.github/workflows/sfl-pr-review\.md\}\}' - ) - foreach ($pattern in $requiredLockPatterns) { - if ($lock -notmatch $pattern) { - $failures.Add("Compiled reviewer is missing contract pattern: $pattern") - } - } +if ($canonical -match [regex]::Escape('if (context.payload.deleted) return;')) { + throw 'Base-branch deletion events are still discarded instead of invalidating gates after a rename.' +} +if ($canonical -match [regex]::Escape('events.flatMap(') -or + $canonical -match [regex]::Escape('events.map(event => github.paginate(')) { + throw 'Codex observer still multiplies or unboundedly paginates workflow-run queries by event.' +} +if ($canonical -notmatch '(?s)invalidate-base-advance:.*?timeout-minutes: 30' -or + $canonical -match [regex]::Escape('for (const pull of openPulls)')) { + throw 'Base-advance invalidation is still a short, serial repository-wide sweep.' +} - $compiledPricingPattern = '\\"providers\\":\{\\"github-copilot\\":\{\\"models\\":\{\\"moonshotai/kimi-k3\\":\{\\"cost\\":\{\\"input\\":\\"3e-06\\",\\"output\\":\\"1\.5e-05\\"\}\}\}\}\}' - $compiledPricingCount = [regex]::Matches($lock, $compiledPricingPattern).Count - if ($compiledPricingCount -ne 2) { - $failures.Add("Expected explicit Kimi pricing in both primary and threat-detection firewall configurations; found $compiledPricingCount occurrence(s).") - } - if ($source -match 'default-ai-credits-pricing' -or - $lock -match 'defaultAiCreditsPricing') { - $failures.Add('Reviewer still relies on generic fallback pricing instead of explicit Kimi pricing.') - } +$artifactBindingIndex = $canonical.IndexOf('const artifactBindsCurrentHead') +$pendingRequestIndex = $canonical.IndexOf('const pendingRequest = candidateRequests[0]') +if ($artifactBindingIndex -lt 0 -or $pendingRequestIndex -lt 0 -or + $artifactBindingIndex -gt $pendingRequestIndex) { + throw 'Codex artifact binding is not validated before pending request selection.' +} - if ($source -match 'copilot-requests:\s*write' -or - $lock -match 'copilot-requests:\s*write') { - $failures.Add('HemSoft OpenRouter reviewer unexpectedly requests Copilot billing permission.') - } - if ($source -match 'needs\.detection\.result') { - $failures.Add('Thread resolution references an undeclared detection dependency.') - } - if ($source -match '(?im)^\s*(?:app-id|github-app-id|installation-id|client-id):\s*(?:\d+|Iv[A-Za-z0-9]+)\s*$') { - $failures.Add('Reviewer source contains a hard-coded GitHub App or installation identity.') - } - if ($actionsLock -notmatch 'github/gh-aw-actions/setup@v0\.86\.2') { - $failures.Add('Action lock does not pin the compiler-matched gh-aw setup action.') - } +$inProgressIndex = $canonical.IndexOf('status: "in_progress"') +$confirmationIndex = if ($inProgressIndex -ge 0) { + $canonical.IndexOf('const confirmedState = await publicationState()', $inProgressIndex) +} else { -1 } +$successIndex = if ($confirmationIndex -ge 0) { + $canonical.IndexOf('conclusion: "success"', $confirmationIndex) +} else { -1 } +if ($inProgressIndex -lt 0 -or $confirmationIndex -lt 0 -or $successIndex -lt 0 -or + $inProgressIndex -gt $confirmationIndex -or $confirmationIndex -gt $successIndex) { + throw 'Codex success is exposed before the final pull-context confirmation.' +} - $eligibilityGuardIndex = $source.IndexOf('(!thread.isResolved && !thread.isOutdated)') - $resolvedGuardIndex = $source.IndexOf('if (thread.isResolved)') - $resolutionQueueIndex = $source.IndexOf('unresolvedOutdatedThreadIds.push(threadId)') - $resolutionMutationIndex = $source.IndexOf('resolveReviewThread(input: { threadId: $threadId })') - if ($eligibilityGuardIndex -lt 0 -or - $resolvedGuardIndex -le $eligibilityGuardIndex -or - $resolutionQueueIndex -le $resolvedGuardIndex -or - $resolutionMutationIndex -le $resolutionQueueIndex) { - $failures.Add('Thread eligibility, idempotency, queueing, and mutation are not ordered fail-closed.') - } +$eligibilityMatch = [regex]::Match( + $canonical, + '(?s)// BEGIN TESTABLE REQUEST ELIGIBILITY\s*(.*?)\s*// END TESTABLE REQUEST ELIGIBILITY' +) +if (-not $eligibilityMatch.Success) { + throw 'Could not locate the testable request-eligibility block.' +} +$eligibilityTest = @' +const owner = "HemSoft"; +const pullNumber = 42; +const currentBase = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"; +const baseMarker = ``, +}); +const first = comment(1, "2026-08-19T00:00:01Z", "none"); +const second = comment(2, "2026-08-19T00:00:02Z", "none"); +const otherContext = comment(3, "2026-08-19T00:00:02Z", "base-advance"); +const recoveredRetry = comment(4, "2026-08-19T00:00:04Z", "none"); +const unregistered = comment(5, "2026-08-19T00:00:05Z", "none"); +const editedSecond = {...second, updated_at: "2026-08-19T00:00:03Z"}; +const sameSecondBodyMutation = {...second, body: `please @codex review\n\n${baseMarker}context=none -->`}; +const firstTime = Date.parse(first.created_at); +const firstPrefix = requestGateExternalIdPrefix(pullNumber, currentBase, "none", first.id, firstTime); +const terminal = completed_at => ({ + app: {id: 15368}, + status: "completed", + completed_at, + external_id: `${firstPrefix}:artifact:r123`, +}); +const cases = [ + {name: "overlap blocked", comments: [first, second], checks: [], want: [1]}, + {name: "terminal predecessor allows retry", comments: [first, second], checks: [terminal("2026-08-19T00:00:01.500Z")], want: [1, 2]}, + {name: "same-second terminal fails closed", comments: [first, second], checks: [terminal("2026-08-19T00:00:02Z")], want: [1]}, + {name: "edited marker is rejected", comments: [first, editedSecond], checks: [terminal("2026-08-19T00:00:01.500Z")], want: [1]}, + {name: "same-second body mutation is rejected", comments: [first, sameSecondBodyMutation], checks: [terminal("2026-08-19T00:00:01.500Z")], want: [1]}, + {name: "late terminal does not authorize retry", comments: [first, second], checks: [terminal("2026-08-19T00:00:02.500Z")], want: [1]}, + {name: "later retry recovers after overlap", comments: [first, second, recoveredRetry], checks: [terminal("2026-08-19T00:00:03Z")], want: [1, 4]}, + {name: "different contexts are independent", comments: [first, otherContext], checks: [], want: [1, 3]}, + {name: "unregistered owner marker is rejected", comments: [first, unregistered], checks: [terminal("2026-08-19T00:00:01.500Z")], want: [1]}, +]; +const sameSecondCandidates = eligibleReviewRequests([first], [], Date.parse(first.created_at)) + .map(candidate => candidate.comment.id); +if (JSON.stringify(sameSecondCandidates) !== JSON.stringify([1])) { + throw new Error(`same-second request lookup: got ${JSON.stringify(sameSecondCandidates)}, want [1]`); +} +const refreshedRegistrations = new Set([1, 2, 3, 4, 5]); +const refreshedCandidates = eligibleReviewRequests( + [first, unregistered], + [terminal("2026-08-19T00:00:01.500Z")], + Number.POSITIVE_INFINITY, + refreshedRegistrations, +).map(candidate => candidate.comment.id); +if (JSON.stringify(refreshedCandidates) !== JSON.stringify([1, 5])) { + throw new Error(`refreshed registrations: got ${JSON.stringify(refreshedCandidates)}, want [1,5]`); +} +for (const fixture of cases) { + const got = eligibleReviewRequests(fixture.comments, fixture.checks, Number.POSITIVE_INFINITY) + .map(candidate => candidate.comment.id); + if (JSON.stringify(got) !== JSON.stringify(fixture.want)) { + throw new Error(`${fixture.name}: got ${JSON.stringify(got)}, want ${JSON.stringify(fixture.want)}`); + } +} +'@ +$eligibilityTest | node - +if ($LASTEXITCODE -ne 0) { + throw 'Request-eligibility fixture tests failed.' } -function Get-ExpectedThreadResolutionEligibility { - param( - [bool] $IsResolved, - [bool] $IsOutdated - ) +$helperMatches = [regex]::Matches( + $canonical, + '(?s)// BEGIN TESTABLE INVALIDATION ORDER\s*(.*?)\s*// END TESTABLE INVALIDATION ORDER' +) +if ($helperMatches.Count -ne 2) { + throw "Expected two testable invalidation-order helpers, found $($helperMatches.Count)." +} +if ($helperMatches[0].Groups[1].Value -ne $helperMatches[1].Groups[1].Value) { + throw 'Pull-context and base-advance invalidation-order helpers differ.' +} - return $IsResolved -or $IsOutdated +$helperTest = @" +$($helperMatches[0].Groups[1].Value) +const cases = [ + ["sfl-codex-review:pull-context:at:2000:20", 1000, 10, true], + ["sfl-codex-review:base-advance:at:1000:20:42", 1000, 10, true], + ["sfl-codex-review:pull-context:at:999:99", 1000, 10, false], + ["sfl-codex-review:pull-context:at:1000:10", 1000, 10, false], + ["sfl-codex-review:pull:42:base:abc", 1000, 10, false], +]; +for (const [externalId, eventTime, runId, expected] of cases) { + const actual = supersedesInvalidation(externalId, eventTime, runId); + if (actual !== expected) { + throw new Error("Unexpected ordering for " + externalId + ": " + actual); + } +} +"@ +$helperTest | node - +if ($LASTEXITCODE -ne 0) { + throw 'Invalidation-order helper tests failed.' } -$threadStateCases = @( - @{ Name = 'live unresolved thread'; Resolved = $false; Outdated = $false; Expected = $false }, - @{ Name = 'outdated unresolved thread'; Resolved = $false; Outdated = $true; Expected = $true }, - @{ Name = 'already resolved current thread'; Resolved = $true; Outdated = $false; Expected = $true }, - @{ Name = 'already resolved outdated thread'; Resolved = $true; Outdated = $true; Expected = $true } -) -foreach ($case in $threadStateCases) { - $actual = Get-ExpectedThreadResolutionEligibility ` - -IsResolved $case.Resolved ` - -IsOutdated $case.Outdated - if ($actual -ne $case.Expected) { - $failures.Add("Thread eligibility case failed: $($case.Name)") +foreach ($legacyPath in @( + 'deployment\workflows\sfl-pr-review.md', + 'deployment\infrastructure\sfl-pr-review-recovery.yml', + '.github\workflows\sfl-pr-review.md', + '.github\workflows\sfl-pr-review.lock.yml', + '.github\workflows\sfl-pr-review-recovery.yml' +)) { + if (Test-Path -LiteralPath (Join-Path $repoRoot $legacyPath)) { + throw "Retired OpenRouter reviewer artifact remains: $legacyPath" } } -if ($failures.Count -gt 0) { - $failures | ForEach-Object { Write-Error $_ } - throw "SFL review artifact contract failed with $($failures.Count) finding(s)." +$policy = Get-Content -LiteralPath (Join-Path $repoRoot 'deployment\engine-policy.json') -Raw +if ($policy -match '(?i)openrouter|moonshotai/kimi|OPENROUTER_API_KEY') { + throw 'Engine policy retained the OpenRouter reviewer configuration.' } -Write-Output 'SFL review artifact contract passed.' +Write-Output 'Subscription-backed Codex reviewer contract tests passed.' diff --git a/deployment/tests/test-sfl-review-platform.ps1 b/deployment/tests/test-sfl-review-platform.ps1 index 4ee5b37a..90d08e28 100644 --- a/deployment/tests/test-sfl-review-platform.ps1 +++ b/deployment/tests/test-sfl-review-platform.ps1 @@ -5,291 +5,63 @@ Set-StrictMode -Version Latest $ErrorActionPreference = 'Stop' $repoRoot = (Resolve-Path (Join-Path $PSScriptRoot '..\..')).ProviderPath -. (Join-Path $PSScriptRoot 'line-ending-test-helpers.ps1') -$failures = [System.Collections.Generic.List[string]]::new() - -function Read-RepoFile([string] $RelativePath) { - $path = Join-Path $repoRoot $RelativePath - if (-not (Test-Path -LiteralPath $path -PathType Leaf)) { - $failures.Add("Missing file: $RelativePath") - return '' - } - return Get-Content -LiteralPath $path -Raw +$files = @{ + Init = Get-Content -LiteralPath (Join-Path $repoRoot 'gh-sfl\init.go') -Raw + Review = Get-Content -LiteralPath (Join-Path $repoRoot 'gh-sfl\review.go') -Raw + Deploy = Get-Content -LiteralPath (Join-Path $repoRoot 'deployment\scripts\deploy-workflow.ps1') -Raw + LabelsJson = Get-Content -LiteralPath (Join-Path $repoRoot 'deployment\governance\labels.json') -Raw + LabelSetup = Get-Content -LiteralPath (Join-Path $repoRoot 'deployment\governance\setup-labels.ps1') -Raw + Observer = Get-Content -LiteralPath (Join-Path $repoRoot 'deployment\infrastructure\sfl-pr-review-auto.yml') -Raw } -function Assert-PatternSet( - [string] $RelativePath, - [string[]] $Patterns -) { - $content = Read-RepoFile $RelativePath - foreach ($pattern in $Patterns) { - if ($content -notmatch $pattern) { - $failures.Add("$RelativePath does not contain pattern: $pattern") - } +foreach ($required in @( + 'codexReviewCommand', + '"@codex review"', + 'sfl-codex-review:', + 'findCodexReviewTrigger', + 'sfl-pr-review-auto.yml' +)) { + if ($files.Review -notmatch [regex]::Escape($required)) { + throw "gh sfl review is missing contract text: $required" } } -function Assert-PatternAbsent( - [string] $RelativePath, - [string] $Pattern -) { - $content = Read-RepoFile $RelativePath - if ($content -match $Pattern) { - $failures.Add("$RelativePath contains forbidden pattern: $Pattern") - } +if ($files.Init -match 'sfl-pr-review\.lock\.yml|sfl-pr-review-recovery\.yml' -or + $files.Deploy -match '@\("sfl-pr-review-auto", "sfl-pr-review-recovery"\)') { + throw 'Deployment tiers still include the retired compiled reviewer or recovery workflow.' } -function Assert-ExactPair( - [string] $CanonicalPath, - [string] $StagedPath -) { - $canonical = Read-RepoFile $CanonicalPath - $staged = Read-RepoFile $StagedPath - if (-not (Test-NormalizedTextEqual $canonical $staged)) { - $failures.Add("Staged file differs from canonical source: $StagedPath") +foreach ($labelContract in @($files.LabelsJson, $files.LabelSetup)) { + if ($labelContract -notmatch 'Deprecated: use gh sfl review; applying this label does not trigger a review') { + throw 'The retired sfl-review label still advertises an active review trigger.' } } -$reviewer = 'deployment\workflows\sfl-pr-review.md' -$auto = 'deployment\infrastructure\sfl-pr-review-auto.yml' -$recovery = 'deployment\infrastructure\sfl-pr-review-recovery.yml' - -Assert-ExactPair $reviewer '.github\workflows\sfl-pr-review.md' -Assert-ExactPair $auto '.github\workflows\sfl-pr-review-auto.yml' -Assert-ExactPair $recovery '.github\workflows\sfl-pr-review-recovery.yml' - -Assert-PatternSet $reviewer @( - 'source: HemSoft/set-it-free-loop/deployment/workflows/sfl-pr-review\.md@main', - 'COPILOT_PROVIDER_BASE_URL: https://openrouter\.ai/api/v1', - 'COPILOT_MODEL: moonshotai/kimi-k3', - 'item_number:', - 'base_sha:', - 'head_sha:', - 'Validate trusted review context', - 'sfl-review-run-provenance', - 'external_id:', - 'Validate and resolve requested SFL threads', - 'Thread \$\{threadId\} is not an obsolete SFL finding', - 'Could not enumerate unresolved SFL findings', - 'Critical, High, Medium, or Low', - 'SFL Reviewer Approval', - 'sfl-app\[bot\]' -) -Assert-PatternAbsent $reviewer 'set-it-free-loop\[bot\]' - -Assert-PatternSet $auto @( - "vars.SFL_ENABLED != 'false'", - 'issue_comment:\s*\r?\n\s+types: \[created\]', - "github\.event\.comment\.body == '@sfl-app review'", - '\["OWNER","MEMBER","COLLABORATOR"\]', - "github\.event_name != 'issue_comment'", - 'name: Validate command and seal pull request state', - 'sfl-review-command:\$\{COMMAND_COMMENT_ID\}', - 'DISPATCH_ID="command-\$\{COMMAND_COMMENT_ID\}"', - 'find_command_acknowledgement', - 'find_command_run', - 'select\(\.display_title \| endswith\(" dispatch=" \+ \$dispatch_id\)\)', - 'command_acknowledgement_complete', - 'command_acknowledgement_run_id', - 'command_run_matches', - 'repos/\$\{REPOSITORY\}/actions/runs/\$\{ACKNOWLEDGED_RUN_ID\}', - 'Ignoring stale SFL acknowledgement run', - 'test\("\\\\\[Actions run \[1-9\]\[0-9\]\*\\\\\]\\\\\("\)', - '\(capture\("\\\\\[Actions run \(\?\[1-9\]\[0-9\]\*\)\\\\\]\\\\\("\)\? \| \.id\) // empty', - 'Command comment \$\{COMMAND_COMMENT_ID\} was already acknowledged', - 'DRAFT=\$\(jq -r ''if \.draft == false then false else true end'' <<< "\$PR"\)', - 'for ATTEMPT in \$\(seq 1 120\); do', - 'inputs\[dispatch_id\]=\$\{DISPATCH_ID\}', - 'inputs\[review_effort\]=\$\{REVIEW_EFFORT\}', - 'expected-review-run-id: \$\{\{ steps\.request-review\.outputs\.review-run-id \}\}', - 'needs: \[comment-command, dispatch\]', - 'needs\.comment-command\.outputs\.expected-review-run-id', - 'Could not resolve the exact comment-dispatched SFL review run', - 'SFL review was requested for `%s`, but its Actions run could not be correlated\.', - '\[Command run\]\(%s\)', - 'SFL review requested for `%s`: \[Actions run %s\]\(%s\)', - 'types: \[opened, synchronize, reopened, ready_for_review, edited, review_requested, labeled\]', - '\$\(normalize_login "\$REVIEW_AUTHOR"\)" = "sfl-app"', - '\$\(normalize_login "\$REQUESTED_REVIEWER"\)" != "sfl-app"', - 'pull_request_review:\s*\r?\n\s+types: \[submitted\]', - "github\.event\.label\.name == 'sfl-review'", - 'HEAD_REPOSITORY: \$\{\{ github\.event\.pull_request\.head\.repo\.full_name \}\}', - 'if \[ "\$HEAD_REPOSITORY" != "\$REPOSITORY" \]', - 'if \[ "\$BASE_REF" != "\$DEFAULT_BRANCH" \]', - 'name: Detect installed reviewer contract', - 'reviewer-installed: \$\{\{ steps\.reviewer-contract\.outputs\.installed \}\}', - "steps\.reviewer-contract\.outputs\.installed == 'true'", - 'Reviewer bootstrap deployment detected', - 'permission-pull-requests: write', - 'consume_review_label', - 'resolve_review_effort', - 'reusable_review_statuses', - 'for STATUS in \$\(reusable_review_statuses\)', - 'runs\?status=\$\{STATUS\}', - 'find_reusable_review_run', - 'already active or successfully completed', - 'first observes a live explicit request', - 'inputs\[base_sha\]', - 'inputs\[head_sha\]', - 'name: SFL Reviewer Gate Runner', - 'name: Resolve immutable approval context', - 'PR_NUMBER: \$\{\{ github\.event\.pull_request\.number \|\| needs\.comment-command\.outputs\.pr-number \|\| github\.event\.issue\.number \}\}', - 'PR=\$\(gh api "repos/\$\{REPOSITORY\}/pulls/\$\{PR_NUMBER\}"\)', - 'Could not resolve immutable SFL approval context', - 'BASE_SHA: \$\{\{ steps\.approval-context\.outputs\.base-sha \}\}', - 'HEAD_SHA: \$\{\{ steps\.approval-context\.outputs\.head-sha \}\}', - 'if: always\(\)', - 'checks: write', - '--arg name "SFL Reviewer Approval"', - 'head_sha: \$head_sha', - 'continue-on-error: true', - 'SFL_ENABLED: \$\{\{ vars\.SFL_ENABLED \}\}', - 'required reviewer gate fails closed', - 'COMMENT_COMMAND_RESULT: \$\{\{ needs\.comment-command\.result \}\}', - 'DISPATCH_RESULT: \$\{\{ needs\.dispatch\.result \}\}', - 'if \[ "\$EVENT_NAME" = "issue_comment" \].*\\', - '&& \[ "\$COMMENT_COMMAND_RESULT" != "success" \]', - 'SFL comment command job concluded \$\{COMMENT_COMMAND_RESULT\}; refusing to bootstrap', - 'if \[ "\$EVENT_NAME" != "issue_comment" \].*\\', - '&& \[ "\$DISPATCH_RESULT" != "success" \]', - 'SFL dispatch job concluded \$\{DISPATCH_RESULT\}; refusing to bootstrap', - 'name: Finalize head approval check', - 'WAIT_OUTCOME: \$\{\{ steps\.wait-review\.outcome \}\}', - 'validate_review_run' -) -Assert-PatternAbsent $auto 'normalize_login[^\r\n]+set-it-free-loop' -Assert-PatternAbsent $auto '\.draft // true' -Assert-PatternAbsent $auto 'has\("draft"\) then \.draft' - -$autoContent = Read-RepoFile $auto -$labelGuardCount = ([regex]::Matches( - $autoContent, - [regex]::Escape("github.event.action != 'labeled'") - )).Count -if ($labelGuardCount -ne 2) { - $failures.Add( - "Auto-review dispatch and approval jobs must both reject unrelated label events; found $labelGuardCount guards." - ) -} -if ($autoContent -match 'permission-pull-requests:\s*read') { - $failures.Add('Auto-review dispatcher cannot consume pull-request labels with a read-only pull-request token.') -} -if ($autoContent -match 'permission-issues:\s*write') { - $failures.Add('Auto-review dispatcher should not retain issue-wide write access when pull-request write covers its label mutations.') -} - -Assert-PatternSet $recovery @( - "vars.SFL_ENABLED != 'false'", - "github\.event\.workflow_run\.path == '\.github/workflows/sfl-pr-review\.lock\.yml'", - "github\.event\.workflow_run\.conclusion == 'cancelled'", - 'name: Finalize interrupted review evidence', - 'checks: write', - 'FAILED_RUN_TITLE: \$\{\{ github\.event\.workflow_run\.display_title \}\}', - 'sfl-review:\$\{pullNumber\}:\$\{baseSha\}:\$\{headSha\}:\$\{runId\}', - "check\.external_id === externalId && check\.app\?\.id === 15368", - "status: 'completed'", - "conclusion: 'failure'", - "if: github\.event\.workflow_run\.conclusion != 'cancelled'", - 'decide_review_recovery', - 'newer_run_suppresses_retry', - 'missing_data', - 'missing_tool', - 'report_incomplete', - '\.retry_count == 0 or \.retry_count == 1', - 'inputs\[retry_count\]=1', - 'suppressing stale retry', - 'produced no formal review after its one trusted retry', - 'Review agent output is unavailable; refusing an unproven retry', - 'Review agent output is malformed; refusing an unproven retry', - 'Last-moment interlock', - 'FINAL_PULL_REQUEST=', - 'FINAL_NEWER_RUN_ID=', - 'changed before recovery dispatch; suppressing stale retry' -) - -Assert-PatternSet 'deployment\scripts\deploy-workflow.ps1' @( - 'sfl-pr-review-auto', - 'sfl-pr-review-recovery', - 'Assert-HemSoftRepository', - "owner.login -ne 'HemSoft'", - 'Add-SflYamlSourcePin', - 'deployment/infrastructure/\$inf\.yml@\$CurrentSha' -) - -Assert-PatternSet 'deployment\scripts\set-sfl-review-gate.ps1' @( - "ValidatePattern\('\^HemSoft/", - "owner.login -ne 'HemSoft'", - "reviewContext = 'SFL Reviewer Approval'", - 'githubActionsAppId = 15368', - "activeLogin -ne 'HemSoft'", - 'strict = \$true', - 'protection/required_status_checks', - 'protectionExists', - 'enabling status checks without replacing existing branch protection', - 'Could not inspect full branch protection', - '--method PUT', - '--method PATCH', - 'preserving' -) - -Assert-PatternSet 'deployment\scripts\install-gh-sfl-hemsoft.ps1' @( - 'Join-Path \$repoRoot ''gh-sfl''', - 'gofmt -l \.', - 'go vet \./\.\.\.', - 'go test \./\.\.\.', - 'go build -trimpath -buildvcs=false', - 'GitHub CLI\\extensions\\gh-sfl' -) - -Assert-PatternSet 'gh-sfl\github.go' @( - 'outside the HemSoft repository scope', - 'GitHub CLI must be authenticated as HemSoft', - 'is protected and cannot be targeted' -) - -Assert-PatternSet 'gh-sfl\init.go' @( - '"reviewer": \{', - 'sfl-pr-review\.lock\.yml', - 'sfl-pr-review-auto\.yml', - 'sfl-pr-review-recovery\.yml', - 'opts\.tier = "reviewer"', - 'opts\.pr = true' -) - -Assert-PatternSet 'gh-sfl\sync.go' @( - 'opts := syncOptions\{pr: true\}', - 'resolveDeploymentRelease\(opts\.sourceRef\)', - 'sourceRef := release\.SHA' -) - -foreach ($manifestPath in @('deployment\sfl-manifest.schema.json', 'sfl.json')) { - Assert-PatternSet $manifestPath @('sfl-pr-review-auto', 'sfl-pr-review-recovery') -} - -Assert-PatternSet 'docs\SFL-REVIEWER.md' @( - 'HemSoft-owned repositories', - 'OPENROUTER_API_KEY', - 'retries exactly once', - 'SFL Reviewer Approval', - 'gh sfl gate --repo' -) - -$sensitivePatterns = @( - '(?im)^\s*(?:app-id|github-app-id|installation-id|client-id):\s*(?:\d+|Iv[A-Za-z0-9]+)\s*$', - '(?im)^\s*source:[ \t]+(?!HemSoft/set-it-free-loop(?:/|@))[^\r\n]*set-it-free-loop' -) -foreach ($path in @($reviewer, $auto, $recovery, 'docs\SFL-REVIEWER.md')) { - $content = Read-RepoFile $path - foreach ($pattern in $sensitivePatterns) { - if ($content -match $pattern) { - $failures.Add("$path contains prohibited organization-specific content: $pattern") - } +foreach ($required in @( + 'Workflows = @()', + 'Infrastructure = @("sfl-pr-review-auto")', + 'Components = @("sfl-pr-review-auto")', + 'Remove-Item -LiteralPath $retiredPath -Force', + 'gh pr edit $existingPrNumber', + '--body $prBody', + 'actionlint .github/workflows/sfl-pr-review-auto.yml' +)) { + if ($files.Deploy -notmatch [regex]::Escape($required)) { + throw "Review-tier deployment is missing contract text: $required" } } -if ($failures.Count -gt 0) { - $failures | ForEach-Object { Write-Error $_ } - exit 1 +foreach ($required in @( + 'issue_comment:', + 'pull_request_review:', + 'Reviewed commit:', + 'github.rest.repos.getCommit', + 'Codex artifact is stale for the current pull request head', + 'Codex reported review findings on the current head' +)) { + if ($files.Observer -notmatch [regex]::Escape($required)) { + throw "Observer is missing result contract text: $required" + } } -Write-Output 'SFL reviewer platform contract passed.' +Write-Output 'SFL Codex reviewer platform contract tests passed.' diff --git a/deployment/tests/test-sfl-verdict-validator.ps1 b/deployment/tests/test-sfl-verdict-validator.ps1 deleted file mode 100644 index 66b3f2de..00000000 --- a/deployment/tests/test-sfl-verdict-validator.ps1 +++ /dev/null @@ -1,68 +0,0 @@ -[CmdletBinding()] -param() - -Set-StrictMode -Version Latest -$ErrorActionPreference = 'Stop' - -$repoRoot = (Resolve-Path (Join-Path $PSScriptRoot '..\..')).ProviderPath -$workflowPath = Join-Path $repoRoot 'deployment\workflows\sfl-pr-review.md' -$workflow = Get-Content -LiteralPath $workflowPath -Raw -$failures = [System.Collections.Generic.List[string]]::new() - -$requiredPatterns = @( - 'Review body must contain exactly one recognized SFL verdict', - "\? 'APPROVE'\s*\r?\n\s*: 'COMMENT'", - "\? 'APPROVED'\s*\r?\n\s*: 'COMMENTED'", - 'Published review manifest does not match the immutable agent verdict', - 'Published finding count \$\{publishedFindingCount\} does not match immutable agent count', - 'const gateApproved =\s*\r?\n\s*verdictApproved &&\s*\r?\n\s*immutableFindingCount === 0 &&\s*\r?\n\s*unresolvedFindingCount === 0', - 'SFL_GATE_APPROVED: \$\{\{ steps\.finalize-review\.outputs\.gate-approved \}\}', - 'const baseMatchesExpected = pullRequest\.base\.sha === baseSha', - 'const headMatchesExpected = pullRequest\.head\.sha === headSha', - "initializedEvidence\.name !== 'SFL Review Evidence'", - 'initializedEvidence\.head_sha !== headSha', - 'initializedEvidence\.external_id !== gateExternalId', - 'initializedEvidence\.app\?\.id !== 15368', - "conclusion: approved \? 'success' : 'failure'", - 'if \(!baseMatchesExpected \|\| !headMatchesExpected\)', - 'if \(!gateApproved\)' -) -foreach ($pattern in $requiredPatterns) { - if ($workflow -notmatch $pattern) { - $failures.Add("Reviewer verdict contract is missing pattern: $pattern") - } -} - -function Get-ExpectedGateApproval( - [bool] $VerdictApproved, - [int] $NewFindingCount, - [int] $UnresolvedFindingCount -) { - return $VerdictApproved -and - $NewFindingCount -eq 0 -and - $UnresolvedFindingCount -eq 0 -} - -$cases = @( - @{ Name = 'zero findings approve'; Verdict = $true; New = 0; Open = 0; Expected = $true }, - @{ Name = 'needs work blocks'; Verdict = $false; New = 0; Open = 0; Expected = $false }, - @{ Name = 'new low finding blocks'; Verdict = $true; New = 1; Open = 0; Expected = $false }, - @{ Name = 'carried finding blocks'; Verdict = $true; New = 0; Open = 1; Expected = $false }, - @{ Name = 'new and carried findings block'; Verdict = $true; New = 2; Open = 3; Expected = $false } -) -foreach ($case in $cases) { - $actual = Get-ExpectedGateApproval ` - -VerdictApproved $case.Verdict ` - -NewFindingCount $case.New ` - -UnresolvedFindingCount $case.Open - if ($actual -ne $case.Expected) { - $failures.Add("Verdict case failed: $($case.Name)") - } -} - -if ($failures.Count -gt 0) { - $failures | ForEach-Object { Write-Error $_ } - throw "SFL verdict contract failed with $($failures.Count) finding(s)." -} - -Write-Output 'SFL verdict contract passed.' diff --git a/deployment/tests/test-source-pin-placement.ps1 b/deployment/tests/test-source-pin-placement.ps1 index 01b94b08..7eec9221 100644 --- a/deployment/tests/test-source-pin-placement.ps1 +++ b/deployment/tests/test-source-pin-placement.ps1 @@ -62,20 +62,47 @@ if ($upgraded -match '(?m)^# (?:Deployed from|To upgrade):' -or $yamlWorkflow = @' # HemSoft SFL reviewer platform v2 +# Source: HemSoft/set-it-free-loop/deployment/infrastructure/sfl-pr-review-auto.yml@main name: SFL PR Review Auto Trigger on: + push: + branches: [main] pull_request_target: +env: + SFL_REVIEW_BASE_BRANCH: main '@ $yamlRef = 'HemSoft/set-it-free-loop/deployment/infrastructure/sfl-pr-review-auto.yml@bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb' -$yamlPinned = Add-SflYamlSourcePin -Content $yamlWorkflow -SourceRef $yamlRef +$yamlPinned = Add-SflYamlSourcePin -Content $yamlWorkflow -SourceRef $yamlRef -DefaultBranch 'trunk' if ($yamlPinned -notmatch "\A# Deployed from: $([regex]::Escape($yamlRef))\r?\n# To upgrade:") { throw 'YAML source pin was not placed at the beginning of the workflow.' } +if ($yamlPinned -match '@main' -or + ([regex]::Matches($yamlPinned, [regex]::Escape($yamlRef)).Count -ne 2)) { + throw 'YAML provenance was not rewritten to the immutable source SHA.' +} +if ($yamlPinned -notmatch [regex]::Escape("branches: ['trunk']") -or + $yamlPinned -match [regex]::Escape('branches: [main]') -or + $yamlPinned -notmatch [regex]::Escape("SFL_REVIEW_BASE_BRANCH: 'trunk'")) { + throw 'YAML push trigger was not restricted to the target default branch.' +} + +$yamlPinnedFromCrLf = Add-SflYamlSourcePin ` + -Content $yamlWorkflow.Replace("`n", "`r`n") ` + -SourceRef $yamlRef ` + -DefaultBranch 'trunk' +if ($yamlPinnedFromCrLf -notmatch [regex]::Escape("branches: ['trunk']") -or + $yamlPinnedFromCrLf -notmatch [regex]::Escape("SFL_REVIEW_BASE_BRANCH: 'trunk'") -or + $yamlPinnedFromCrLf -match "`r") { + throw 'CRLF YAML was not normalized and rewritten for the target default branch.' +} $yamlUpdatedRef = 'HemSoft/set-it-free-loop/deployment/infrastructure/sfl-pr-review-auto.yml@cccccccccccccccccccccccccccccccccccccccc' -$yamlRepinned = Add-SflYamlSourcePin -Content $yamlPinned -SourceRef $yamlUpdatedRef +$yamlRepinned = Add-SflYamlSourcePin -Content $yamlPinned -SourceRef $yamlUpdatedRef -DefaultBranch 'release/next' if (([regex]::Matches($yamlRepinned, '(?m)^# Deployed from:').Count -ne 1) -or - $yamlRepinned -notmatch [regex]::Escape($yamlUpdatedRef)) { + ([regex]::Matches($yamlRepinned, [regex]::Escape($yamlUpdatedRef)).Count -ne 2) -or + $yamlRepinned -match [regex]::Escape($yamlRef) -or + $yamlRepinned -notmatch [regex]::Escape("branches: ['release/next']") -or + $yamlRepinned -notmatch [regex]::Escape("SFL_REVIEW_BASE_BRANCH: 'release/next'")) { throw 'Existing YAML source pin was not replaced idempotently.' } diff --git a/deployment/workflows/sfl-pr-review.md b/deployment/workflows/sfl-pr-review.md deleted file mode 100644 index 8cdcca85..00000000 --- a/deployment/workflows/sfl-pr-review.md +++ /dev/null @@ -1,1648 +0,0 @@ ---- -# HemSoft SFL reviewer platform v2 -name: SFL PR Review -description: | - Full-spectrum PR review dispatched automatically by the SFL PR review wrapper - or manually through workflow_dispatch. Performs 3 evidence-based review - passes (Security, Accuracy & Reliability, Quality & Maintainability) and - posts inline review comments on specific lines with an overall summary. -source: HemSoft/set-it-free-loop/deployment/workflows/sfl-pr-review.md@main - -on: - workflow_dispatch: - inputs: - aw_context: - description: gh-aw pull request context JSON - required: true - type: string - item_number: - description: Pull request number to review - required: true - type: string - base_sha: - description: Expected pull request base commit - required: true - type: string - head_sha: - description: Expected pull request head commit - required: true - type: string - dispatch_id: - description: Unique wrapper or recovery dispatch identifier - required: false - default: manual - type: string - retry_count: - description: Missing-review-output retry counter - required: false - default: "0" - type: string - review_effort: - description: Audit effort marker retained in provenance and recovery - required: false - default: low - type: choice - options: - - low - - medium - - high - -run-name: "SFL PR Review #${{ inputs.item_number }} ${{ inputs.base_sha }}:${{ inputs.head_sha }} retry=${{ inputs.retry_count }} dispatch=${{ inputs.dispatch_id }}" - -concurrency: - group: sfl-pr-review-${{ inputs.item_number }} - cancel-in-progress: true - -permissions: - contents: read - checks: read - issues: read - pull-requests: read - -env: - SFL_REVIEW_EFFORT: ${{ inputs.review_effort || 'low' }} - -pre-steps: - - name: Validate trusted review context - env: - AW_CONTEXT: ${{ inputs.aw_context }} - BASE_SHA: ${{ inputs.base_sha }} - DISPATCH_ID: ${{ inputs.dispatch_id }} - HEAD_SHA: ${{ inputs.head_sha }} - ITEM_NUMBER: ${{ inputs.item_number }} - run: | - set -euo pipefail - if ! [[ "$ITEM_NUMBER" =~ ^[1-9][0-9]*$ ]]; then - echo "::error::Invalid pull request number" - exit 1 - fi - if ! [[ "$BASE_SHA" =~ ^[0-9a-f]{40}$ ]] \ - || ! [[ "$HEAD_SHA" =~ ^[0-9a-f]{40}$ ]]; then - echo "::error::Invalid pull request revision" - exit 1 - fi - if ! [[ "$DISPATCH_ID" =~ ^[A-Za-z0-9._-]{1,64}$ ]]; then - echo "::error::Invalid dispatch identifier" - exit 1 - fi - if ! jq -e \ - --argjson item_number "$ITEM_NUMBER" \ - --arg base_sha "$BASE_SHA" \ - --arg head_sha "$HEAD_SHA" ' - . == { - item_type: "pull_request", - item_number: $item_number, - base_sha: $base_sha, - head_sha: $head_sha - } - ' <<< "$AW_CONTEXT" > /dev/null; then - echo "::error::Untrusted or inconsistent review context" - exit 1 - fi - - - name: Install ripgrep with bounded diagnostics - id: sfl-ripgrep - shell: bash - run: | - set -euo pipefail - log_path="${RUNNER_TEMP}/sfl-ripgrep-install.log" - : > "$log_path" - if command -v rg > /dev/null 2>&1; then - rg --version | tee -a "$log_path" - exit 0 - fi - - set +e - timeout --signal=TERM --kill-after=15s 180s bash -c ' - set -euo pipefail - sudo apt-get update \ - -o Acquire::Retries=3 \ - -o Acquire::http::Timeout=30 \ - -o Acquire::https::Timeout=30 - sudo apt-get install --no-install-recommends -y ripgrep - ' 2>&1 | tee -a "$log_path" - install_status=${PIPESTATUS[0]} - set -e - - if [ "$install_status" -eq 124 ] || [ "$install_status" -eq 137 ]; then - echo "::error::ripgrep installation exceeded the 180-second setup limit" - exit 1 - fi - if [ "$install_status" -ne 0 ]; then - echo "::error::ripgrep installation failed with exit code ${install_status}" - exit "$install_status" - fi - if ! command -v rg > /dev/null 2>&1; then - echo "::error::ripgrep installation completed without making rg available" - exit 1 - fi - rg --version | tee -a "$log_path" - - - name: Retain ripgrep setup diagnostics - if: always() - uses: actions/upload-artifact@043fb46d1a93c77aae656e7c1c64a875d1fc6a0a # v7.0.1 - with: - name: sfl-ripgrep-setup-${{ github.run_id }}-${{ github.run_attempt }} - path: ${{ runner.temp }}/sfl-ripgrep-install.log - if-no-files-found: ignore - retention-days: 1 - -# HemSoft runs the reviewer through Kimi K3 on its private OpenRouter route. - -models: - providers: - github-copilot: - models: - "moonshotai/kimi-k3": - cost: - input: "3e-06" - output: "1.5e-05" - -max-turn-cache-misses: 10 - -engine: - id: copilot - env: - COPILOT_MODEL: moonshotai/kimi-k3 - COPILOT_PROVIDER_API_KEY: ${{ secrets.OPENROUTER_API_KEY }} - COPILOT_PROVIDER_BASE_URL: https://openrouter.ai/api/v1 - COPILOT_PROVIDER_TYPE: openai - COPILOT_PROVIDER_WIRE_API: responses - -model: moonshotai/kimi-k3 - -network: - allowed: - - openrouter.ai - -tools: - github: - lockdown: false - bash: true - -safe-outputs: - threat-detection: - continue-on-error: false - steps: - - name: Install threat-detection ripgrep with bounded diagnostics - id: sfl-threat-detection-ripgrep - if: always() - shell: bash - run: | - set -euo pipefail - log_path="${RUNNER_TEMP}/sfl-threat-detection-ripgrep-install.log" - : > "$log_path" - if command -v rg > /dev/null 2>&1; then - rg --version | tee -a "$log_path" - exit 0 - fi - - set +e - timeout --signal=TERM --kill-after=15s 180s bash -c ' - set -euo pipefail - sudo apt-get update \ - -o Acquire::Retries=3 \ - -o Acquire::http::Timeout=30 \ - -o Acquire::https::Timeout=30 - sudo apt-get install --no-install-recommends -y ripgrep - ' 2>&1 | tee -a "$log_path" - install_status=${PIPESTATUS[0]} - set -e - - if [ "$install_status" -eq 124 ] || [ "$install_status" -eq 137 ]; then - echo "::error::threat-detection ripgrep installation exceeded the 180-second setup limit" - exit 1 - fi - if [ "$install_status" -ne 0 ]; then - echo "::error::threat-detection ripgrep installation failed with exit code ${install_status}" - exit "$install_status" - fi - if ! command -v rg > /dev/null 2>&1; then - echo "::error::threat-detection ripgrep installation completed without making rg available" - exit 1 - fi - rg --version | tee -a "$log_path" - - name: Retain threat-detection ripgrep setup diagnostics - if: always() - uses: actions/upload-artifact@043fb46d1a93c77aae656e7c1c64a875d1fc6a0a # v7.0.1 - with: - name: sfl-threat-detection-ripgrep-setup-${{ github.run_id }}-${{ github.run_attempt }} - path: ${{ runner.temp }}/sfl-threat-detection-ripgrep-install.log - if-no-files-found: ignore - retention-days: 1 - post-steps: - - name: Generate SFL App token for final head verification - id: sfl-final-head-token - uses: actions/create-github-app-token@bcd2ba49218906704ab6c1aa796996da409d3eb1 # v3.2.0 - with: - client-id: ${{ vars.SFL_APP_CLIENT_ID }} - private-key: ${{ secrets.SFL_APP_PRIVATE_KEY }} - owner: ${{ github.repository_owner }} - repositories: ${{ github.event.repository.name }} - permission-pull-requests: read - - name: Verify pull request base and head before safe outputs - env: - EXPECTED_BASE: ${{ inputs.base_sha }} - EXPECTED_HEAD: ${{ inputs.head_sha }} - GH_TOKEN: ${{ steps.sfl-final-head-token.outputs.token }} - PR_NUMBER: ${{ inputs.item_number }} - REPOSITORY: ${{ github.repository }} - run: | - set -euo pipefail - if [[ ! "${EXPECTED_HEAD}" =~ ^[0-9a-f]{40}$ ]]; then - echo "::error::Invalid expected pull request head: ${EXPECTED_HEAD}" - exit 1 - fi - if [[ ! "${EXPECTED_BASE}" =~ ^[0-9a-f]{40}$ ]]; then - echo "::error::Invalid expected pull request base: ${EXPECTED_BASE}" - exit 1 - fi - pull_request="$(gh api "repos/${REPOSITORY}/pulls/${PR_NUMBER}")" - live_head="$(jq -r '.head.sha' <<< "$pull_request")" - live_base="$(jq -r '.base.sha' <<< "$pull_request")" - if [[ "${live_head}" != "${EXPECTED_HEAD}" ]]; then - echo "::error::Pull request head changed from ${EXPECTED_HEAD} to ${live_head}; suppressing stale SFL outputs" - exit 1 - fi - if [[ "${live_base}" != "${EXPECTED_BASE}" ]]; then - echo "::error::Pull request base changed from ${EXPECTED_BASE} to ${live_base}; suppressing stale SFL outputs" - exit 1 - fi - github-app: - client-id: ${{ vars.SFL_APP_CLIENT_ID }} - private-key: ${{ secrets.SFL_APP_PRIVATE_KEY }} - create-pull-request-review-comment: - commit-id: "${{ inputs.head_sha }}" - max: 30 - target: "${{ inputs.item_number }}" - submit-pull-request-review: - commit-id: "${{ inputs.head_sha }}" - max: 1 - target: "${{ inputs.item_number }}" - jobs: - resolve-sfl-review-thread: - description: Resolve one obsolete SFL finding thread after deterministic validation - runs-on: ubuntu-slim - needs: safe_outputs - if: needs.safe_outputs.result == 'success' - permissions: - contents: write - pull-requests: write - inputs: - thread_id: - description: GraphQL node ID of the obsolete SFL review thread - required: true - type: string - steps: - - name: Generate SFL App token for thread validation - id: validation-token - uses: actions/create-github-app-token@bcd2ba49218906704ab6c1aa796996da409d3eb1 # v3.2.0 - with: - client-id: ${{ vars.SFL_APP_CLIENT_ID }} - private-key: ${{ secrets.SFL_APP_PRIVATE_KEY }} - owner: ${{ github.repository_owner }} - repositories: ${{ github.event.repository.name }} - permission-contents: read - permission-pull-requests: read - - name: Resolve SFL App identity - id: app-identity - uses: actions/github-script@3a2844b7e9c422d3c10d287c895573f7108da1b3 # v9.0.0 - with: - github-token: ${{ steps.validation-token.outputs.token }} - script: | - const data = await github.graphql(`query { viewer { login } }`); - const login = (data.viewer?.login || '') - .replace(/\[bot\]$/i, '') - .toLowerCase(); - if (!login) { - core.setFailed('Could not resolve the SFL App identity'); - return; - } - core.setOutput('login', login); - - name: Validate and resolve requested SFL threads - uses: actions/github-script@3a2844b7e9c422d3c10d287c895573f7108da1b3 # v9.0.0 - env: - SFL_APP_LOGIN: ${{ steps.app-identity.outputs.login }} - SFL_EXPECTED_BASE: ${{ inputs.base_sha }} - SFL_EXPECTED_HEAD: ${{ inputs.head_sha }} - SFL_PR_NUMBER: ${{ inputs.item_number }} - with: - github-token: ${{ github.token }} - script: | - const fs = require('fs'); - const outputPath = process.env.GH_AW_AGENT_OUTPUT; - const expectedBase = process.env.SFL_EXPECTED_BASE; - const expectedHead = process.env.SFL_EXPECTED_HEAD; - const pullNumber = Number(process.env.SFL_PR_NUMBER); - if (!outputPath || !fs.existsSync(outputPath)) { - core.setFailed('SFL agent output is unavailable for thread resolution'); - return; - } - if (!/^[0-9a-f]{40}$/.test(expectedHead || '')) { - core.setFailed(`Invalid expected pull request head: ${expectedHead || 'missing'}`); - return; - } - if (!/^[0-9a-f]{40}$/.test(expectedBase || '')) { - core.setFailed(`Invalid expected pull request base: ${expectedBase || 'missing'}`); - return; - } - if (!Number.isInteger(pullNumber) || pullNumber <= 0) { - core.setFailed(`Invalid pull request number: ${process.env.SFL_PR_NUMBER}`); - return; - } - - const output = JSON.parse(fs.readFileSync(outputPath, 'utf8')); - const requests = (Array.isArray(output.items) ? output.items : []) - .filter(item => item.type === 'resolve_sfl_review_thread'); - const threadIds = [...new Set(requests.map(item => item.thread_id))]; - if (threadIds.length === 0) { - core.info('No obsolete SFL review threads requested for resolution'); - return; - } - if (threadIds.length > 30) { - core.setFailed(`Requested ${threadIds.length} thread resolutions; maximum is 30`); - return; - } - - const normalizeActorLogin = login => - (login || '').replace(/\[bot\]$/i, '').toLowerCase(); - const appLogin = normalizeActorLogin(process.env.SFL_APP_LOGIN); - if (!appLogin) { - core.setFailed('Could not resolve the SFL App identity'); - return; - } - const severityPrefix = - /^(🔴 \*\*CRITICAL|🟠 \*\*HIGH|🟡 \*\*MEDIUM|⚪ \*\*LOW) —/; - const unresolvedOutdatedThreadIds = []; - - for (const threadId of threadIds) { - if (typeof threadId !== 'string' || !threadId.startsWith('PRRT_')) { - core.setFailed(`Invalid pull request review thread ID: ${threadId}`); - return; - } - const data = await github.graphql( - `query($threadId: ID!) { - node(id: $threadId) { - ... on PullRequestReviewThread { - id - isOutdated - isResolved - pullRequest { - number - baseRefOid - headRefOid - repository { - nameWithOwner - } - } - comments(first: 1) { - nodes { - author { - login - __typename - } - body - path - } - } - } - } - }`, - { threadId } - ); - const thread = data.node; - const rootComment = thread?.comments?.nodes?.[0]; - const body = rootComment?.body || ''; - const path = rootComment?.path; - if ( - !thread || - thread.pullRequest?.repository?.nameWithOwner !== - `${context.repo.owner}/${context.repo.repo}` || - thread.pullRequest?.number !== pullNumber || - thread.pullRequest?.baseRefOid !== expectedBase || - thread.pullRequest?.headRefOid !== expectedHead || - rootComment?.author?.__typename !== 'Bot' || - normalizeActorLogin(rootComment.author.login) !== appLogin || - !severityPrefix.test(body) || - !body.includes('SFL Reviewer') || - typeof path !== 'string' || - path.length === 0 || - (!thread.isResolved && !thread.isOutdated) - ) { - core.setFailed( - `Thread ${threadId} is not an obsolete SFL finding on PR #${pullNumber} at base ${expectedBase} and head ${expectedHead}` - ); - return; - } - if (thread.isResolved) { - core.info(`SFL review thread ${threadId} is already resolved`); - continue; - } - try { - const { data: pathAtHead } = await github.rest.repos.getContent({ - owner: context.repo.owner, - repo: context.repo.repo, - path, - ref: expectedHead, - }); - if (Array.isArray(pathAtHead) || pathAtHead.type !== 'file') { - core.setFailed( - `Thread ${threadId} path ${path} is not a file at head ${expectedHead}` - ); - return; - } - } catch (error) { - if (error.status === 404) { - core.setFailed( - `Thread ${threadId} path ${path} no longer exists at head ${expectedHead}; human resolution is required` - ); - return; - } - throw error; - } - unresolvedOutdatedThreadIds.push(threadId); - } - - const failedThreadIds = []; - for (const threadId of unresolvedOutdatedThreadIds) { - let resolved = false; - let lastError; - for (let attempt = 1; attempt <= 3; attempt += 1) { - try { - const result = await github.graphql( - `mutation($threadId: ID!) { - resolveReviewThread(input: { threadId: $threadId }) { - thread { - id - isResolved - } - } - }`, - { threadId } - ); - if (result.resolveReviewThread?.thread?.isResolved) { - resolved = true; - break; - } - lastError = new Error('mutation did not return a resolved thread'); - } catch (error) { - lastError = error; - } - if (attempt < 3) { - await new Promise(resolve => setTimeout(resolve, attempt * 1000)); - } - } - if (!resolved) { - core.error( - `Failed to resolve SFL review thread ${threadId}: ${lastError?.message || 'unknown error'}` - ); - failedThreadIds.push(threadId); - continue; - } - core.info(`Resolved obsolete SFL review thread ${threadId}`); - } - if (failedThreadIds.length > 0) { - core.setFailed( - `Failed to resolve ${failedThreadIds.length} obsolete SFL review thread(s)` - ); - } - -jobs: - publish_review_provenance: - if: always() - runs-on: ubuntu-slim - outputs: - evidence-check-run-id: ${{ steps.initialize-evidence.outputs.check-run-id }} - permissions: - checks: write - contents: read - pull-requests: read - steps: - - name: Write immutable review dispatch provenance - env: - BASE_SHA: ${{ inputs.base_sha }} - HEAD_SHA: ${{ inputs.head_sha }} - PR_NUMBER: ${{ inputs.item_number }} - PROVENANCE_PATH: ${{ runner.temp }}/sfl-review-run-provenance.json - REPOSITORY: ${{ github.repository }} - REVIEW_EFFORT: ${{ env.SFL_REVIEW_EFFORT }} - RETRY_COUNT: ${{ inputs.retry_count }} - RUN_ID: ${{ github.run_id }} - run: | - set -euo pipefail - if ! [[ "$PR_NUMBER" =~ ^[1-9][0-9]*$ ]]; then - echo "::error::Invalid pull request number: ${PR_NUMBER}" - exit 1 - fi - if ! [[ "$HEAD_SHA" =~ ^[0-9a-f]{40}$ ]]; then - echo "::error::Invalid expected pull request head: ${HEAD_SHA}" - exit 1 - fi - if ! [[ "$BASE_SHA" =~ ^[0-9a-f]{40}$ ]]; then - echo "::error::Invalid expected pull request base: ${BASE_SHA}" - exit 1 - fi - if ! [[ "$RETRY_COUNT" =~ ^[01]$ ]]; then - echo "::error::Invalid review retry count: ${RETRY_COUNT}" - exit 1 - fi - case "$REVIEW_EFFORT" in - low|medium|high) ;; - *) - echo "::error::Invalid review effort: ${REVIEW_EFFORT}" - exit 1 - ;; - esac - jq -n \ - --arg repository "$REPOSITORY" \ - --arg workflow_run_id "$RUN_ID" \ - --argjson pr_number "$PR_NUMBER" \ - --arg base_sha "$BASE_SHA" \ - --arg head_sha "$HEAD_SHA" \ - --arg review_effort "$REVIEW_EFFORT" \ - --argjson retry_count "$RETRY_COUNT" \ - '{ - repository: $repository, - workflow_run_id: $workflow_run_id, - pr_number: $pr_number, - base_sha: $base_sha, - head_sha: $head_sha, - review_effort: $review_effort, - retry_count: $retry_count - }' > "$PROVENANCE_PATH" - - name: Initialize review evidence - id: initialize-evidence - uses: actions/github-script@3a2844b7e9c422d3c10d287c895573f7108da1b3 # v9.0.0 - env: - SFL_BASE_SHA: ${{ inputs.base_sha }} - SFL_HEAD_SHA: ${{ inputs.head_sha }} - SFL_PR_NUMBER: ${{ inputs.item_number }} - SFL_RUN_ID: ${{ github.run_id }} - with: - github-token: ${{ github.token }} - script: | - const baseSha = process.env.SFL_BASE_SHA; - const headSha = process.env.SFL_HEAD_SHA; - const pullNumber = Number(process.env.SFL_PR_NUMBER); - const runId = Number(process.env.SFL_RUN_ID); - if (!Number.isSafeInteger(runId) || runId <= 0) { - core.setFailed(`Invalid workflow run ID: ${process.env.SFL_RUN_ID}`); - return; - } - const { data: pullRequest } = await github.rest.pulls.get({ - owner: context.repo.owner, - repo: context.repo.repo, - pull_number: pullNumber, - }); - if ( - pullRequest.base.sha !== baseSha || - pullRequest.head.sha !== headSha - ) { - core.setFailed( - `Pull request changed from base ${baseSha} / head ${headSha} ` + - `to base ${pullRequest.base.sha} / head ${pullRequest.head.sha}` - ); - return; - } - const { data: evidence } = await github.rest.checks.create({ - owner: context.repo.owner, - repo: context.repo.repo, - name: 'SFL Review Evidence', - head_sha: headSha, - external_id: - `sfl-review:${pullNumber}:${baseSha}:${headSha}:${runId}`, - status: 'in_progress', - started_at: new Date().toISOString(), - output: { - title: 'SFL review is in progress', - summary: 'The immutable pull request state is being reviewed.', - text: 'replacement_retry_eligible=false', - }, - }); - core.setOutput('check-run-id', String(evidence.id)); - - name: Upload immutable review dispatch provenance - if: always() - uses: actions/upload-artifact@043fb46d1a93c77aae656e7c1c64a875d1fc6a0a # v7.0.1 - with: - name: sfl-review-run-provenance - path: ${{ runner.temp }}/sfl-review-run-provenance.json - if-no-files-found: error - retention-days: 1 - - review_metadata: - needs: [agent, publish_review_provenance, safe_outputs, resolve_sfl_review_thread] - if: always() - runs-on: ubuntu-slim - permissions: - checks: write - contents: read - pull-requests: read - steps: - - name: Verify expected pull request head - id: resolve-head - uses: actions/github-script@3a2844b7e9c422d3c10d287c895573f7108da1b3 # v9.0.0 - env: - SFL_EXPECTED_BASE: ${{ inputs.base_sha }} - SFL_EXPECTED_HEAD: ${{ inputs.head_sha }} - SFL_PR_NUMBER: ${{ inputs.item_number }} - with: - github-token: ${{ github.token }} - script: | - const pullNumber = Number(process.env.SFL_PR_NUMBER); - const expectedBase = process.env.SFL_EXPECTED_BASE; - const expectedHead = process.env.SFL_EXPECTED_HEAD; - if (!Number.isInteger(pullNumber) || pullNumber <= 0) { - core.setFailed(`Invalid pull request number: ${process.env.SFL_PR_NUMBER}`); - return; - } - if (!/^[0-9a-f]{40}$/.test(expectedHead || '')) { - core.setFailed(`Invalid expected pull request head: ${expectedHead || 'missing'}`); - return; - } - if (!/^[0-9a-f]{40}$/.test(expectedBase || '')) { - core.setFailed(`Invalid expected pull request base: ${expectedBase || 'missing'}`); - return; - } - - const { data: pullRequest } = await github.rest.pulls.get({ - owner: context.repo.owner, - repo: context.repo.repo, - pull_number: pullNumber, - }); - core.setOutput('head-sha', expectedHead); - if ( - pullRequest.base.sha !== expectedBase || - pullRequest.head.sha !== expectedHead - ) { - core.setFailed( - `Pull request changed from base ${expectedBase} / head ${expectedHead} to base ${pullRequest.base.sha} / head ${pullRequest.head.sha}` - ); - } - - - name: Download safe output items - if: needs.agent.result == 'success' && needs.safe_outputs.result == 'success' && (needs.resolve_sfl_review_thread.result == 'success' || needs.resolve_sfl_review_thread.result == 'skipped') - uses: actions/download-artifact@3e5f45b2cfb9172054b4087a40e8e0b5a5461e7c # v8.0.1 - with: - name: safe-outputs-items - path: /tmp/sfl-review-safe-outputs - - - name: Download reviewer agent output - if: needs.agent.result == 'success' && needs.safe_outputs.result == 'success' && (needs.resolve_sfl_review_thread.result == 'success' || needs.resolve_sfl_review_thread.result == 'skipped') - uses: actions/download-artifact@3e5f45b2cfb9172054b4087a40e8e0b5a5461e7c # v8.0.1 - with: - name: agent - path: /tmp/gh-aw - - - name: Generate GitHub App token - if: always() - id: app-token - uses: actions/create-github-app-token@bcd2ba49218906704ab6c1aa796996da409d3eb1 # v3.2.0 - with: - client-id: ${{ vars.SFL_APP_CLIENT_ID }} - private-key: ${{ secrets.SFL_APP_PRIVATE_KEY }} - owner: ${{ github.repository_owner }} - repositories: ${{ github.event.repository.name }} - permission-actions: read - permission-contents: read - permission-pull-requests: write - - name: Finalize review metadata - if: needs.agent.result == 'success' && needs.safe_outputs.result == 'success' && (needs.resolve_sfl_review_thread.result == 'success' || needs.resolve_sfl_review_thread.result == 'skipped') - id: finalize-review - uses: actions/github-script@3a2844b7e9c422d3c10d287c895573f7108da1b3 # v9.0.0 - env: - SFL_AIC: ${{ needs.agent.outputs.aic }} - SFL_EFFORT: ${{ env.SFL_REVIEW_EFFORT }} - SFL_EXPECTED_BASE: ${{ inputs.base_sha }} - SFL_EXPECTED_HEAD: ${{ inputs.head_sha }} - SFL_AGENT_OUTPUT: /tmp/gh-aw/agent_output.json - SFL_MODEL: ${{ needs.agent.outputs.model }} - SFL_PR_NUMBER: ${{ github.event.pull_request.number || inputs.item_number }} - SFL_RUN_ID: ${{ github.run_id }} - SFL_SAFE_OUTPUT_ITEMS: /tmp/sfl-review-safe-outputs/safe-output-items.jsonl - with: - github-token: ${{ steps.app-token.outputs.token }} - script: | - // Keep these helpers inline so the HemSoft deployer can install a - // self-contained workflow without additional script dependencies. - function formatElapsed(startedAt, submittedAt) { - if (!Number.isFinite(startedAt) || !Number.isFinite(submittedAt)) { - return 'Unavailable'; - } - const elapsedSeconds = Math.max(0, Math.round((submittedAt - startedAt) / 1000)); - const hours = Math.floor(elapsedSeconds / 3600); - const minutes = Math.floor((elapsedSeconds % 3600) / 60); - const seconds = elapsedSeconds % 60; - return hours > 0 - ? `${hours}h ${minutes}m ${seconds}s` - : `${minutes}m ${seconds}s`; - } - - function formatModel(modelId) { - const [baseModel, query = ''] = modelId.split('?', 2); - const effort = new URLSearchParams(query).get('effort') || - (process.env.SFL_EFFORT || '').trim(); - const claude = baseModel.match(/^claude-(opus|sonnet|haiku)-(.+)$/); - let displayName = baseModel; - if (claude) { - displayName = `${claude[1][0].toUpperCase()}${claude[1].slice(1)} ${claude[2]}`; - } else if (baseModel.startsWith('gpt-')) { - const parts = baseModel.slice(4).split('-'); - displayName = `GPT-${parts.shift()}${parts.length ? ` ${parts.map(part => part[0].toUpperCase() + part.slice(1)).join(' ')}` : ''}`; - } - return effort - ? `${displayName} (${effort[0].toUpperCase()}${effort.slice(1)} effort)` - : displayName; - } - - function formatAic(rawAic) { - const value = rawAic.trim() === '' ? Number.NaN : Number(rawAic); - return Number.isFinite(value) - ? value.toFixed(1).replace(/\.0$/, '') - : 'Unavailable'; - } - - async function countUnresolvedSflFindings({ - owner, - repo, - pullNumber, - reviewAuthor, - }) { - const normalizeActorLogin = login => - (login || '').replace(/\[bot\]$/i, '').toLowerCase(); - const severityPrefix = - /^(🔴 \*\*CRITICAL|🟠 \*\*HIGH|🟡 \*\*MEDIUM|⚪ \*\*LOW) —/; - let cursor = null; - let unresolvedCount = 0; - do { - const data = await github.graphql( - `query($owner: String!, $repo: String!, $pullNumber: Int!, $cursor: String) { - repository(owner: $owner, name: $repo) { - pullRequest(number: $pullNumber) { - reviewThreads(first: 100, after: $cursor) { - nodes { - isResolved - comments(first: 1) { - nodes { - author { - login - __typename - } - body - } - } - } - pageInfo { - hasNextPage - endCursor - } - } - } - } - }`, - { owner, repo, pullNumber, cursor } - ); - const threads = data.repository?.pullRequest?.reviewThreads; - if (!threads) { - throw new Error('Pull request review threads were unavailable'); - } - for (const thread of threads.nodes || []) { - const rootComment = thread.comments?.nodes?.[0]; - const body = rootComment?.body || ''; - if ( - !thread.isResolved && - rootComment?.author?.__typename === 'Bot' && - normalizeActorLogin(rootComment.author.login) === - normalizeActorLogin(reviewAuthor) && - severityPrefix.test(body) && - body.includes('SFL Reviewer') - ) { - unresolvedCount += 1; - } - } - cursor = threads.pageInfo.hasNextPage - ? threads.pageInfo.endCursor - : null; - } while (cursor); - return unresolvedCount; - } - - const owner = context.repo.owner; - const repo = context.repo.repo; - const pullNumber = Number(process.env.SFL_PR_NUMBER); - const runId = Number(process.env.SFL_RUN_ID); - const expectedBase = process.env.SFL_EXPECTED_BASE; - const expectedHead = process.env.SFL_EXPECTED_HEAD; - if (!Number.isInteger(pullNumber) || pullNumber <= 0) { - core.setFailed(`Invalid pull request number: ${process.env.SFL_PR_NUMBER}`); - return; - } - if (!Number.isInteger(runId) || runId <= 0) { - core.setFailed(`Invalid workflow run ID: ${process.env.SFL_RUN_ID}`); - return; - } - if (!/^[0-9a-f]{40}$/.test(expectedHead || '')) { - core.setFailed(`Invalid expected pull request head: ${expectedHead || 'missing'}`); - return; - } - if (!/^[0-9a-f]{40}$/.test(expectedBase || '')) { - core.setFailed(`Invalid expected pull request base: ${expectedBase || 'missing'}`); - return; - } - - const { data: pullRequest } = await github.rest.pulls.get({ - owner, - repo, - pull_number: pullNumber, - }); - if ( - pullRequest.base.sha !== expectedBase || - pullRequest.head.sha !== expectedHead - ) { - core.setFailed( - `Pull request changed from base ${expectedBase} / head ${expectedHead} to base ${pullRequest.base.sha} / head ${pullRequest.head.sha}` - ); - return; - } - - const fs = require('fs'); - const safeOutputItemsPath = process.env.SFL_SAFE_OUTPUT_ITEMS; - let safeOutputItems; - try { - safeOutputItems = fs - .readFileSync(safeOutputItemsPath, 'utf8') - .split(/\r?\n/) - .filter(Boolean) - .map(line => JSON.parse(line)); - } catch (error) { - core.setFailed( - `Could not read safe-output review metadata: ${error.message}` - ); - return; - } - let agentItems = []; - let replacementRetryEligible = false; - try { - const agentOutput = JSON.parse( - fs.readFileSync(process.env.SFL_AGENT_OUTPUT, 'utf8') - ); - agentItems = Array.isArray(agentOutput) - ? agentOutput - : (agentOutput.items || []); - if (!Array.isArray(agentItems)) { - throw new Error('agent items are not an array'); - } - replacementRetryEligible = !agentItems.some(item => - item.type === 'missing_data' || - item.type === 'missing_tool' || - item.type === 'report_incomplete' - ); - } catch (error) { - core.warning( - `Could not inspect terminal review signals; suppressing replacement: ${error.message}` - ); - } - core.setOutput( - 'replacement-retry-eligible', - replacementRetryEligible - ); - const agentSubmittedReviews = agentItems.filter( - item => item.type === 'submit_pull_request_review' - ); - if (agentSubmittedReviews.length !== 1) { - core.setFailed( - `Expected exactly one immutable agent submitted-review item, found ${agentSubmittedReviews.length}` - ); - return; - } - const [agentSubmittedReview] = agentSubmittedReviews; - // gh-aw preserves the agent's raw tool arguments; on a - // fixed-target workflow an omitted repo/pull_request_number means - // the configured pull request. Default omitted targets while - // still rejecting explicitly mismatched ones. - const configuredRepo = `${owner}/${repo}`; - if ( - (agentSubmittedReview.repo ?? configuredRepo) !== configuredRepo || - Number(agentSubmittedReview.pull_request_number ?? pullNumber) !== pullNumber - ) { - core.setFailed('Immutable agent submitted-review target does not match this pull request'); - return; - } - const severityPrefix = - /^(🔴 \*\*CRITICAL|🟠 \*\*HIGH|🟡 \*\*MEDIUM|⚪ \*\*LOW) —/; - const agentReviewComments = agentItems.filter( - item => item.type === 'create_pull_request_review_comment' - ); - if (agentReviewComments.some(item => - (item.repo ?? configuredRepo) !== configuredRepo || - Number(item.pull_request_number ?? pullNumber) !== pullNumber || - !severityPrefix.test(item.body || '') - )) { - core.setFailed('Immutable agent review comments contain an invalid target or finding format'); - return; - } - const publishedReviewComments = safeOutputItems.filter( - item => item.type === 'create_pull_request_review_comment' - ); - if (publishedReviewComments.some(item => - item.repo !== `${owner}/${repo}` || - Number(item.number) !== pullNumber - )) { - core.setFailed('Published review-comment manifest target does not match this pull request'); - return; - } - if (publishedReviewComments.length !== agentReviewComments.length) { - core.setFailed( - `Published review-comment count ${publishedReviewComments.length} does not match immutable agent count ${agentReviewComments.length}` - ); - return; - } - const submittedReviews = safeOutputItems.filter( - item => item.type === 'submit_pull_request_review' - ); - if (submittedReviews.length !== 1) { - core.setFailed( - `Expected exactly one submitted-review manifest item, found ${submittedReviews.length}` - ); - return; - } - const [submittedReview] = submittedReviews; - if ( - submittedReview.repo !== `${owner}/${repo}` || - Number(submittedReview.number) !== pullNumber - ) { - core.setFailed('Submitted-review manifest target does not match this pull request'); - return; - } - const reviewId = Number(submittedReview.metadata?.review_id); - if (!Number.isInteger(reviewId) || reviewId <= 0) { - core.setFailed('Submitted-review manifest does not contain a valid review ID'); - return; - } - const { data: review } = await github.rest.pulls.getReview({ - owner, - repo, - pull_number: pullNumber, - review_id: reviewId, - }); - if (!review.commit_id || review.commit_id !== expectedHead) { - core.setFailed( - `SFL review commit ${review.commit_id || 'missing'} does not match expected head ${expectedHead}` - ); - return; - } - - const { data: run } = await github.rest.actions.getWorkflowRun({ - owner, - repo, - run_id: runId, - }); - const startedAt = Date.parse(run.run_started_at || run.created_at); - const submittedAt = Date.parse(review.submitted_at); - const elapsed = formatElapsed(startedAt, submittedAt); - const model = formatModel(process.env.SFL_MODEL || 'unknown'); - const aic = formatAic(process.env.SFL_AIC || ''); - - const originalBody = agentSubmittedReview.body || ''; - for (const placeholder of [ - 'SFL_MODEL_PENDING', - 'SFL_TIME_PENDING', - 'SFL_AIC_PENDING', - ]) { - const occurrences = originalBody.split(placeholder).length - 1; - if (occurrences !== 1) { - core.setFailed( - `Review body must contain ${placeholder} exactly once; found ${occurrences}` - ); - return; - } - } - const templatePlaceholders = [ - '{verdict_icon}', - '{VERDICT}', - '{verdict_reason}', - '{security_critical_or_dash}', - '{security_high_or_dash}', - '{security_medium_or_dash}', - '{security_low_or_dash}', - '{accuracy_critical_or_dash}', - '{accuracy_high_or_dash}', - '{accuracy_medium_or_dash}', - '{accuracy_low_or_dash}', - '{quality_maintainability_critical_or_dash}', - '{quality_maintainability_high_or_dash}', - '{quality_maintainability_medium_or_dash}', - '{quality_maintainability_low_or_dash}', - '{total_critical_or_dash}', - '{total_high_or_dash}', - '{total_medium_or_dash}', - '{total_low_or_dash}', - '{Top 3 most impactful current findings with their severity prefixes, or "No findings."}', - ]; - const unresolvedPlaceholder = templatePlaceholders.find(placeholder => - originalBody.includes(placeholder) - ); - if (unresolvedPlaceholder) { - core.setFailed( - `Review body contains unresolved template placeholder ${unresolvedPlaceholder}` - ); - return; - } - - const body = originalBody - .replaceAll('SFL_MODEL_PENDING', model) - .replaceAll('SFL_TIME_PENDING', elapsed) - .replaceAll('SFL_AIC_PENDING', aic); - - const verdictApproved = body.includes('## ✅ Verdict: APPROVE'); - const verdictNeedsWork = body.includes('## ⚠️ Verdict: NEEDS WORK'); - if (verdictApproved === verdictNeedsWork) { - core.setFailed('Review body must contain exactly one recognized SFL verdict'); - return; - } - - const prAuthor = pullRequest.user?.login; - const reviewAuthor = review.user?.login; - if (!reviewAuthor) { - core.setFailed('SFL review author identity is unavailable'); - return; - } - const selfAuthored = Boolean(prAuthor) && prAuthor === reviewAuthor; - const expectedReviewEvent = verdictApproved && !selfAuthored - ? 'APPROVE' - : 'COMMENT'; - const expectedReviewState = verdictApproved && !selfAuthored - ? 'APPROVED' - : 'COMMENTED'; - if ((agentSubmittedReview.event || '').toUpperCase() !== expectedReviewEvent) { - core.setFailed( - `Immutable agent review event ${agentSubmittedReview.event || 'missing'} does not match expected event ${expectedReviewEvent}` - ); - return; - } - if ( - (submittedReview.metadata?.review_event || '').toUpperCase() !== - expectedReviewEvent || - submittedReview.metadata?.review_state !== expectedReviewState - ) { - core.setFailed('Published review manifest does not match the immutable agent verdict'); - return; - } - if (review.state !== expectedReviewState) { - core.setFailed( - `Review state ${review.state || 'missing'} does not match expected state ${expectedReviewState}` - ); - return; - } - - // GitHub accepts body-only updates to submitted COMMENTED reviews - // when authenticated as the app that authored the review. - await github.request('PUT /repos/{owner}/{repo}/pulls/{pull_number}/reviews/{review_id}', { - owner, - repo, - pull_number: pullNumber, - review_id: review.id, - body, - }); - - let publishedFindingCount; - try { - const reviewComments = await github.paginate( - 'GET /repos/{owner}/{repo}/pulls/{pull_number}/reviews/{review_id}/comments', - { - owner, - repo, - pull_number: pullNumber, - review_id: review.id, - per_page: 100, - } - ); - const inlineRunMarker = `[Run ${runId}]`; - publishedFindingCount = reviewComments.filter(comment => - severityPrefix.test(comment.body || '') && - comment.body?.includes(inlineRunMarker) - ).length; - } catch (error) { - core.setFailed(`Could not enumerate SFL findings: ${error.message}`); - return; - } - const immutableFindingCount = agentReviewComments.length; - if (publishedFindingCount !== immutableFindingCount) { - core.setFailed( - `Published finding count ${publishedFindingCount} does not match immutable agent count ${immutableFindingCount}` - ); - return; - } - - let unresolvedFindingCount; - try { - unresolvedFindingCount = await countUnresolvedSflFindings({ - owner, - repo, - pullNumber, - reviewAuthor, - }); - } catch (error) { - core.setFailed(`Could not enumerate unresolved SFL findings: ${error.message}`); - return; - } - - const gateApproved = - verdictApproved && - immutableFindingCount === 0 && - unresolvedFindingCount === 0; - core.setOutput('head-sha', expectedHead); - core.setOutput('gate-approved', gateApproved); - core.setOutput('finding-count', unresolvedFindingCount); - core.setOutput('new-finding-count', immutableFindingCount); - core.setOutput('review-matches-head', true); - core.setOutput('review-state', review.state); - - name: Finalize SFL review evidence - if: always() - uses: actions/github-script@3a2844b7e9c422d3c10d287c895573f7108da1b3 # v9.0.0 - env: - SFL_BASE_SHA: ${{ inputs.base_sha }} - SFL_EVIDENCE_CHECK_RUN_ID: ${{ needs.publish_review_provenance.outputs.evidence-check-run-id }} - SFL_HEAD_SHA: ${{ inputs.head_sha }} - SFL_GATE_APPROVED: ${{ steps.finalize-review.outputs.gate-approved }} - SFL_FINDING_COUNT: ${{ steps.finalize-review.outputs.finding-count }} - SFL_REPLACEMENT_RETRY_ELIGIBLE: ${{ steps.finalize-review.outputs.replacement-retry-eligible }} - SFL_REVIEW_MATCHES_HEAD: ${{ steps.finalize-review.outputs.review-matches-head }} - SFL_REVIEW_STATE: ${{ steps.finalize-review.outputs.review-state }} - SFL_PR_NUMBER: ${{ inputs.item_number }} - SFL_RUN_ID: ${{ github.run_id }} - SFL_UPSTREAM_RESULT: agent=${{ needs.agent.result }}, safe_outputs=${{ needs.safe_outputs.result }}, thread_resolution=${{ needs.resolve_sfl_review_thread.result }} - with: - github-token: ${{ github.token }} - script: | - const baseSha = process.env.SFL_BASE_SHA; - const headSha = process.env.SFL_HEAD_SHA; - if (!/^[0-9a-f]{40}$/.test(baseSha || '')) { - core.setFailed(`Cannot publish SFL approval gate for invalid base SHA: ${baseSha || 'missing'}`); - return; - } - if (!/^[0-9a-f]{40}$/.test(headSha || '')) { - core.setFailed(`Cannot publish SFL approval gate for invalid head SHA: ${headSha || 'missing'}`); - return; - } - const pullNumber = Number(process.env.SFL_PR_NUMBER); - const runId = Number(process.env.SFL_RUN_ID); - if (!Number.isSafeInteger(runId) || runId <= 0) { - core.setFailed(`Invalid workflow run ID: ${process.env.SFL_RUN_ID}`); - return; - } - const { data: pullRequest } = await github.rest.pulls.get({ - owner: context.repo.owner, - repo: context.repo.repo, - pull_number: pullNumber, - }); - const baseMatchesExpected = pullRequest.base.sha === baseSha; - const headMatchesExpected = pullRequest.head.sha === headSha; - const gateApproved = process.env.SFL_GATE_APPROVED === 'true'; - const replacementRetryEligible = - process.env.SFL_REPLACEMENT_RETRY_ELIGIBLE === 'true'; - const evidenceCheckRunId = - Number(process.env.SFL_EVIDENCE_CHECK_RUN_ID); - const gateExternalId = - `sfl-review:${pullNumber}:${baseSha}:${headSha}:${runId}`; - if (!Number.isSafeInteger(evidenceCheckRunId) || evidenceCheckRunId <= 0) { - core.setFailed('Cannot finalize missing SFL review evidence'); - return; - } - const { data: initializedEvidence } = await github.rest.checks.get({ - owner: context.repo.owner, - repo: context.repo.repo, - check_run_id: evidenceCheckRunId, - }); - if ( - initializedEvidence.name !== 'SFL Review Evidence' || - initializedEvidence.head_sha !== headSha || - initializedEvidence.external_id !== gateExternalId || - initializedEvidence.app?.id !== 15368 - ) { - core.setFailed('Initialized SFL review evidence does not match this run'); - return; - } - const publishEvidence = async ({ approved, summary }) => { - let lastError; - for (let attempt = 1; attempt <= 3; attempt += 1) { - try { - return await github.rest.checks.update({ - owner: context.repo.owner, - repo: context.repo.repo, - check_run_id: evidenceCheckRunId, - name: 'SFL Review Evidence', - external_id: gateExternalId, - status: 'completed', - conclusion: approved ? 'success' : 'failure', - completed_at: new Date().toISOString(), - output: { - title: approved - ? 'SFL zero-finding approval gate satisfied' - : 'SFL Reviewer approval is required', - summary, - text: - `replacement_retry_eligible=${replacementRetryEligible}`, - }, - }); - } catch (error) { - lastError = error; - if (attempt < 3) { - core.warning( - `Evidence finalization attempt ${attempt} failed: ${error.message}` - ); - await new Promise(resolve => setTimeout(resolve, attempt * 1000)); - } - } - } - throw lastError; - }; - const publishFailure = async ({ reason }) => { - await publishEvidence({ - approved: false, - summary: - `${reason}. The review cannot satisfy the approval gate. ` + - `Upstream results: ${process.env.SFL_UPSTREAM_RESULT}.`, - }); - core.setFailed(reason); - }; - if (!baseMatchesExpected || !headMatchesExpected) { - await publishFailure({ - reason: - `Pull request changed from base ${baseSha} / head ${headSha} ` + - `to base ${pullRequest.base.sha} / head ${pullRequest.head.sha}`, - }); - return; - } - const summary = gateApproved - ? 'The latest SFL review approved this head with zero inline findings.' - : `The latest SFL review state is ${process.env.SFL_REVIEW_STATE || 'unavailable'} with ${process.env.SFL_FINDING_COUNT || 'unknown'} unresolved SFL finding thread(s), expected-head review match ${process.env.SFL_REVIEW_MATCHES_HEAD || 'unknown'}, live-base match ${baseMatchesExpected}, and live-head match ${headMatchesExpected}, so it does not satisfy the zero-finding approval gate. Upstream results: ${process.env.SFL_UPSTREAM_RESULT}.`; - await publishEvidence({ - approved: gateApproved, - summary, - }); - if (!gateApproved) { - core.setFailed(summary); - } ---- - -## Full-Spectrum PR Review - -## Required Configuration - -This workflow uses a GitHub App for safe-outputs (posting inline review -comments). The following must be configured in the target repository: - -| Kind | Name | Description | -| ---- | ---- | ----------- | -| Variable | `SFL_APP_CLIENT_ID` | Client ID of the GitHub App used for safe-outputs authentication | -| Secret | `SFL_APP_PRIVATE_KEY` | Private key (PEM) of the GitHub App used to mint installation tokens | -| Secret | `OPENROUTER_API_KEY` | HemSoft OpenRouter credential used to run Kimi K3 | - -Set these under **Settings → Secrets and variables → Actions** in the target -repository. If they are missing, the token minting step will fail and review -comments will not be posted. - -**Required Permissions**: Automatic reviews are dispatched by -`sfl-pr-review-auto.yml` with an SFL GitHub App installation token. Humans can -request an explicit rerun with: - -```bash -gh workflow run sfl-pr-review.lock.yml \ - -f item_number=PR_NUMBER \ - -f base_sha=PULL_REQUEST_BASE_SHA \ - -f head_sha=PULL_REQUEST_HEAD_SHA \ - -f review_effort=low \ - -f 'aw_context={"item_type":"pull_request","item_number":PR_NUMBER,"base_sha":"PULL_REQUEST_BASE_SHA","head_sha":"PULL_REQUEST_HEAD_SHA"}' -``` - ---- - -You are an expert code reviewer performing a comprehensive 3-pass analysis of -this pull request. Your goal is to surface **real issues** — bugs, security -vulnerabilities, logic errors, and architectural concerns — not style nits. - -## Setup - -1. Get the PR diff and changed files: - - ```bash - gh pr diff ${{ inputs.item_number }} --repo ${{ github.repository }} - ``` - -2. Get the list of changed files: - - ```bash - gh pr view ${{ inputs.item_number }} --repo ${{ github.repository }} --json files --jq '.files[].path' - ``` - -3. Read the full content of each changed file to understand context beyond the - diff (imports, class structure, related functions). - -4. Treat `${{ inputs.base_sha }}` and `${{ inputs.head_sha }}` as the immutable - expected base and head for this run. Read the live pull request base and head - before reviewing. If either differs, stop without posting review output; the - changed pull request state requires its own SFL run. - -5. List the pull request's unresolved review threads before creating comments. - Re-evaluate every unresolved SFL finding against the expected head. A prior - finding is an unresolved inline comment authored by the SFL reviewer whose - body starts with one of the exact severity prefixes below and contains the - `SFL Reviewer` run footer. Carry forward findings that still apply. Exclude - resolved findings. For every unresolved SFL finding that no longer applies - and whose thread GitHub reports as outdated, call - `resolve_sfl_review_thread` with its thread ID before submitting the current - review. Never request resolution for a live unresolved thread; carry it - forward until a maintainer resolves it or a later commit makes it outdated. - -## Review Passes - -Execute all 3 passes sequentially. For each finding, post an **inline review -comment** on the specific line using the `create_pull_request_review_comment` -tool. - -### Severity Accounting - -Every inline comment must represent **exactly one finding** and begin with -exactly one of these prefixes, replacing `{Pass}` with `Security`, -`Accuracy`, or `Quality & Maintainability`: - -| Severity | Prefix | Definition | -| ---------- | -------- | ------------ | -| Critical | `🔴 **CRITICAL — {Pass}**` | Exploitable security issue, data loss, systemic outage, or release-blocking defect | -| High | `🟠 **HIGH — {Pass}**` | Likely production defect, serious security weakness, or major regression | -| Medium | `🟡 **MEDIUM — {Pass}**` | Material correctness, maintainability, or operational issue that should be fixed | -| Low | `⚪ **LOW — {Pass}**` | Minor but actionable improvement with limited risk | - -Apply this same severity scale to all three passes. Do not combine multiple -findings into one inline comment. Maintain a ledger containing both new -findings successfully posted in this run and still-applicable unresolved SFL -findings carried from earlier runs, grouped by pass and severity. Do not post a -duplicate inline comment for a carried finding. - -For each new finding, pass `side: RIGHT` for an added or context line and -`side: LEFT` for a deleted line. Always pass the side explicitly. End every new -inline finding with this run marker: - -```markdown ---- -*SFL Reviewer • {Pass} • {Severity} • [Run ${{ github.run_id }}](${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }})* -``` - -The run marker makes findings from repeated reviews distinguishable. New -findings use the current run ID; carried findings retain their original run -marker and are counted through the unresolved-finding ledger. - ---- - -### Pass 1: 🔒 Security Review - -Analyze for security vulnerabilities using OWASP Top 10 and CWE Top 25 as -the baseline. Look for: - -| Category | Examples | -| ---------- | ---------- | -| Injection | SQL injection, command injection, XSS, SSTI | -| Authentication | Broken auth flows, weak token handling, session issues | -| Authorization | Missing access control, privilege escalation, IDOR | -| Data exposure | PII leaks, sensitive data in logs, missing encryption | -| Configuration | Hardcoded secrets, debug mode in prod, insecure defaults | -| Dependencies | Known vulnerable packages, outdated critical deps | -| Cryptography | Weak algorithms, improper key management, predictable random | -| Input validation | Missing validation, type confusion, buffer overflow | - -Only report findings with **>80% confidence** of exploitability. Do not -flag theoretical risks that require implausible attack vectors. - ---- - -### Pass 2: ✅ Accuracy & Reliability Review - -Trace the changed behavior from inputs and state through outputs and failure -paths. Look for demonstrable defects: - -| Area | What to check | -| ------ | --------------- | -| Logic | Incorrect conditions, calculations, ordering, or control flow | -| Edge cases | Empty, null, boundary, overflow, timeout, and partial-success behavior | -| State | Invalid transitions, stale state, race conditions, or non-idempotent retries | -| Error handling | Swallowed errors, incorrect recovery, misleading success, or missing cleanup | -| Data integrity | Lost, duplicated, corrupted, or partially committed data | -| Contracts | Implementations that violate explicit API, type, or behavioral contracts | - -Only report a finding when you can explain a concrete input/state sequence and -the incorrect result it produces. Do not speculate about hypothetical runtime -conditions without a credible path through the changed code. - ---- - -### Pass 3: 🧰 Quality & Maintainability Review - -Evaluate whether the change remains understandable, modifiable, and testable: - -| Area | What to check | -| ------ | --------------- | -| Cohesion | Functions or modules with unrelated responsibilities | -| Coupling | Hidden dependencies, brittle cross-module knowledge, or leaky abstractions | -| Duplication | Independently maintained logic that can drift or contradict itself | -| Complexity | Control flow or state handling that obscures behavior and increases defect risk | -| API clarity | Ambiguous contracts, surprising side effects, or inconsistent interfaces | -| Testability | Design choices that prevent important behavior from being isolated or verified | - -Do **not** report formatting, import ordering, bracket placement, comment style, -or naming that follows repository conventions. Do not calculate or estimate -CRAP, coverage, scorecard, or other numeric scores without measured repository -artifacts. - -### Conditional Evidence Checks - -Use these only when the changed files provide direct evidence: - -- **Tests**: Flag a gap only when changed behavior can be mapped to an existing - test seam and a concrete regression case is missing. -- **Compatibility**: Check only when the PR changes a public API, schema, - configuration key, serialized format, or persisted data contract. -- **Performance**: Report only concrete static problems such as N+1 queries, - unbounded work, blocking I/O on a known hot path, or avoidable large - allocations. -- **Migrations/rollout**: Check only when migration or deployment artifacts are - changed and a specific forward/rollback failure is visible. - -Classify applicable findings under Accuracy or Quality & Maintainability. If -evidence is unavailable, do not create a finding. - ---- - -## Communication quality - -Run the embedded Unslop pass on every user-facing sentence before each -`create_pull_request_review_comment` or `submit_pull_request_review` call. This -includes inline findings, the verdict reason, key findings, and any explanation -of the self-approval exception. Preserve the technical meaning, evidence, -severity, location, remediation, counts, and verdict. - -Protocol literals override the Unslop rules. Keep the required severity -prefixes, verdict headings, summary heading, table labels, placeholders, run -markers, and footer exactly as specified, including their emoji, bold text, and -em dashes. Apply the rules below only to the natural-language prose around -those literals. - -### Embedded Unslop pass - -1. Scan the draft for the patterns below. -2. Rewrite it without changing technical meaning or evidence. -3. Add a direct human voice where it helps clarity. -4. Ask, "What makes this obviously AI generated?" Fix any remaining tells. - -Use these writing rules: - -- State what happened. Cut puffery, promotional language, vague claims, and - formulaic framing. -- Name the source of a claim or remove the attribution. Do not write "experts - believe" or "reports suggest" without a source. -- Remove superficial `-ing` phrases that add no evidence. -- Prefer plain words. Avoid AI stock words such as "additionally", "crucial", - "delve", "enhance", "foster", "intricate", "pivotal", "showcase", - "tapestry", "testament", "underscore", and abstract uses of "landscape". -- Use "is" and "has" instead of inflated substitutes such as "serves as", - "stands as", "boasts", or "features". -- Do not use "not just X, but Y", forced groups of three, synonym cycling, or - false "from X to Y" ranges. -- Do not use em dashes in free-form prose. Split the sentence or use a comma. -- Use colons only when they introduce a real list or example. -- Avoid decorative bold text, decorative emoji, title-case headings, and curly - quotes in free-form prose. -- Remove chatbot filler, cutoff disclaimers, sycophancy, generic conclusions, - and phrases such as "I hope this helps", "Of course", or "Great question". -- Cut filler. Use "to" instead of "in order to" and "because" instead of "due - to the fact that". Remove "it is important to note that". -- Reduce hedging to the uncertainty the evidence requires. -- Replace abstract metaphor jargon such as "substrate", "wedge", "vector", - "locus", "nexus", "primitive", "harness", "surface", "bedrock", - "scaffolding", "modality", "paradigm", "gold-plating", "ratchet", - "evacuate", "endgame", "north star", and "flywheel" with concrete terms. -- Say what the code does and what fails. Do not substitute mood or feeling for - a mechanism, condition, path, command, or measured result. -- Keep one idea per sentence. Split sentences that require rereading. -- Prefer active voice. Name the actor when it matters. -- Cut adverbs that prop up weak verbs. -- Be specific and opinionated when the evidence supports it. Use "I" only when - it sounds natural in a review comment. -- Vary sentence length, but do not make the prose chatty or theatrical. - -Run this pass after the technical content is complete. It is a communication -filter, not permission to soften, omit, invent, or reclassify a finding. - ---- - -## Summary - -This is a hard completion contract. Do not end the run with prose-only output. -Before exiting, make exactly one successful `submit_pull_request_review` call. -If a defensible verdict cannot be produced because required evidence is -unavailable, call `missing_data` with the specific blocker instead; never call -`noop` and never silently stop. - -After all 3 passes, submit a **pull request review** using -`submit_pull_request_review` with: - -- **Event**: - - Use `APPROVE` when the verdict is `APPROVE` and the pull request author is - not `sfl-app[bot]`. - - Use `COMMENT` when the verdict is `NEEDS WORK`. - - Use `COMMENT` when the verdict is `APPROVE` but the pull request was authored - by `sfl-app[bot]`, because GitHub does not allow an app to approve - its own pull request. Explain this exception in the verdict reason. -- **Body**: A structured summary: - -```markdown -## {verdict_icon} Verdict: {VERDICT} - -{verdict_reason} - -## 🔨 Full-Spectrum Review Summary - -> **Review done by:** SFL_MODEL_PENDING
**Review time:** SFL_TIME_PENDING
**Token count:** SFL_AIC_PENDING AIC - -| Pass | 🔴 Critical | 🟠 High | 🟡 Medium | ⚪ Low | -|------|------------:|--------:|----------:|-------:| -| 🔒 Security | {security_critical_or_dash} | {security_high_or_dash} | {security_medium_or_dash} | {security_low_or_dash} | -| ✅ Accuracy | {accuracy_critical_or_dash} | {accuracy_high_or_dash} | {accuracy_medium_or_dash} | {accuracy_low_or_dash} | -| 🧰 Quality & Maintainability | {quality_maintainability_critical_or_dash} | {quality_maintainability_high_or_dash} | {quality_maintainability_medium_or_dash} | {quality_maintainability_low_or_dash} | -| **Total** | **{total_critical_or_dash}** | **{total_high_or_dash}** | **{total_medium_or_dash}** | **{total_low_or_dash}** | - -### Key Findings -{Top 3 most impactful current findings with their severity prefixes, or "No findings."} - ---- -*HemSoft full-spectrum review triggered automatically or by workflow dispatch • 3 evidence-based passes • [Run details](${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }}) • SFL run ID: ${{ github.run_id }}* -``` - -Choose the verdict deterministically: - -- If any new or still-applicable unresolved Critical, High, Medium, or Low - finding exists, use - `## ⚠️ Verdict: NEEDS WORK` and explain that every actionable finding must be - resolved before merge. -- Use `## ✅ Verdict: APPROVE` only when all four severity totals are zero. - -Before submitting, reconcile the ledger against new inline comments and -carried unresolved findings: - -1. Count each successfully posted current-run inline finding exactly once. -2. Count each still-applicable unresolved SFL finding from an earlier run - exactly once without duplicating its inline comment. -3. Resolve every earlier SFL thread that no longer applies and GitHub reports - as outdated. Never request resolution for a live unresolved thread. Never - approve while any SFL finding thread remains unresolved. -4. Never count resolved findings or duplicate reports of the same defect. -5. Every counted finding must have exactly one severity prefix. -6. Each table cell must equal the union of new and carried findings with that - exact pass and severity prefix. -7. The Total row must equal the column sums and the complete current finding - set. -8. Do not count positive observations, failed comment attempts, or summary-only - notes as findings. -9. Render every zero count as an em dash (`—`). Render only nonzero counts as - numbers. Keep the underlying numeric totals for verdict calculation. -10. Copy the `SFL_MODEL_PENDING`, `SFL_TIME_PENDING`, and `SFL_AIC_PENDING` - placeholders exactly. A post-review job replaces them with finalized values. -11. Replace every other `{...}` template field with its actual value. Never - submit literal brace or angle-bracket placeholders. -12. **Match event to verdict**: An `APPROVE` verdict must submit an `APPROVE` - event unless GitHub's self-approval restriction applies. Never submit a - `COMMENT` event for an eligible `APPROVE` verdict. - -## Rules - -1. **Be precise**: Comment on exact lines, not general areas. -2. **Be actionable**: Every finding must include a specific fix or recommendation. -3. **One comment, one finding**: Never combine independently actionable issues. -4. **Severity is mandatory**: Begin every inline finding with exactly one - severity prefix from the Severity Accounting table. -5. **Be honest**: If confidence is below the pass threshold, do not post it. -6. **No false positives**: Better to miss a minor issue than flag a non-issue. -7. **Cross-reference**: If a finding in one pass relates to another, mention it. -8. **Respect context**: Read the full file, not just the diff. Understand the - codebase patterns before flagging "violations." -9. **Pin the review**: All comments and the summary review must target - `${{ inputs.head_sha }}` and apply only to base `${{ inputs.base_sha }}`. - Never target a later pull request head or a different base. -10. **Close obsolete threads**: Resolve prior SFL threads only after verifying - their findings no longer apply to the expected head and GitHub reports the - thread as outdated. Never request resolution for a live unresolved thread. -11. **Accepted gate trust boundary**: The reviewer initializes and finalizes - one `SFL Review Evidence` check on the immutable head with the - repository-scoped `GITHUB_TOKEN` and exact - `sfl-review::::` locator. Because check fields are - caller-controlled, neither App ID nor `external_id` authorizes a merge. The - branch protection requires the GitHub Actions-owned native - immutable-head `SFL Reviewer Approval` check; the default-branch - `sfl-pr-review-auto.yml` gate runner authenticates the exact reviewer - through the Actions API and publishes that check with the - repository-scoped `GITHUB_TOKEN` only when the exact run succeeds. - Maintainers who can modify workflows on the protected default branch are - explicitly trusted under this first-version model. Defending against those - maintainers requires a central publisher or OIDC broker and is deliberately - out of scope. Still file any change that weakens this boundary, such as - trusting check fields without Actions-run validation, dropping PR/base/head - matching, or replacing the required workflow with a reusable head status. diff --git a/docs/SFL-REVIEWER-PARITY.md b/docs/SFL-REVIEWER-PARITY.md index 8ead8d00..0e12063c 100644 --- a/docs/SFL-REVIEWER-PARITY.md +++ b/docs/SFL-REVIEWER-PARITY.md @@ -1,84 +1,45 @@ -# SFL reviewer parity baseline +# SFL reviewer migration baseline -HemSoft tracks the reviewer contract from the private -`relias-engineering/set-it-free-loop` repository without treating byte-for-byte -identity as the goal. Provider configuration and distribution boundaries differ -intentionally, and HemSoft retains tested hardening when it is at least as -strict as the Relias behavior. +HemSoft no longer maintains byte-level parity with the Relias compiled reviewer. +Issue #125 deliberately replaces the Kimi/OpenRouter execution path with the +native Codex GitHub review included in Franz's ChatGPT subscription. -The machine-readable baseline and HemSoft release identity are in -[`deployment/release-metadata.json`](../deployment/release-metadata.json). -Its `distribution.version` is the HemSoft package version; the independent -`reviewerBaseline.releaseVersion` identifies the reviewed Relias contract. -It distinguishes the `v6.5.7` tag commit -`8d5e30714fa6cc61a89189266f8eb463132abde9` from the reviewed default-branch -commit `669d4d84ef37ebab107c5931d6727846626062e5`. Only `TODO.md` changed between -those commits; all four reviewer artifacts are identical at both points. +The machine-readable runtime identity is recorded in +[deployment/release-metadata.json](../deployment/release-metadata.json). The +deployed contract is one observer workflow, not a copied agent workflow, +compiled lock, and recovery wrapper. -## Intentional differences +## Verified identities -| Artifact | Difference class | HemSoft contract | Regression evidence | -| --- | --- | --- | --- | -| Reviewer source and compiled lock | Provider configuration | Uses Kimi K3 through the private OpenRouter route, carries explicit pricing into both runtimes, restricts network access to `openrouter.ai`, and does not request Copilot billing permission. | `test-sfl-pr-review.ps1` | -| Reviewer source and compiled lock | Private distribution boundary | Uses HemSoft source identity, repository-scoped App configuration, and no hard-coded App installation identity. | `test-sfl-pr-review.ps1`, `test-sfl-review-platform.ps1` | -| Reviewer source and compiled lock | Tested HemSoft hardening | Resolves only already-resolved or GitHub-outdated SFL threads and refuses to resolve a live unresolved thread. | `test-sfl-pr-review.ps1`, `test-sfl-verdict-validator.ps1` | -| Auto trigger and gate | Private distribution boundary | Supports bootstrap deployment before the compatible reviewer lock reaches the default branch and mints a repository-scoped App token with pull-request write permission only where label mutation requires it. | `test-sfl-review-platform.ps1` | -| Auto trigger and gate | Tested HemSoft hardening | Reuses successful exact-head reviews after delayed ordinary events, preserves explicit-label reruns, publishes the native immutable-head `SFL Reviewer Approval` check only after authenticating the exact reviewer run, and suppresses maintenance-mode dispatch while keeping the required runner active and failed closed. | `test-sfl-dispatch-dedup.ps1`, `test-sfl-review-platform.ps1`; issues #46 and #49 runtime evidence | -| CLI gate | Private distribution boundary | Uses a repository-scoped strict `SFL Reviewer Gate Runner` rule owned by GitHub Actions because `HemSoft` is a personal account. The runner authenticates the inner App approval and evidence checks. Relias uses an organization required-workflow rule. Immediate entity-tag and state rechecks detect drift before unsupported unconditional GitHub ruleset mutations. Verification and bounded rollback preserve unrelated repository policy. | `gate_test.go`, `deployment_test.go`; issue #73 | -| Recovery | Tested HemSoft hardening | Fails closed when agent output is absent or malformed, rechecks immutable PR state plus newer-run ownership immediately before retry dispatch, and suppresses retry while maintenance mode is active. | `test-sfl-review-platform.ps1` | +| Purpose | Identity | +| --- | --- | +| Codex GitHub App | ID 1144995, slug chatgpt-codex-connector, owner openai | +| Codex bot | User ID 199175422, login chatgpt-codex-connector[bot] | +| Required SFL gate | SFL Reviewer Gate Runner, GitHub Actions App ID 15368 | -The generated lock is not independently allowlisted: its differences must be -the compiled representation of the documented reviewer-source differences. -`test-sfl-pr-review.ps1` enforces the compiler/runtime markers and explicit -provider pricing, while canonical/staged identity remains enforced by the -reviewer platform contract. +## Runtime evidence -gh-aw v0.86.2 emits `concurrency.queue: max` in the generated conclusion job. -GitHub has accepted and executed that lock, but actionlint v1.7.12 does not yet -recognize the key. `.github/actionlint.yaml` ignores only that exact diagnostic -and only for the generated reviewer lock; all other actionlint checks remain -active for the lock and wrappers. +The initial hs-buddy experiment produced both supported shapes: -## Running the audit +- [owner trigger on PR #427](https://github.com/HemSoft/hs-buddy/pull/427#issuecomment-5336227425); +- [clean Codex result on PR #427](https://github.com/HemSoft/hs-buddy/pull/427#issuecomment-5336243021); +- [finding review example on PR #415](https://github.com/HemSoft/hs-buddy/pull/415#pullrequestreview-4947117457). -The default mode verifies the metadata, normalized HemSoft hashes, staged -copies, difference classes, and referenced tests. Normalizing CRLF and LF before -hashing keeps checkout line endings from creating false drift. +GitHub exposes full App provenance on the clean issue comment. Its review REST +object omits performed_via_github_app, so findings use the immutable bot ID, +review login, COMMENTED state, full commit_id, resolved reviewed-commit prefix, +and current pull request head. -```powershell -pwsh -NoProfile -File deployment/tests/test-relias-reviewer-parity.ps1 -``` +## Regression evidence -When an authorized local Relias checkout is available, supply it to verify the -pinned tag, reviewed commit, post-tag path delta, and upstream hashes directly -from Git objects. The audit is read-only and does not check out or modify the -Relias worktree. +- gh-sfl/reviewer_contract_test.go executes clean, finding, stale, spoofed, and + malformed classification fixtures. +- gh-sfl/review_test.go proves one head-bound trigger comment and duplicate + suppression. +- deployment/tests/test-sfl-pr-review.ps1 proves canonical/staged identity, + legacy artifact removal, and absence of OpenRouter engine configuration. +- deployment/tests/test-sfl-review-platform.ps1 proves deployment and gate + wiring. -```powershell -pwsh -NoProfile -File deployment/tests/test-relias-reviewer-parity.ps1 ` - -ReliasCheckout D:\github\Relias\set-it-free-loop -``` - -Any reviewer artifact change fails the audit until the baseline hashes and this -contract-diff note are deliberately reviewed together. A future Relias baseline -update must record both the release tag commit and the exact reviewed commit; -never move the recorded commit to a mutable branch name. HemSoft release bumps -change only `distribution.version`; they do not rewrite the reviewer baseline. - -## CLI source ownership - -The private HemSoft distribution owns its adapted `gh sfl` Go source in -`gh-sfl/`. The initial import is pinned in `gh-sfl/PROVENANCE.md` to Relias -release `v6.5.7` at commit -`8d5e30714fa6cc61a89189266f8eb463132abde9`. Repository builds, tests, and -installation consume only that version-controlled HemSoft tree; the optional -local Relias checkout above is used solely by the read-only reviewer parity -audit. - -The CLI preserves the reviewer rollout capabilities while enforcing -HemSoft-specific boundaries: reviewer-only and pull-request-first defaults, -immutable private HemSoft release reads, `HemSoft` CLI identity, HemSoft-owned -consumer targets, protected mother-repository rejection, and source-path -routing for the four-file reviewer package. `.github/workflows/validate-gh-sfl.yml` -keeps the source formatted, vetted, tested, and cross-buildable for Windows and -Linux. +This migration is intentionally HemSoft-specific. It does not change the Relias +repository or developer-documentation. diff --git a/docs/SFL-REVIEWER.md b/docs/SFL-REVIEWER.md index 5d26711b..4278f8d3 100644 --- a/docs/SFL-REVIEWER.md +++ b/docs/SFL-REVIEWER.md @@ -1,263 +1,151 @@ -# HemSoft SFL Pull Request Reviewer +# HemSoft SFL pull request reviewer -The HemSoft reviewer is a HemSoft-owned repository package that performs three -evidence-based passes over eligible pull requests, publishes native inline -findings, and exposes a current-head approval check suitable for branch -protection. +HemSoft SFL uses the native Codex GitHub review connected to Franz's ChatGPT +subscription. It does not run a model through OpenRouter and does not require an +OpenAI API key. -## Distribution boundary +## Package -The deployment scripts fail closed unless the target is owned by `HemSoft`. -The source repository, release assets, and credentials remain private. Reviewer -workflow deployments may target public or private HemSoft repositories, but -must not be published outside HemSoft-owned repositories. +The review tier installs one workflow: -## Package +- .github/workflows/sfl-pr-review-auto.yml observes authenticated Codex results + and publishes the immutable-head SFL Reviewer Gate Runner check. + +A synchronized deployment also removes the retired sfl-pr-review.md, +sfl-pr-review.lock.yml, and sfl-pr-review-recovery.yml files. + +## Prerequisite + +Install and connect the Codex GitHub App for the target repository using the +same ChatGPT account that owns the Codex subscription. SFL cannot inspect that +subscription connection through GitHub CLI, so a current-head smoke review is +the rollout proof. + +No reviewer Actions variable or AI-provider secret is required. The reviewer +does not read OPENROUTER_API_KEY, OPENAI_API_KEY, CODEX_API_KEY, +SFL_APP_CLIENT_ID, or SFL_APP_PRIVATE_KEY. + +The observer uses the SHA-pinned GitHub-owned actions/github-script action. A +selected-actions policy must therefore allow GitHub-owned actions. + +## Deploy + +Deploy through a pull request: + + .\deployment\scripts\deploy-workflow.ps1 ` + -Tier review ` + -Repos "HemSoft/repository" + +The gh sfl adapter provides the same package: + + gh sfl init --repo HemSoft/repository + gh sfl sync --repo HemSoft/repository + +Both paths are restricted to HemSoft-owned consumers and default to a +reviewable deployment pull request. + +## Request a review + +Request one review for the current pull request head: + + gh sfl review --repo HemSoft/repository --pr 42 + +The reviewer supports pull requests whose head branch is in the target +repository and whose base is the repository's default branch. Fork pull +requests are rejected because GitHub downgrades workflow tokens for fork review +events, preventing reliable publication of the required check. Non-default base +branches are rejected because base-advance invalidation is deliberately scoped +to the trusted default branch. + +Only review requests posted by the repository owner and registered by +`gh sfl review` are accepted. This HemSoft reviewer is owner-operated; member, +collaborator, and hand-crafted owner comments cannot authorize a gate result. + +The command: + +1. verifies the active GitHub identity is HemSoft; +2. verifies the pull request is open and the observer is installed; +3. waits for applicable pull-context and default-branch invalidation workflows, + then revalidates the pull request head, base, and default branch; +4. posts @codex review with an invisible marker containing the full head SHA, + base SHA, and latest SFL invalidation context token; +5. records the returned comment ID in the append-only + `SFL Codex Review Request Registry` commit-status context; +6. reuses the existing request URL instead of posting a duplicate for that + head. + +The registry preserves request identity if the owner later edits the comment. +The observer rejects edited markers, while `--retry` waits for the registered +request's Codex reaction lifecycle before posting an immutable replacement. +Ordinary edited discussion comments are not review requests. +Each registered request also publishes an immediate failing gate before its +Codex result is observed, so an earlier success cannot remain merge-valid while +a newer review is pending. + +A new commit creates a new head and therefore permits one new request. +If a base change or retarget would reuse a head that was already requested +against another base SHA, update the pull request branch first. Native Codex +artifacts identify the reviewed head but not the invoking base, so SFL rejects +same-head cross-base reuse rather than guessing which request produced a result. + +## Result contract + +Codex currently emits two result shapes: + +- clean: an issue comment beginning with + "Codex Review: Didn't find any major issues."; +- findings: a COMMENTED pull request review, usually with inline comments. + +Both include a Reviewed commit SHA prefix. The observer resolves that prefix +through GitHub and requires the resulting full SHA to equal the current pull +request head. A stale result is ignored and cannot satisfy or fail the new head. + +For clean comments, the observer requires GitHub App ID 1144995, slug +chatgpt-codex-connector, owner openai, and bot user ID 199175422. GitHub's +pull-request-review REST object currently omits performed_via_github_app; +finding reviews therefore require the immutable bot user ID and login, a +COMMENTED state, the full review commit_id, and the resolved reviewed-commit +prefix. If GitHub starts returning App provenance on reviews, the observer +requires the same App identity. + +Authenticated malformed current-head output fails closed. Spoofed or stale +output is ignored. Each request can publish only one terminal gate, and event +redelivery is idempotent through a request-specific check-run external ID. + +## Gate + +After deployment, enable the existing strict branch rule: + + gh sfl gate --repo HemSoft/repository + +The rule still requires SFL Reviewer Gate Runner from GitHub Actions App ID +15368 with strict base freshness. The observer writes that exact check on the +reviewed head: + +- clean Codex result: success; +- current-head Codex findings or malformed authenticated output: failure; +- stale or spoofed artifact: no check. + +## Semantic change from the retired reviewer -The `review` tier installs these files: - -- `.github/workflows/sfl-pr-review.md` - runtime-imported agent instructions -- `.github/workflows/sfl-pr-review.lock.yml` - generated executable workflow -- `.github/workflows/sfl-pr-review-auto.yml` - PR lifecycle dispatcher and - immutable-head `SFL Reviewer Approval` check publisher -- `.github/workflows/sfl-pr-review-recovery.yml` - one-shot missing-output - recovery - -The lock must be generated from the deployed Markdown with the repository's -checksum-verified `gh-aw` compiler. Never edit the lock directly. - -The Kimi model has explicit provider pricing in the Markdown frontmatter. The -compiler must carry that pricing into both the primary reviewer and the -mandatory threat-detection firewall configuration. Generic fallback pricing -is not an acceptable substitute because the two runtimes are generated -independently. - -## Required repository configuration - -The target repository needs the following Actions configuration: - -| Kind | Name | Purpose | -| --- | --- | --- | -| Variable | `SFL_APP_CLIENT_ID` | Reviewer GitHub App client ID | -| Secret | `SFL_APP_PRIVATE_KEY` | App installation private key | -| Secret | `OPENROUTER_API_KEY` | Private Kimi K3 route | - -The App installation may cover all HemSoft repositories or selected intended -repositories. Its exact repository permission contract is Actions read/write, -Checks read/write, Contents read, Issues read/write, Metadata read, and Pull -requests read/write. It must not have Contents write, Workflows write, or -unrelated repository permissions. Review -evidence and obsolete-thread mutations use the repository-scoped `GITHUB_TOKEN` -instead of expanding the shared App's permission ceiling. - -Configure App credentials only through the fail-closed bootstrap: - -```powershell -.\deployment\scripts\set-sfl-github-app-credentials.ps1 ` - -Repos HemSoft/repository ` - -AppId 123456 ` - -ClientId Iv1.example ` - -PrivateKeyPath C:\secure\sfl-app.private-key.pem -``` - -Before its first variable or secret write, the bootstrap uses a locally signed -App JWT to verify the App ID, client ID, HemSoft ownership, target access, and -permission ceiling for every requested repository. It also independently -verifies each target is a `HemSoft/*` repository. If any target fails, none of -the targets are mutated. - -`gh sfl init`, `gh sfl sync`, and `gh sfl add pr-review` inspect only repository -metadata and credential names; they never read secret values. Before preparing -a reviewer deployment pull request, including a `pr-review` add-on deployment, -they require a default branch, enabled GitHub Actions, the variable above, and -both secrets above. A normal GitHub CLI OAuth token cannot inspect -GitHub App installations owned by the `HemSoft` personal account, so App scope -and permission-ceiling validation belongs to the App-authenticated credential -bootstrap and remains a required rollout step. -Repositories with a selected-actions policy must enable the policy's -GitHub-owned-actions option; exact allowlist patterns are intentionally not a -supported rollout policy because the compiled reviewer uses a changing set of -SHA-pinned `actions/*` and `github/gh-aw-actions/*` actions. -`gh sfl status` reports each missing prerequisite separately from package drift, -gate posture, and reviewer-run health. - -## Deployment - -Deploy through a reviewable pull request: - -```powershell -.\deployment\scripts\deploy-workflow.ps1 ` - -Tier review ` - -Repos "HemSoft/repository" -``` - -The deployer stamps the exact source commit into the reviewer Markdown and the -two standard YAML wrappers, compiles the Markdown in the consumer checkout, and -records all reviewer components in `sfl.json`. A later deployment from the same -source should produce no unexpected workflow diff. - -For local dogfood materialization: - -```powershell -.\deployment\scripts\deploy-workflow.ps1 -Local -Compile -``` - -The HemSoft `gh sfl` adapter exposes the equivalent package as the `reviewer` -tier and writes the CLI-compatible `.sfl/sfl.json` manifest. Its Go source is -owned in this repository under `gh-sfl/`; building or installing it does not -read from a Relias checkout. The provenance of the initial source import is -recorded in `gh-sfl/PROVENANCE.md`. - -Validate and build without changing the installed extension: - -```powershell -.\gh-sfl\build.ps1 -NoInstall -``` - -Install the local build, then open the default reviewer deployment PR: - -```powershell -.\deployment\scripts\install-gh-sfl-hemsoft.ps1 -gh sfl init --repo HemSoft/repository -``` - -`init` defaults to the `reviewer` tier and both `init` and `sync` default to a -signed deployment pull request. `--direct` is an explicit opt-in for direct -default-branch mutation. Workflow reads are pinned to the SHA behind the -selected private HemSoft release, and `.sfl/sfl.json` records both its version -and source SHA. - -Both deployment paths reject non-HemSoft or non-private mutation targets. The -CLI also requires the active GitHub CLI identity to be `HemSoft`; direct Git -writes use the `github-personal1` SSH profile. - -During the first deployment, review-submitted events can load the wrapper from -the deployment branch before the compatible reviewer lock exists on the -default branch. The wrapper detects that state, does not mint or dispatch, and -reports a successful bootstrap gate. This exception ends as soon as the lock -with exact `dispatch_id` correlation reaches the default branch. Configure -branch protection only after that deployment is merged. - -The supported rollout order is: - -1. Ensure the App installation covers the repository and run the credential - bootstrap above. It verifies target access and the permission ceiling with - App authentication before setting credentials. -2. Run `gh sfl init` or `gh sfl sync` and merge its reviewed deployment PR. -3. Run `gh sfl gate` to require the reviewer without replacing unrelated - repository rules. -4. Open a smoke PR and require `gh sfl status` to show a successful reviewer - run, current manifest/files, healthy credentials/App, and the required gate. - -Maintainers who can change workflow files on the protected default branch are -trusted in this first HemSoft rollout model. The reviewer prevents unreviewed -pull-request heads from satisfying its gate; it does not attempt to defend -against an authorized maintainer changing the trusted default-branch workflow. - -## Automatic and explicit review - -An internal, non-draft pull request targeting the default branch is reviewed -when it is opened, reopened, marked ready for review, or synchronized with a new -commit. Fork pull requests are skipped because repository secrets are not made -available to them. - -Adding `sfl-review` requests an explicit rerun. The dispatcher consumes the -label after validating the pull request and passes immutable PR number, base -SHA, and head SHA inputs to the executable workflow. Duplicate runs for the -same sealed context are suppressed. - -An owner, organization member, or repository collaborator can make the same -request by adding this exact standalone comment to an eligible pull request: - -```text -@sfl-app review -``` - -SFL acknowledges the command once and replaces the acknowledgement with a link -to the exact Actions run. Duplicate delivery of one comment is coalesced, and -edited comments do not trigger the command. The command uses the existing -`sfl-review` label path, so automatic review, immutable-head validation, and the -required gate keep their established behavior. - -The optional `review_effort` input is an audit marker retained in provenance -and forwarded by recovery. The HemSoft Kimi route does not currently map it to -a provider-specific reasoning control. - -A maintainer can dispatch manually from the default branch: - -```powershell -$context = '{"item_type":"pull_request","item_number":42,"base_sha":"BASE_SHA","head_sha":"HEAD_SHA"}' -gh workflow run sfl-pr-review.lock.yml ` - --ref main ` - -f item_number=42 ` - -f base_sha=BASE_SHA ` - -f head_sha=HEAD_SHA ` - -f retry_count=0 ` - -f dispatch_id=manual ` - -f review_effort=low ` - -f "aw_context=$context" -``` - -## Evidence and approval - -Each run validates its repository, workflow path, default-branch revision, pull -request, base, and head before the reviewer starts and again before safe output. -The workflow initializes an in-progress evidence check for the exact run and -head, then finalizes that same evidence after reconciling immutable artifacts -and SFL-owned review threads. - -Every unresolved Critical, High, Medium, or Low SFL finding blocks approval. -Older success cannot satisfy a newer head or a pull request whose base branch -advanced. Obsolete threads are resolved only after verifying their SFL App -ownership and proving they belong to the expected pull request context. -The trusted wrapper publishes `SFL Reviewer Approval` directly on that head -with the repository-scoped `GITHUB_TOKEN`; the wrapper job itself runs in the -safer default-branch context and is not used as the required status context. - -## Recovery - -If a trusted reviewer run fails or times out without one formal review, the -recovery workflow downloads and validates its provenance and safe-output -artifacts. It retries exactly once only when: - -- the pull request is still eligible; -- base and head are unchanged; -- no newer run already owns the same context; and -- the agent did not emit an explicit terminal missing-data, missing-tool, or - incomplete-report signal. - -A stale context suppresses recovery. A second missing review fails closed. -Published reviews are reconciled before retry so a missing artifact cannot -produce a duplicate review. - -## Advisory and gated operation - -Deployment is advisory until a repository rule requires -`SFL Reviewer Gate Runner`. After the deployment PR is merged, enable strict -gating with: - -```powershell -gh sfl gate --repo HemSoft/repository -``` - -The command creates a dedicated repository ruleset on the consumer's default -branch. The rule requires `SFL Reviewer Gate Runner` from GitHub Actions App ID -`15368` and uses strict base freshness, so a base update needs a new sealed -review. The runner authenticates the exact App review plus the inner -`SFL Reviewer Approval` and `SFL Review Evidence` checks. This differs from -Relias because `HemSoft` is a personal GitHub account and cannot create -organization required-workflow rulesets. - -An already-correct gate is a write-free no-op. Before changing a stale dedicated -gate, the command rechecks its entity tag and complete state, then aborts if -either changed. It verifies the write and restores the exact pre-write rule if -GitHub accepts an invalid update that remains unchanged before rollback. GitHub -does not support conditional headers on repository ruleset updates or deletes. -The command refuses stale shared or inherited rules instead of changing -unrelated policy. Gate creation, status, and uninstall use repository endpoints -and do not require `admin:org`. The command still rejects public or non-HemSoft -repositories. - -`gh sfl stop` pauses review dispatch and recovery, but the required gate runner -continues to execute and fails closed. New pull request heads remain blocked -until `gh sfl start` re-enables reviews. +The retired Kimi/OpenRouter reviewer ran a prescribed three-pass, +full-spectrum prompt, assigned Critical/High/Medium/Low severities, managed its +own SFL review threads, and retried missing output once. + +Native Codex owns review depth, severity, and presentation. SFL now verifies +Codex provenance and head freshness and translates its clean/finding result +into the existing branch gate. It does not promise three passes, all severity +classes, SFL-authored approvals, obsolete-thread cleanup, or recovery retries. +A rerun is an explicit new `gh sfl review --retry` request. A normal retry is +rejected until the latest request has a request-specific terminal SFL gate. +Retries after a successful terminal gate are rejected; push a new commit so the +old success cannot satisfy the required check while another review is pending. +For an edited or overlapping request that can never receive its own gate, the +CLI uses the durable request registry and waits up to two minutes for the exact +Codex `eyes` reaction to materialize and clear. It then waits beyond GitHub's +one-second timestamp boundary before posting the replacement. This prevents a +delayed artifact from an older request from consuming the replacement's gate. + +Pilot deployments are limited to HemSoft/hs-buddy until the source change and +smoke evidence are accepted. Do not deploy this migration to +developer-documentation. diff --git a/gh-sfl/README.md b/gh-sfl/README.md index a599ebeb..fdb8827d 100644 --- a/gh-sfl/README.md +++ b/gh-sfl/README.md @@ -35,7 +35,7 @@ gh sfl init --slack-webhook "https://hooks.slack.com/..." # Enable Slack notifi | Tier | Workflows | Description | |------|-----------|-------------| -| `reviewer` | PR Reviewer source, runtime, trigger/gate, recovery | Default reviewer-only package | +| `reviewer` | Subscription-backed Codex observer and gate | Default reviewer-only package | | `minimal` | Dispatcher | Legacy label routing only | | `standard` | Dispatcher, Processor, Review Reactor | Full quality loop | | `full` | Standard + Repo Audit, Simplisticate Audit, Simplisticate PR | Everything | @@ -74,8 +74,8 @@ gh sfl gate --repo owner/repo The command creates one repository ruleset for the default branch. It requires `SFL Reviewer Gate Runner` from GitHub Actions App ID `15368` and enables strict -base freshness. The runner authenticates the exact App review plus the inner -`SFL Reviewer Approval` and `SFL Review Evidence` checks. An already-correct +base freshness. The observer authenticates the native Codex result and writes +the runner check on the exact reviewed head. An already-correct gate causes no write. A stale dedicated SFL gate is updated with its current entity tag and state rechecked immediately before the write. The command aborts if either changed, verifies the result, and restores the pre-write snapshot if @@ -84,9 +84,9 @@ does not support conditional headers on repository ruleset updates or deletes. The command refuses to rewrite a shared or inherited rule. It does not use organization endpoints or require the `admin:org` scope. -When `gh sfl stop` enables maintenance mode, automatic review dispatch pauses -but the required gate runner still executes and fails closed. New pull request -heads cannot pass the gate until `gh sfl start` re-enables reviews. +When `gh sfl stop` enables maintenance mode, the observer ignores Codex result +events, so new pull request heads remain blocked by the absent required gate. +`gh sfl start` re-enables observation. ### `gh sfl uninstall` diff --git a/gh-sfl/add.go b/gh-sfl/add.go index d8ae6387..05888a4c 100644 --- a/gh-sfl/add.go +++ b/gh-sfl/add.go @@ -88,6 +88,10 @@ func runAdd(args []string, stdout io.Writer, stderr io.Writer) error { return err } } + defaultBranch, err := getDefaultBranch(owner, repo) + if err != nil { + return fmt.Errorf("getting default branch: %w", err) + } fileMap := make(map[string]string) fmt.Fprintf(stdout, " Fetching %d workflow file(s)...\n", len(workflows)) @@ -97,6 +101,10 @@ func runAdd(args []string, stdout io.Writer, stderr io.Writer) error { if fetchErr != nil { return fmt.Errorf("fetching %s: %w", srcPath, fetchErr) } + content, prepareErr := prepareWorkflowSource(wf, content, manifest.SourceSHA, owner+"/"+repo, defaultBranch) + if prepareErr != nil { + return prepareErr + } rendered, renderErr := renderHemSoftWorkflow(wf, content, manifest.Version) if renderErr != nil { return fmt.Errorf("applying HemSoft engine policy to %s: %w", wf, renderErr) @@ -120,11 +128,6 @@ func runAdd(args []string, stdout io.Writer, stderr io.Writer) error { fileMap[".sfl/sfl.json"] = manifestJSON // Deploy - defaultBranch, err := getDefaultBranch(owner, repo) - if err != nil { - return fmt.Errorf("getting default branch: %w", err) - } - commitMsg := fmt.Sprintf("chore: add SFL add-on %q\n\nDeployed by gh-sfl add", opts.addon) fmt.Fprintf(stdout, "\n") var prURL string @@ -143,15 +146,9 @@ func runAdd(args []string, stdout io.Writer, stderr io.Writer) error { fmt.Fprintf(stdout, "\n✅ Add-on %q deployed to %s/%s\n", opts.addon, owner, repo) - // Only suggest compilation if the addon doesn't ship a precompiled lock file - hasLock := false - for _, wf := range workflows { - if strings.HasSuffix(wf, ".lock.yml") { - hasLock = true - break - } - } - if !hasLock { + // Only Markdown-source workflows need gh-aw compilation. Standard Actions + // YAML files are already executable and do not have lock-file companions. + if addonNeedsCompilation(workflows) { fmt.Fprintf(stdout, "\n ⚠ Note: This workflow is deployed as source (.md) only.\n") fmt.Fprintf(stdout, " Run 'gh aw compile' in the target repo to generate the lock file.\n") } @@ -159,6 +156,16 @@ func runAdd(args []string, stdout io.Writer, stderr io.Writer) error { return nil } +func addonNeedsCompilation(workflows []string) bool { + hasMarkdownSource := false + hasLock := false + for _, workflow := range workflows { + hasMarkdownSource = hasMarkdownSource || strings.HasSuffix(workflow, ".md") + hasLock = hasLock || strings.HasSuffix(workflow, ".lock.yml") + } + return hasMarkdownSource && !hasLock +} + func parseAddOptions(args []string, stderr io.Writer) (addOptions, error) { opts := addOptions{pr: true} var direct bool @@ -214,7 +221,7 @@ Usage: gh sfl add [flags] Available Add-ons: - pr-review Evidence-based PR review with recovery and a zero-finding approval gate + pr-review Subscription-backed Codex review with an immutable-head gate Flags: -R, --repo string Target repository (OWNER/REPO). Defaults to current repo. diff --git a/gh-sfl/addons.go b/gh-sfl/addons.go index c7600ae6..56d653eb 100644 --- a/gh-sfl/addons.go +++ b/gh-sfl/addons.go @@ -4,15 +4,12 @@ import "sort" var addonWorkflows = map[string][]string{ "pr-review": { - "sfl-pr-review.md", - "sfl-pr-review.lock.yml", "sfl-pr-review-auto.yml", - "sfl-pr-review-recovery.yml", }, } var addonDescriptions = map[string]string{ - "pr-review": "Automatic evidence-based PR review with recovery and a zero-finding approval gate", + "pr-review": "Subscription-backed Codex review with an authenticated immutable-head gate", } func validAddon(name string) bool { diff --git a/gh-sfl/comment_command_fixture_test.go b/gh-sfl/comment_command_fixture_test.go deleted file mode 100644 index 49b4167d..00000000 --- a/gh-sfl/comment_command_fixture_test.go +++ /dev/null @@ -1,199 +0,0 @@ -package main - -import ( - "encoding/base64" - "encoding/json" - "os" - "os/exec" - "path/filepath" - "strings" - "testing" -) - -func TestCommentCommandLookupFixtures(t *testing.T) { - bash := reviewerFixtureBash(t) - source, err := os.ReadFile( - filepath.Join("..", "deployment", "infrastructure", "sfl-pr-review-auto.yml"), - ) - if err != nil { - t.Fatalf("read review auto trigger workflow: %v", err) - } - text := strings.ReplaceAll(string(source), "\r\n", "\n") - const begin = "# BEGIN TESTABLE COMMENT COMMAND LOOKUP" - const end = "# END TESTABLE COMMENT COMMAND LOOKUP" - start := strings.Index(text, begin) - finish := strings.Index(text, end) - if start < 0 || finish <= start { - t.Fatal("comment command lookup helper block is missing") - } - helper := text[start+len(begin) : finish] - - script := `#!/usr/bin/env bash -set -euo pipefail -` + helper + ` -case "$1" in - acknowledgement) find_command_acknowledgement "$2" ;; - acknowledgement-complete) command_acknowledgement_complete ;; - acknowledgement-run-id) command_acknowledgement_run_id ;; - run-matches) command_run_matches "$2" "$3" ;; - run) find_command_run "$2" "$3" ;; - *) exit 2 ;; -esac -` - scriptPath := filepath.Join(t.TempDir(), "comment-command.sh") - if err := os.WriteFile(scriptPath, []byte(script), 0o700); err != nil { - t.Fatalf("write comment command fixture: %v", err) - } - scriptArgument := filepath.ToSlash(scriptPath) - if volume := filepath.VolumeName(scriptPath); volume != "" { - scriptArgument = "/" + - strings.ToLower(strings.TrimSuffix(volume, ":")) + - strings.TrimPrefix(scriptArgument, filepath.ToSlash(volume)) - } - - t.Run("only app acknowledgement establishes idempotency", func(t *testing.T) { - input := `[{"id":9,"user":{"login":"someone"},"body":" forged"},` + - `{"id":10,"user":{"login":"sfl-app[bot]"},"body":" pending"}]` - cmd := exec.Command(bash, scriptArgument, "acknowledgement", "") - cmd.Stdin = strings.NewReader(input) - output, err := cmd.CombinedOutput() - if err != nil { - t.Fatalf("execute acknowledgement fixture: %v\n%s", err, output) - } - decoded, err := base64.StdEncoding.DecodeString(strings.TrimSpace(string(output))) - if err != nil { - t.Fatalf("decode acknowledgement: %v", err) - } - var comment struct { - ID int `json:"id"` - } - if err := json.Unmarshal(decoded, &comment); err != nil { - t.Fatalf("parse acknowledgement: %v", err) - } - if comment.ID != 10 { - t.Fatalf("acknowledgement id=%d, want 10", comment.ID) - } - }) - - t.Run("missing acknowledgement remains empty", func(t *testing.T) { - cmd := exec.Command(bash, scriptArgument, "acknowledgement", "") - cmd.Stdin = strings.NewReader(`[{"id":9,"user":{"login":"someone"},"body":"unrelated"}]`) - output, err := cmd.CombinedOutput() - if err != nil { - t.Fatalf("execute empty acknowledgement fixture: %v\n%s", err, output) - } - if got := strings.TrimSpace(string(output)); got != "" { - t.Fatalf("acknowledgement=%q, want empty", got) - } - }) - - t.Run("only final actions run marker completes acknowledgement", func(t *testing.T) { - for _, test := range []struct { - name string - body string - want bool - }{ - { - name: "pending acknowledgement", - body: `{"body":" pending"}`, - }, - { - name: "timeout command run link", - body: `{"body":" [Command run](https://github.com/acme/repo/actions/runs/123)"}`, - }, - { - name: "final actions run marker", - body: `{"body":" [Actions run 456](https://github.com/acme/repo/actions/runs/456)"}`, - want: true, - }, - } { - t.Run(test.name, func(t *testing.T) { - cmd := exec.Command(bash, scriptArgument, "acknowledgement-complete") - cmd.Stdin = strings.NewReader(test.body) - err := cmd.Run() - if got := err == nil; got != test.want { - t.Fatalf("complete=%t, want %t (err=%v)", got, test.want, err) - } - }) - } - }) - - t.Run("final acknowledgement preserves exact run id", func(t *testing.T) { - cmd := exec.Command(bash, scriptArgument, "acknowledgement-run-id") - cmd.Stdin = strings.NewReader( - `{"body":" [Actions run 456](https://github.com/acme/repo/actions/runs/456)"}`, - ) - output, err := cmd.CombinedOutput() - if err != nil { - t.Fatalf("extract acknowledgement run id: %v\n%s", err, output) - } - if got := strings.TrimSpace(string(output)); got != "456" { - t.Fatalf("acknowledgement run id=%q, want 456", got) - } - }) - - t.Run("acknowledged run must match sealed base and head", func(t *testing.T) { - const title = "SFL PR Review #42 aaa111:bbb222 retry=" - for _, test := range []struct { - name string - run string - want bool - }{ - { - name: "matching command run", - run: `{"path":".github/workflows/sfl-pr-review.lock.yml","event":"workflow_dispatch","display_title":"SFL PR Review #42 aaa111:bbb222 retry=0 dispatch=command-1234"}`, - want: true, - }, - { - name: "stale head", - run: `{"path":".github/workflows/sfl-pr-review.lock.yml","event":"workflow_dispatch","display_title":"SFL PR Review #42 aaa111:old999 retry=0 dispatch=command-1234"}`, - }, - { - name: "wrong dispatch", - run: `{"path":".github/workflows/sfl-pr-review.lock.yml","event":"workflow_dispatch","display_title":"SFL PR Review #42 aaa111:bbb222 retry=0 dispatch=auto-100"}`, - }, - } { - t.Run(test.name, func(t *testing.T) { - cmd := exec.Command(bash, scriptArgument, "run-matches", title, "command-1234") - cmd.Stdin = strings.NewReader(test.run) - err := cmd.Run() - if got := err == nil; got != test.want { - t.Fatalf("matches=%t, want %t (err=%v)", got, test.want, err) - } - }) - } - }) - - t.Run("run lookup seals command dispatch id", func(t *testing.T) { - const title = "SFL PR Review #42 aaa111:bbb222 retry=" - input := `{"workflow_runs":[` + - `{"id":100,"display_title":"SFL PR Review #42 aaa111:bbb222 retry=0 dispatch=auto-100"},` + - `{"id":102,"display_title":"SFL PR Review #42 aaa111:bbb222 retry=1 dispatch=recovery-102"},` + - `{"id":101,"display_title":"SFL PR Review #42 aaa111:bbb222 retry=0 dispatch=command-1234"}]}` - cmd := exec.Command(bash, scriptArgument, "run", title, "command-1234") - cmd.Stdin = strings.NewReader(input) - output, err := cmd.CombinedOutput() - if err != nil { - t.Fatalf("execute run lookup fixture: %v\n%s", err, output) - } - if got := strings.TrimSpace(string(output)); got != "101" { - t.Fatalf("run=%q, want 101", got) - } - }) - - t.Run("unrelated same-head runs remain unmatched", func(t *testing.T) { - const title = "SFL PR Review #42 aaa111:bbb222 retry=" - input := `{"workflow_runs":[` + - `{"id":100,"display_title":"SFL PR Review #42 aaa111:bbb222 retry=0 dispatch=auto-100"},` + - `{"id":102,"display_title":"SFL PR Review #42 aaa111:bbb222 retry=1 dispatch=recovery-102"}]}` - cmd := exec.Command(bash, scriptArgument, "run", title, "command-1234") - cmd.Stdin = strings.NewReader(input) - output, err := cmd.CombinedOutput() - if err != nil { - t.Fatalf("execute unmatched run fixture: %v\n%s", err, output) - } - if got := strings.TrimSpace(string(output)); got != "" { - t.Fatalf("run=%q, want empty", got) - } - }) -} diff --git a/gh-sfl/concurrency_group_fixture_test.go b/gh-sfl/concurrency_group_fixture_test.go deleted file mode 100644 index e5de3c21..00000000 --- a/gh-sfl/concurrency_group_fixture_test.go +++ /dev/null @@ -1,61 +0,0 @@ -package main - -import ( - "fmt" - "os" - "path/filepath" - "strings" - "testing" -) - -func TestReviewTriggerConcurrencyGroups(t *testing.T) { - source, err := os.ReadFile( - filepath.Join("..", "deployment", "infrastructure", "sfl-pr-review-auto.yml"), - ) - if err != nil { - t.Fatalf("read review auto trigger workflow: %v", err) - } - - const group = "group: sfl-pr-review-auto-${{ github.event.pull_request.number || github.event.issue.number }}-${{ github.event.pull_request.head.sha || github.event.comment.id }}-${{ github.event_name }}-${{ github.event.action }}-${{ github.event.action == 'labeled' && github.event.label.name == 'sfl-review' && 'explicit' || 'standard' }}" - if !strings.Contains(normalizeLineEndings(source), group) { - t.Fatal("automatic reviewer does not isolate required runs, callbacks, heads, and explicit labels") - } - - groupFor := func(pr, head, event, action, label string) string { - suffix := "standard" - if action == "labeled" && label == "sfl-review" { - suffix = "explicit" - } - return fmt.Sprintf("sfl-pr-review-auto-%s-%s-%s-%s-%s", pr, head, event, action, suffix) - } - - required := groupFor("78", "head-a", "pull_request_target", "synchronize", "") - distinct := map[string]string{ - "prior head": groupFor("78", "head-b", "pull_request_target", "synchronize", ""), - "review callback": groupFor("78", "head-a", "pull_request_review", "submitted", ""), - "edited event": groupFor("78", "head-a", "pull_request_target", "edited", ""), - "explicit label": groupFor("78", "head-a", "pull_request_target", "labeled", "sfl-review"), - "ordinary label": groupFor("78", "head-a", "pull_request_target", "labeled", "sfl-effort:high"), - } - for name, candidate := range distinct { - if candidate == required { - t.Fatalf("%s group %q can evict required group %q", name, candidate, required) - } - } - if distinct["explicit label"] == distinct["ordinary label"] { - t.Fatal("explicit review trigger shares concurrency with ordinary labels") - } - if duplicate := groupFor("78", "head-a", "pull_request_target", "synchronize", ""); duplicate != required { - t.Fatalf("duplicate required group=%q, want %q", duplicate, required) - } - commentGroupFor := func(issue, commentID string) string { - return fmt.Sprintf("sfl-pr-review-auto-%s-%s-issue_comment-created-standard", issue, commentID) - } - command := commentGroupFor("78", "1234") - if duplicate := commentGroupFor("78", "1234"); duplicate != command { - t.Fatalf("duplicate command group=%q, want %q", duplicate, command) - } - if anotherComment := commentGroupFor("78", "1235"); anotherComment == command { - t.Fatal("distinct comment commands share a concurrency group") - } -} diff --git a/gh-sfl/deployment_test.go b/gh-sfl/deployment_test.go index 4fba1be4..41178e29 100644 --- a/gh-sfl/deployment_test.go +++ b/gh-sfl/deployment_test.go @@ -410,6 +410,60 @@ func TestParseInitOptionsRegistersAndDeduplicatesAddons(t *testing.T) { } } +func TestAddonCompilationGuidanceOnlyAppliesToMarkdownSource(t *testing.T) { + for _, test := range []struct { + name string + workflows []string + want bool + }{ + {name: "standard Actions YAML", workflows: []string{"sfl-pr-review-auto.yml"}, want: false}, + {name: "Markdown source", workflows: []string{"repo-audit.md"}, want: true}, + {name: "precompiled Markdown", workflows: []string{"repo-audit.md", "repo-audit.lock.yml"}, want: false}, + } { + t.Run(test.name, func(t *testing.T) { + if got := addonNeedsCompilation(test.workflows); got != test.want { + t.Fatalf("addonNeedsCompilation(%v) = %v, want %v", test.workflows, got, test.want) + } + }) + } +} + +func TestReviewerAddonRequiresSubscriptionBackedSource(t *testing.T) { + if _, err := prepareWorkflowSource( + "sfl-pr-review-auto.yml", + "name: SFL PR Review Auto Trigger", + strings.Repeat("a", 40), + "HemSoft/consumer", + "main", + ); err == nil || !strings.Contains(err.Error(), "predates") || + !strings.Contains(err.Error(), strings.Repeat("a", 40)) { + t.Fatalf("legacy reviewer source error = %v", err) + } + + content := reviewerSourcePlaceholder + "\nname: SFL Codex Review Observer\non:\n push:\n" + reviewerPushBranchPlaceholder + "\nenv:\n" + reviewerBaseBranchPlaceholder + "\nif: github.event.sender.id == 199175422\n" + prepared, err := prepareWorkflowSource("sfl-pr-review-auto.yml", content, strings.Repeat("b", 40), "HemSoft/consumer", "release/next") + if err != nil { + t.Fatalf("subscription-backed reviewer source rejected: %v", err) + } + if strings.Contains(prepared, "@main") || !strings.Contains(prepared, "@"+strings.Repeat("b", 40)) || + !strings.HasPrefix(prepared, "# Deployed from:") || !strings.Contains(prepared, "branches: ['release/next']") || + !strings.Contains(prepared, "SFL_REVIEW_BASE_BRANCH: 'release/next'") { + t.Fatalf("prepared reviewer source lacks immutable provenance: %q", prepared) + } + if _, err := prepareWorkflowSource("repo-audit.md", "legacy content", strings.Repeat("a", 40), "HemSoft/consumer", "main"); err != nil { + t.Fatalf("non-reviewer add-on source rejected: %v", err) + } +} + +func TestReviewerSourceValidationCoversEveryDeploymentPath(t *testing.T) { + for _, file := range []string{"init.go", "sync.go", "add.go"} { + source := string(readContractFile(t, file)) + if !strings.Contains(source, "prepareWorkflowSource(wf, content,") { + t.Fatalf("%s does not validate the selected reviewer source", file) + } + } +} + func TestParseAddOptionsDefaultsToPullRequest(t *testing.T) { opts, err := parseAddOptions([]string{"pr-review"}, io.Discard) if err != nil { @@ -650,12 +704,12 @@ func TestInstalledManifestWorkflowResolutionFailsClosed(t *testing.T) { custom, err := workflowsForInstalledManifest(&sflManifest{ Tier: "custom", - Components: []string{"repo-audit", "sfl-pr-review"}, + Components: []string{"repo-audit", "sfl-pr-review", "sfl-pr-review-auto"}, }) if err != nil { t.Fatalf("custom workflow resolution: %v", err) } - for _, want := range []string{"repo-audit.md", "sfl-pr-review.md", "sfl-pr-review.lock.yml"} { + for _, want := range []string{"repo-audit.md", "sfl-pr-review-auto.yml"} { if !slices.Contains(custom, want) { t.Errorf("custom workflows missing %q: %v", want, custom) } @@ -671,6 +725,36 @@ func TestInstalledManifestWorkflowResolutionFailsClosed(t *testing.T) { } } +func TestNormalizeManifestForSyncRemovesRetiredReviewerComponents(t *testing.T) { + for _, tier := range []string{"review", "reviewer"} { + t.Run(tier, func(t *testing.T) { + manifest := &sflManifest{ + Tier: tier, + Components: []string{"sfl-pr-review", "sfl-pr-review-auto", "sfl-pr-review-recovery"}, + } + normalizeManifestForSync(manifest, canonicalDeploymentTier(manifest.Tier)) + if manifest.Tier != "reviewer" { + t.Fatalf("normalized tier = %q, want reviewer", manifest.Tier) + } + if !slices.Equal(manifest.Components, tierComponents["reviewer"]) { + t.Fatalf("normalized components = %v, want %v", manifest.Components, tierComponents["reviewer"]) + } + }) + } + + full := &sflManifest{Tier: "full", Components: []string{"sfl-pr-review", "sfl-pr-review-recovery"}} + normalizeManifestForSync(full, canonicalDeploymentTier(full.Tier)) + if !slices.Equal(full.Components, []string{"sfl-pr-review-auto"}) { + t.Fatalf("full components did not migrate to the observer component: %v", full.Components) + } + + custom := &sflManifest{Tier: "custom", Components: []string{"repo-audit", "sfl-pr-review", "sfl-pr-review-recovery"}} + normalizeManifestForSync(custom, canonicalDeploymentTier(custom.Tier)) + if !slices.Equal(custom.Components, []string{"repo-audit", "sfl-pr-review-auto"}) { + t.Fatalf("custom components = %v, want legacy reviewer migrated to observer", custom.Components) + } +} + func TestSyncChecksManagedFilesWhenSourceRevisionIsCurrent(t *testing.T) { source := string(readContractFile(t, "sync.go")) if !strings.Contains(source, "verifying managed files for drift") { @@ -703,10 +787,7 @@ func TestSyncReportsNonFatalLabelSourceFailures(t *testing.T) { func TestReviewerTierContainsOnlyReviewerPackage(t *testing.T) { want := []string{ - "sfl-pr-review.md", - "sfl-pr-review.lock.yml", "sfl-pr-review-auto.yml", - "sfl-pr-review-recovery.yml", } if !slices.Equal(tierWorkflows["reviewer"], want) { t.Errorf("reviewer workflows = %v, want %v", tierWorkflows["reviewer"], want) @@ -725,9 +806,6 @@ func TestReviewerHealthUsesManifestPackageAndVersionMarkers(t *testing.T) { } want := []string{ "sfl-pr-review-auto.yml", - "sfl-pr-review-recovery.yml", - "sfl-pr-review.lock.yml", - "sfl-pr-review.md", } if got := expectedWorkflowFiles(manifest); !slices.Equal(got, want) { t.Errorf("expectedWorkflowFiles() = %v, want %v", got, want) @@ -741,7 +819,7 @@ func TestReviewerHealthUsesManifestPackageAndVersionMarkers(t *testing.T) { func TestReviewerHealthCoversEveryTierContainingReviewer(t *testing.T) { custom := &sflManifest{ Tier: "custom", - Components: []string{"labels", "sfl-pr-review", "sfl-pr-review-auto", "sfl-pr-review-recovery"}, + Components: []string{"labels", "sfl-pr-review", "sfl-pr-review-auto"}, } for name, manifest := range map[string]*sflManifest{ "reviewer": {Tier: "reviewer"}, @@ -754,9 +832,6 @@ func TestReviewerHealthCoversEveryTierContainingReviewer(t *testing.T) { } want := []string{ "sfl-pr-review-auto.yml", - "sfl-pr-review-recovery.yml", - "sfl-pr-review.lock.yml", - "sfl-pr-review.md", } if got := expectedReviewerWorkflowFiles(manifest); !slices.Equal(got, want) { t.Fatalf("expectedReviewerWorkflowFiles(%s) = %v, want %v", name, got, want) @@ -772,9 +847,6 @@ func TestReviewerHealthCoversEveryTierContainingReviewer(t *testing.T) { } wantAddon := []string{ "sfl-pr-review-auto.yml", - "sfl-pr-review-recovery.yml", - "sfl-pr-review.lock.yml", - "sfl-pr-review.md", } if got := expectedReviewerWorkflowFiles(addonManifest); !slices.Equal(got, wantAddon) { t.Fatalf("reviewer add-on files = %v, want %v", got, wantAddon) @@ -1089,9 +1161,6 @@ func TestInspectReviewerRolloutReportsEveryDistinctFault(t *testing.T) { "repository has no default branch", "GitHub Actions is disabled", "Actions policy allows only local actions", - "missing Actions variable SFL_APP_CLIENT_ID", - "missing Actions secret SFL_APP_PRIVATE_KEY", - "missing Actions secret OPENROUTER_API_KEY", } { if !strings.Contains(joined, want) { t.Errorf("rollout issues missing %q:\n%s", want, joined) @@ -1099,35 +1168,19 @@ func TestInspectReviewerRolloutReportsEveryDistinctFault(t *testing.T) { } } -func TestReviewerCredentialMetadataPaginatesWithoutReadingValues(t *testing.T) { - firstVariables := make([]string, 100) - firstSecrets := make([]string, 100) - for index := range firstVariables { - firstVariables[index] = fmt.Sprintf("VARIABLE_%03d", index) - firstSecrets[index] = fmt.Sprintf("SECRET_%03d", index) - } +func TestReviewerPreflightDoesNotReadCredentialMetadata(t *testing.T) { client := &fakeREST{ repositoryDefaultBranch: "main", actionsEnabled: true, actionsAllowed: "all", - actionVariablePages: [][]string{firstVariables, {"SFL_APP_CLIENT_ID"}}, - actionSecretPages: [][]string{firstSecrets, {"SFL_APP_PRIVATE_KEY", "OPENROUTER_API_KEY"}}, } health, err := inspectReviewerRolloutWithClient(client, "owner", "repo") if err != nil || len(health.issues()) != 0 { t.Fatalf("paginated rollout health = %+v, %v", health, err) } joinedRequests := strings.Join(client.gets, "\n") - for _, want := range []string{ - "actions/variables?per_page=100&page=2", - "actions/secrets?per_page=100&page=2", - } { - if !strings.Contains(joinedRequests, want) { - t.Errorf("metadata requests missing %q: %v", want, client.gets) - } - } - if strings.Contains(joinedRequests, "values") { - t.Errorf("preflight requested credential values: %v", client.gets) + if strings.Contains(joinedRequests, "/actions/variables") || strings.Contains(joinedRequests, "/actions/secrets") { + t.Errorf("subscription-backed reviewer inspected API credentials: %v", client.gets) } } @@ -1136,8 +1189,8 @@ func TestReviewerActionsSelectedPolicyRequiresGitHubOwnedActions(t *testing.T) { githubOwned bool wantHealthy bool }{ - "GitHub owned": {githubOwned: true, wantHealthy: true}, - "exact patterns are deliberately unsupported": {}, + "GitHub owned": {githubOwned: true, wantHealthy: true}, + "GitHub owned disabled": {}, } for name, tc := range cases { t.Run(name, func(t *testing.T) { @@ -1151,10 +1204,7 @@ func TestReviewerActionsSelectedPolicyRequiresGitHubOwnedActions(t *testing.T) { if tc.wantHealthy && len(issues) != 0 { t.Fatalf("healthy selected policy issues = %v", issues) } - if !tc.wantHealthy && !slices.Contains( - issues, - "selected Actions policy must allow GitHub-owned actions required by the reviewer; exact action patterns are not a supported rollout policy", - ) { + if !tc.wantHealthy && !slices.Contains(issues, "selected Actions policy must allow GitHub-owned actions for the reviewer") { t.Fatalf("blocked selected policy issues = %v", issues) } }) @@ -1164,7 +1214,7 @@ func TestReviewerActionsSelectedPolicyRequiresGitHubOwnedActions(t *testing.T) { func TestAssertReviewerRolloutReadyFailsBeforeAnyWrite(t *testing.T) { client := &fakeREST{ repositoryDefaultBranch: "main", - actionsEnabled: true, + actionsEnabled: false, actionsAllowed: "all", actionVariablePages: [][]string{{}}, actionSecretPages: [][]string{{}}, @@ -1174,7 +1224,7 @@ func TestAssertReviewerRolloutReadyFailsBeforeAnyWrite(t *testing.T) { t.Cleanup(func() { newRESTClient = oldClient }) err := assertReviewerRolloutReady("owner", "repo", io.Discard) - if err == nil || !strings.Contains(err.Error(), "missing Actions variable SFL_APP_CLIENT_ID") { + if err == nil || !strings.Contains(err.Error(), "GitHub Actions is disabled") { t.Fatalf("assertReviewerRolloutReady() error = %v", err) } if len(client.posts)+len(client.puts)+len(client.patches)+len(client.deletes) != 0 { @@ -1202,8 +1252,8 @@ func TestAssertReviewerRolloutReadyDoesNotClaimAppVerification(t *testing.T) { t.Fatalf("preflight falsely reported App verification:\n%s", output.String()) } for _, want := range []string{ - "Actions policy, variable, and secret metadata ready", - "rely on the App-authenticated credential bootstrap check", + "default branch main and Actions policy ready", + "verify with a current-head review after deployment", } { if !strings.Contains(output.String(), want) { t.Errorf("preflight output missing %q:\n%s", want, output.String()) @@ -1223,15 +1273,12 @@ func TestPrintReviewerPrerequisitesSeparatesCredentialFaults(t *testing.T) { var output bytes.Buffer printReviewerPrerequisites(&output, newTableStyler(&output, false), health, nil) for _, want := range []string{ - "Reviewer App:", + "Codex App:", "App scope requires App-authenticated credential bootstrap verification", - "Reviewer credentials and Actions:", + "Reviewer prerequisites:", "Default branch: main", "GitHub Actions disabled", "Actions policy allows only local actions", - "Missing Actions variable SFL_APP_CLIENT_ID", - "Missing Actions secret SFL_APP_PRIVATE_KEY", - "Missing Actions secret OPENROUTER_API_KEY", } { if !strings.Contains(output.String(), want) { t.Errorf("status output missing %q:\n%s", want, output.String()) @@ -1320,7 +1367,7 @@ func TestUninstallUsesManifestTierWithoutExpandingUnknownTiers(t *testing.T) { if err != nil { t.Fatalf("uninstallFiles(review): %v", err) } - if !slices.Contains(legacyReview, ".github/workflows/sfl-pr-review.md") { + if !slices.Contains(legacyReview, ".github/workflows/sfl-pr-review-auto.yml") { t.Errorf("legacy review uninstall is missing reviewer workflow: %v", legacyReview) } if slices.Contains(legacyReview, ".github/workflows/pr-fixer.md") { @@ -1568,7 +1615,6 @@ func TestMutatingCommandsRejectProtectedRepositoryBeforeAccess(t *testing.T) { {name: "sync", run: runSync, args: func(target string) []string { return []string{"--repo", target} }}, {name: "gate", run: runGate, args: func(target string) []string { return []string{"--repo", target} }}, {name: "add", run: runAdd, args: func(target string) []string { return []string{"--repo", target, "policy-manager"} }}, - {name: "review", run: runReview, args: func(target string) []string { return []string{"--repo", target, "1"} }}, {name: "uninstall", run: runUninstall, args: func(target string) []string { return []string{"--repo", target, "--force"} }}, {name: "start", run: runStart, args: func(target string) []string { return []string{"--repo", target} }}, {name: "stop", run: runStop, args: func(target string) []string { return []string{"--repo", target} }}, @@ -2090,24 +2136,7 @@ func TestDeployViaPullRequestSkipsCurrentOpenPR(t *testing.T) { } func TestDeployViaPullRequestSkipsCurrentDefaultBranch(t *testing.T) { - reviewerWorkflow, err := applyHemSoftEnginePolicyToWorkflow( - string(readContractFile(t, filepath.Join("..", "deployment", "workflows", "sfl-pr-review.md"))), - "sfl-pr-review", - ) - if err != nil { - t.Fatalf("normalize reviewer workflow: %v", err) - } - rewrittenReviewerWorkflow, err := applyHemSoftEnginePolicyToWorkflow(reviewerWorkflow, "sfl-pr-review") - if err != nil || rewrittenReviewerWorkflow != reviewerWorkflow { - firstLines := strings.Split(reviewerWorkflow, "\n") - secondLines := strings.Split(rewrittenReviewerWorkflow, "\n") - for index := 0; index < min(len(firstLines), len(secondLines)); index++ { - if firstLines[index] != secondLines[index] { - t.Fatalf("reviewer workflow rewrite differs at line %d: first=%q second=%q err=%v", index+1, firstLines[index], secondLines[index], err) - } - } - t.Fatalf("reviewer workflow rewrite length differs: first=%d second=%d err=%v", len(firstLines), len(secondLines), err) - } + reviewerWorkflow := string(readContractFile(t, filepath.Join("..", "deployment", "infrastructure", "sfl-pr-review-auto.yml"))) currentManifest := `{ "version": "6.5.0", "tier": "reviewer", @@ -2129,7 +2158,7 @@ func TestDeployViaPullRequestSkipsCurrentDefaultBranch(t *testing.T) { rest := &fakeREST{ manifestContent: currentManifest, fileContents: map[string]string{ - ".github/workflows/sfl-pr-review.md": reviewerWorkflow, + ".github/workflows/sfl-pr-review-auto.yml": reviewerWorkflow, }, } graphQL := &fakeGraphQL{} @@ -2142,8 +2171,8 @@ func TestDeployViaPullRequestSkipsCurrentDefaultBranch(t *testing.T) { "main", "init", map[string]string{ - ".sfl/sfl.json": desiredManifest, - ".github/workflows/sfl-pr-review.md": reviewerWorkflow, + ".sfl/sfl.json": desiredManifest, + ".github/workflows/sfl-pr-review-auto.yml": reviewerWorkflow, }, "init", true, @@ -2278,13 +2307,13 @@ func TestDeployViaPullRequestRemovesStaleManagedWorkflows(t *testing.T) { } func TestDeployViaPullRequestRemovesLegacyGovernanceFromReviewerInstall(t *testing.T) { - reviewerWorkflow := string(readContractFile(t, filepath.Join("..", "deployment", "workflows", "sfl-pr-review.md"))) + reviewerWorkflow := string(readContractFile(t, filepath.Join("..", "deployment", "infrastructure", "sfl-pr-review-auto.yml"))) manifest := `{ "version": "6.5.1", "tier": "reviewer", "source": "HemSoft/set-it-free-loop", "sourceSha": "source-sha", - "components": ["sfl-pr-review", "sfl-pr-review-auto", "sfl-pr-review-recovery"] + "components": ["sfl-pr-review", "sfl-pr-review-auto"] }` governancePath := ".sfl/governance/policy.md" rest := &fakeREST{ @@ -2292,8 +2321,8 @@ func TestDeployViaPullRequestRemovesLegacyGovernanceFromReviewerInstall(t *testi openPRBranch: "sfl/init-existing", manifestContent: manifest, fileContents: map[string]string{ - ".github/workflows/sfl-pr-review.md": reviewerWorkflow, - governancePath: "legacy policy", + ".github/workflows/sfl-pr-review-auto.yml": reviewerWorkflow, + governancePath: "legacy policy", }, } graphQL := &fakeGraphQL{} @@ -2305,8 +2334,8 @@ func TestDeployViaPullRequestRemovesLegacyGovernanceFromReviewerInstall(t *testi "main", "init", map[string]string{ - ".sfl/sfl.json": manifest, - ".github/workflows/sfl-pr-review.md": reviewerWorkflow, + ".sfl/sfl.json": manifest, + ".github/workflows/sfl-pr-review-auto.yml": reviewerWorkflow, }, "deploy reviewer", true, diff --git a/gh-sfl/dispatch_dedup_fixture_test.go b/gh-sfl/dispatch_dedup_fixture_test.go deleted file mode 100644 index e3652545..00000000 --- a/gh-sfl/dispatch_dedup_fixture_test.go +++ /dev/null @@ -1,149 +0,0 @@ -package main - -import ( - "os" - "os/exec" - "path/filepath" - "strings" - "testing" -) - -// The dispatch dedup filter in sfl-pr-review-auto.yml decides whether an -// automatic trigger dispatches a review. These fixtures execute the shipped -// jq against representative workflow-runs payloads. -func TestDispatchDedupFixtures(t *testing.T) { - bash := reviewerFixtureBash(t) - - source, err := os.ReadFile( - filepath.Join("..", "deployment", "infrastructure", "sfl-pr-review-auto.yml"), - ) - if err != nil { - t.Fatalf("read review auto trigger workflow: %v", err) - } - text := strings.ReplaceAll(string(source), "\r\n", "\n") - const begin = "# BEGIN TESTABLE DISPATCH DEDUP" - const end = "# END TESTABLE DISPATCH DEDUP" - start := strings.Index(text, begin) - finish := strings.Index(text, end) - if start < 0 || finish <= start { - t.Fatal("dispatch dedup helper block is missing") - } - helper := text[start+len(begin) : finish] - - script := `#!/usr/bin/env bash -set -euo pipefail -` + helper + ` -if [ "${4:-false}" = "true" ]; then - produce_large_input() { - local i - for ((i = 0; i < 131072; i++)); do - printf x - done - } - produce_large_input | find_reusable_review_run "$1" "$3" -else - find_reusable_review_run "$1" "$3" -fi -` - scriptPath := filepath.Join(t.TempDir(), "dispatch-dedup.sh") - if err := os.WriteFile(scriptPath, []byte(script), 0o700); err != nil { - t.Fatalf("write dispatch dedup fixture: %v", err) - } - scriptArgument := filepath.ToSlash(scriptPath) - if volume := filepath.VolumeName(scriptPath); volume != "" { - scriptArgument = "/" + - strings.ToLower(strings.TrimSuffix(volume, ":")) + - strings.TrimPrefix(scriptArgument, filepath.ToSlash(volume)) - } - - const expectedTitle = "SFL PR Review #42 aaa111:bbb222 retry=" - for _, fixture := range []struct { - name string - action string - label string - pipe string - input string - want string - }{ - { - name: "matching active run suppresses dispatch", - action: "synchronize", - label: "false", - pipe: "false", - input: `{"total_count":2,"workflow_runs":[ - {"id":101,"display_title":"SFL PR Review #42 aaa111:bbb222 retry=0"}, - {"id":102,"display_title":"SFL PR Review #99 ccc333:ddd444 retry=0"} -]}`, - want: "101", - }, - { - name: "unrelated runs allow dispatch", - action: "opened", - label: "false", - pipe: "false", - input: `{"total_count":2,"workflow_runs":[ - {"id":101,"display_title":"SFL PR Review #99 ccc333:ddd444 retry=0"}, - {"id":102,"display_title":"SFL PR Review #41 eee555:fff666 retry=1"} -]}`, - want: "", - }, - { - name: "empty page allows dispatch", - action: "ready_for_review", - label: "false", - pipe: "false", - input: `{"total_count":0,"workflow_runs":[]}`, - want: "", - }, - { - name: "same PR but different head allows dispatch", - action: "synchronize", - label: "false", - pipe: "false", - input: `{"total_count":1,"workflow_runs":[{"id":101,"display_title":"SFL PR Review #42 aaa111:ccc999 retry=0"}]}`, - want: "", - }, - { - name: "live explicit request bypasses active run", - action: "labeled", - label: "true", - pipe: "false", - input: `{"total_count":1,"workflow_runs":[{"id":101,"display_title":"SFL PR Review #42 aaa111:bbb222 retry=0"}]}`, - want: "", - }, - { - name: "stale labeled event uses active run", - action: "labeled", - label: "false", - pipe: "false", - input: `{"total_count":1,"workflow_runs":[{"id":101,"display_title":"SFL PR Review #42 aaa111:bbb222 retry=0"}]}`, - want: "101", - }, - { - name: "live request drains producer under pipefail", - action: "labeled", - label: "true", - pipe: "true", - want: "", - }, - } { - t.Run(fixture.name, func(t *testing.T) { - cmd := exec.Command( - bash, - scriptArgument, - expectedTitle, - fixture.action, - fixture.label, - fixture.pipe, - ) - cmd.Stdin = strings.NewReader(fixture.input) - output, err := cmd.CombinedOutput() - if err != nil { - t.Fatalf("execute dispatch dedup fixture: %v\n%s", err, output) - } - if got := strings.TrimSpace(string(output)); got != fixture.want { - t.Fatalf("run=%q, want %q", got, fixture.want) - } - }) - } -} diff --git a/gh-sfl/github.go b/gh-sfl/github.go index 2e698580..f058c0e2 100644 --- a/gh-sfl/github.go +++ b/gh-sfl/github.go @@ -116,6 +116,17 @@ func parseRepoFlag(repoFlag string) (string, string, error) { return parts[0], parts[1], nil } +// isNotFoundError reports whether an API read failed because the resource does +// not exist, rather than because of authentication, rate limiting, or network +// failure. Some go-gh errors expose only the rendered HTTP status. +func isNotFoundError(err error) bool { + return err != nil && isNotFoundMessage(err.Error()) +} + +func isNotFoundMessage(message string) bool { + return strings.Contains(message, "HTTP 404") || strings.Contains(message, "Not Found") +} + func parseMutationTarget(repoFlag string) (string, string, error) { owner, repo, err := parseRepoFlag(repoFlag) if err != nil { @@ -127,6 +138,17 @@ func parseMutationTarget(repoFlag string) (string, string, error) { return owner, repo, nil } +func parseReviewTarget(repoFlag string) (string, string, error) { + owner, repo, err := parseRepoFlag(repoFlag) + if err != nil { + return "", "", err + } + if err := validateHemSoftTarget(owner, repo); err != nil { + return "", "", err + } + return owner, repo, nil +} + func validateDeploymentTarget(owner, repo string) error { if !strings.EqualFold(owner, "HemSoft") { return fmt.Errorf("%s/%s is outside the HemSoft repository scope", owner, repo) @@ -134,7 +156,13 @@ func validateDeploymentTarget(owner, repo string) error { if strings.EqualFold(repo, motherRepoName) { return fmt.Errorf("%s/%s is protected and cannot be targeted by SFL deployment operations", owner, repo) } + return validateHemSoftTarget(owner, repo) +} +func validateHemSoftTarget(owner, repo string) error { + if !strings.EqualFold(owner, "HemSoft") { + return fmt.Errorf("%s/%s is outside the HemSoft repository scope", owner, repo) + } loginOut, loginErr, err := ghExec("api", "user", "--jq", ".login") if err != nil { return fmt.Errorf("checking GitHub CLI identity: %s: %w", loginErr.String(), err) @@ -725,6 +753,9 @@ func deploymentManifestContentsMatch(current []byte, desired string) (bool, erro // older SFL releases installed but the current catalog no longer contains. var retiredWorkflowPaths = []string{ "sfl-copilot-review-bridge.yml", + "sfl-pr-review.md", + "sfl-pr-review.lock.yml", + "sfl-pr-review-recovery.yml", } func managedDeploymentPaths() []string { diff --git a/gh-sfl/hemsoft.go b/gh-sfl/hemsoft.go index 33b801d5..467743c8 100644 --- a/gh-sfl/hemsoft.go +++ b/gh-sfl/hemsoft.go @@ -8,7 +8,7 @@ import ( "sync" ) -const hemSoftEnginePolicyJSON = "{\r\n \"$schema\": \"./engine-policy.schema.json\",\r\n \"defaultProfile\": \"codex-gpt-55-high\",\r\n \"profiles\": {\r\n \"codex-gpt-55-high\": {\r\n \"provider\": \"codex\",\r\n \"model\": \"gpt-5.5\",\r\n \"effort\": \"high\",\r\n \"requiredSecretsAnyOf\": [\r\n \"CODEX_API_KEY\",\r\n \"OPENAI_API_KEY\"\r\n ]\r\n },\r\n \"openrouter-kimi-k3-high\": {\r\n \"provider\": \"copilot\",\r\n \"model\": \"moonshotai/kimi-k3\",\r\n \"requiredSecretsAnyOf\": [\r\n \"OPENROUTER_API_KEY\"\r\n ],\r\n \"environment\": {\r\n \"COPILOT_PROVIDER_BASE_URL\": \"https://openrouter.ai/api/v1\",\r\n \"COPILOT_PROVIDER_API_KEY\": \"${{ secrets.OPENROUTER_API_KEY }}\",\r\n \"COPILOT_PROVIDER_TYPE\": \"openai\",\r\n \"COPILOT_PROVIDER_WIRE_API\": \"responses\",\r\n \"COPILOT_MODEL\": \"moonshotai/kimi-k3\"\r\n }\r\n }\r\n },\r\n \"workflows\": {\r\n \"sfl-pr-review\": {\r\n \"profile\": \"openrouter-kimi-k3-high\"\r\n }\r\n }\r\n}\r\n" +const hemSoftEnginePolicyJSON = "{\r\n \"$schema\": \"./engine-policy.schema.json\",\r\n \"defaultProfile\": \"codex-gpt-55-high\",\r\n \"profiles\": {\r\n \"codex-gpt-55-high\": {\r\n \"provider\": \"codex\",\r\n \"model\": \"gpt-5.5\",\r\n \"effort\": \"high\",\r\n \"requiredSecretsAnyOf\": [\r\n \"CODEX_API_KEY\",\r\n \"OPENAI_API_KEY\"\r\n ]\r\n }\r\n },\r\n \"workflows\": {}\r\n}\r\n" type hemSoftEnginePolicy struct { DefaultProfile string `json:"defaultProfile"` @@ -361,15 +361,41 @@ func mergeHemSoftEnginePolicyManifest( func sourceWorkflowPath(name string) string { switch name { - case "sfl-dispatcher.yml", "sfl-auditor.yml", "sfl-pr-review-auto.yml", "sfl-pr-review-recovery.yml": + case "sfl-dispatcher.yml", "sfl-auditor.yml", "sfl-pr-review-auto.yml": return "deployment/infrastructure/" + name - case "sfl-pr-review.lock.yml": - return ".github/workflows/" + name default: return "deployment/workflows/" + name } } +const reviewerSourcePlaceholder = "# Source: HemSoft/set-it-free-loop/deployment/infrastructure/sfl-pr-review-auto.yml@main" +const reviewerPushBranchPlaceholder = " branches: [main]" +const reviewerBaseBranchPlaceholder = " SFL_REVIEW_BASE_BRANCH: main" + +func prepareWorkflowSource(workflow, content, sourceSHA, targetRepo, defaultBranch string) (string, error) { + if workflow != "sfl-pr-review-auto.yml" { + return content, nil + } + if !strings.Contains(content, "name: SFL Codex Review Observer") || + !strings.Contains(content, "github.event.sender.id == 199175422") || + !strings.Contains(content, reviewerSourcePlaceholder) || + !strings.Contains(content, reviewerPushBranchPlaceholder) || + !strings.Contains(content, reviewerBaseBranchPlaceholder) { + return "", fmt.Errorf("the SFL source %s selected for %s predates the subscription-backed Codex reviewer; deploy or sync from a release containing SFL Codex Review Observer", sourceSHA, targetRepo) + } + if strings.TrimSpace(defaultBranch) == "" { + return "", fmt.Errorf("preparing the SFL Codex observer for %s: target default branch is empty", targetRepo) + } + sourceRef := "HemSoft/set-it-free-loop/" + sourceWorkflowPath(workflow) + "@" + sourceSHA + content = strings.Replace(content, reviewerSourcePlaceholder, "# Source: "+sourceRef, 1) + escapedBranch := strings.ReplaceAll(defaultBranch, "'", "''") + content = strings.Replace(content, reviewerPushBranchPlaceholder, " branches: ['"+escapedBranch+"']", 1) + content = strings.Replace(content, reviewerBaseBranchPlaceholder, " SFL_REVIEW_BASE_BRANCH: '"+escapedBranch+"'", 1) + prefix := "# Deployed from: " + sourceRef + "\n" + + "# To upgrade: re-run deploy-workflow.ps1 at the desired SHA\n" + return prefix + content, nil +} + func renderHemSoftWorkflow(name, content, sflVersion string) (string, error) { rendered := renderWorkflow(content, sflVersion) if !strings.HasSuffix(name, ".md") { diff --git a/gh-sfl/hemsoft_test.go b/gh-sfl/hemsoft_test.go index 809b8587..d5bae912 100644 --- a/gh-sfl/hemsoft_test.go +++ b/gh-sfl/hemsoft_test.go @@ -36,30 +36,6 @@ func TestHemSoftEnginePolicyRewriteIsIdempotent(t *testing.T) { } } -func TestCanonicalReviewerEnginePolicyIsDeploymentStable(t *testing.T) { - canonical := normalizeLineEndings(readContractFile( - t, - filepath.Join("..", "deployment", "workflows", "sfl-pr-review.md"), - )) - - rendered, err := applyHemSoftEnginePolicyToWorkflow(canonical, "sfl-pr-review") - if err != nil { - t.Fatalf("render canonical reviewer workflow: %v", err) - } - if rendered != canonical { - firstDifference := 0 - for firstDifference < len(canonical) && firstDifference < len(rendered) && canonical[firstDifference] == rendered[firstDifference] { - firstDifference++ - } - t.Fatalf( - "canonical reviewer workflow changes during consumer deployment, which invalidates its generated lock: canonical bytes=%d, rendered bytes=%d, first difference=%d", - len(canonical), - len(rendered), - firstDifference, - ) - } -} - func TestRenderHemSoftWorkflowMatchesDeploymentAndStatus(t *testing.T) { markdown := "---\nname: Example\nmodel: legacy\nnetwork: defaults\n---\nVersion " + sflVersionPlaceholder + "\n" rendered, err := renderHemSoftWorkflow("example.md", markdown, "2.0.0") @@ -81,19 +57,16 @@ func TestRenderHemSoftWorkflowMatchesDeploymentAndStatus(t *testing.T) { } } -func TestHemSoftReviewerUsesPrivateOpenRouterProfile(t *testing.T) { - config, err := hemSoftEngineConfigForWorkflow("sfl-pr-review") +func TestHemSoftEnginePolicyHasNoOpenRouterReviewerProfile(t *testing.T) { + policy, err := hemSoftEnginePolicyConfig() if err != nil { - t.Fatalf("resolve reviewer engine: %v", err) + t.Fatalf("load engine policy: %v", err) } - if config.Profile != "openrouter-kimi-k3-high" || config.Provider != "copilot" { - t.Fatalf("reviewer engine = %+v", config) + if _, exists := policy.Workflows["sfl-pr-review"]; exists { + t.Fatal("native Codex reviewer still has a gh-aw engine mapping") } - if config.Environment["COPILOT_PROVIDER_BASE_URL"] != "https://openrouter.ai/api/v1" { - t.Fatalf("reviewer base URL = %q", config.Environment["COPILOT_PROVIDER_BASE_URL"]) - } - if len(config.RequiredSecretsAnyOf) != 1 || config.RequiredSecretsAnyOf[0] != "OPENROUTER_API_KEY" { - t.Fatalf("reviewer secret requirements = %v", config.RequiredSecretsAnyOf) + if strings.Contains(hemSoftEnginePolicyJSON, "OPENROUTER") || strings.Contains(hemSoftEnginePolicyJSON, "openrouter") { + t.Fatal("embedded engine policy retained OpenRouter configuration") } } @@ -171,13 +144,29 @@ func TestValidateDeploymentTargetRejectsScopeBeforeGitHubAccess(t *testing.T) { } } +func TestValidateReviewTargetAllowsSourceRepository(t *testing.T) { + oldGHExec := ghExec + t.Cleanup(func() { ghExec = oldGHExec }) + ghExec = func(args ...string) (bytes.Buffer, bytes.Buffer, error) { + if strings.Join(args, " ") != "api user --jq .login" { + return bytes.Buffer{}, bytes.Buffer{}, fmt.Errorf("unexpected gh arguments: %v", args) + } + return *bytes.NewBufferString("HemSoft\n"), bytes.Buffer{}, nil + } + + if err := validateHemSoftTarget("HemSoft", motherRepoName); err != nil { + t.Fatalf("validateHemSoftTarget() rejected source review: %v", err) + } + if err := validateDeploymentTarget("HemSoft", motherRepoName); err == nil || + !strings.Contains(err.Error(), "is protected") { + t.Fatalf("validateDeploymentTarget() source error = %v, want deployment protection", err) + } +} + func TestHemSoftWorkflowSourceRouting(t *testing.T) { for name, want := range map[string]string{ - "sfl-pr-review.md": "deployment/workflows/sfl-pr-review.md", - "sfl-pr-review.lock.yml": ".github/workflows/sfl-pr-review.lock.yml", - "sfl-pr-review-auto.yml": "deployment/infrastructure/sfl-pr-review-auto.yml", - "sfl-pr-review-recovery.yml": "deployment/infrastructure/sfl-pr-review-recovery.yml", - "repo-audit.md": "deployment/workflows/repo-audit.md", + "sfl-pr-review-auto.yml": "deployment/infrastructure/sfl-pr-review-auto.yml", + "repo-audit.md": "deployment/workflows/repo-audit.md", } { if got := sourceWorkflowPath(name); got != want { t.Errorf("sourceWorkflowPath(%q) = %q, want %q", name, got, want) @@ -196,7 +185,7 @@ func TestReviewerStatusReadsPinnedFilesThroughHemSoftRouting(t *testing.T) { if !strings.Contains(statusSource, "sourceWorkflowPath(workflow)") { t.Error("reviewer status does not resolve pinned artifacts through HemSoft source routing") } - if !strings.Contains(statusSource, "renderHemSoftWorkflow(workflow, source, manifest.Version)") { + if !strings.Contains(statusSource, "renderHemSoftWorkflow(workflow, preparedSource, manifest.Version)") { t.Error("reviewer status does not apply the same engine policy as deployment") } if strings.Contains(statusSource, `"workflows/"+workflow`) { diff --git a/gh-sfl/init.go b/gh-sfl/init.go index f2310c98..983ebd3a 100644 --- a/gh-sfl/init.go +++ b/gh-sfl/init.go @@ -47,10 +47,7 @@ func (f *stringSliceFlag) Set(val string) error { // Tier definitions: which workflow files to deploy per tier. var tierWorkflows = map[string][]string{ "reviewer": { - "sfl-pr-review.md", - "sfl-pr-review.lock.yml", "sfl-pr-review-auto.yml", - "sfl-pr-review-recovery.yml", }, "minimal": { "sfl-dispatcher.yml", @@ -76,17 +73,14 @@ var tierWorkflows = map[string][]string{ "pr-analyzer-testing.md", "pr-fixer.md", "pr-promoter.md", - "sfl-pr-review.md", - "sfl-pr-review.lock.yml", "sfl-pr-review-auto.yml", - "sfl-pr-review-recovery.yml", }, } var tierComponents = map[string][]string{ - "reviewer": {"sfl-pr-review", "sfl-pr-review-auto", "sfl-pr-review-recovery"}, + "reviewer": {"sfl-pr-review-auto"}, "minimal": {"labels", "governance", "sfl-dispatcher"}, "standard": {"labels", "governance", "sfl-dispatcher", "sfl-auditor", "daily-repo-status", "repo-audit", "issue-processor", "simplisticate"}, - "full": {"labels", "governance", "sfl-dispatcher", "sfl-auditor", "daily-repo-status", "repo-audit", "issue-processor", "simplisticate", "pr-analyzer-general", "pr-analyzer-quality", "pr-analyzer-security", "pr-analyzer-testing", "pr-fixer", "pr-promoter", "sfl-pr-review", "sfl-pr-review-auto", "sfl-pr-review-recovery"}, + "full": {"labels", "governance", "sfl-dispatcher", "sfl-auditor", "daily-repo-status", "repo-audit", "issue-processor", "simplisticate", "pr-analyzer-general", "pr-analyzer-quality", "pr-analyzer-security", "pr-analyzer-testing", "pr-fixer", "pr-promoter", "sfl-pr-review-auto"}, } func canonicalDeploymentTier(tier string) string { @@ -227,6 +221,10 @@ func runInit(args []string, stdout io.Writer, stderr io.Writer) error { if fetchErr != nil { return fmt.Errorf("fetching %s: %w", srcPath, fetchErr) } + content, prepareErr := prepareWorkflowSource(wf, content, release.SHA, owner+"/"+repo, defaultBranch) + if prepareErr != nil { + return prepareErr + } rendered, renderErr := renderHemSoftWorkflow(wf, content, release.Version) if renderErr != nil { return fmt.Errorf("applying HemSoft engine policy to %s: %w", wf, renderErr) @@ -245,6 +243,10 @@ func runInit(args []string, stdout io.Writer, stderr io.Writer) error { if fetchErr != nil { return fmt.Errorf("fetching add-on %s: %w", srcPath, fetchErr) } + content, prepareErr := prepareWorkflowSource(wf, content, release.SHA, owner+"/"+repo, defaultBranch) + if prepareErr != nil { + return prepareErr + } rendered, renderErr := renderHemSoftWorkflow(wf, content, release.Version) if renderErr != nil { return fmt.Errorf("applying HemSoft engine policy to add-on %s: %w", wf, renderErr) @@ -511,7 +513,7 @@ func writeInitUsage(w io.Writer) { fmt.Fprint(w, initUsage) } -const initUsage = `Deploy the latest synchronized SFL PR Reviewer release. +const initUsage = `Deploy the latest synchronized SFL Codex Reviewer release. Usage: gh sfl init [flags] @@ -523,7 +525,7 @@ Flags: --source-ref Deploy a specific synchronized release tag (defaults to latest) Tiers: - reviewer SFL PR Reviewer package only (default) + reviewer Subscription-backed Codex reviewer observer only (default) minimal SFL Dispatcher only (label routing) standard Dispatcher + Processor + Review Reactor (full quality loop) full Standard + Repo Audit + Simplisticate Audit + Simplisticate PR + PR Review diff --git a/gh-sfl/review.go b/gh-sfl/review.go index 92ea917a..77d73297 100644 --- a/gh-sfl/review.go +++ b/gh-sfl/review.go @@ -1,28 +1,113 @@ package main import ( - "encoding/json" + "encoding/base64" "errors" "flag" "fmt" "io" - "regexp" + "net/http" + "net/url" + "sort" "strconv" "strings" "time" - gh "github.com/cli/go-gh/v2" + "github.com/cli/go-gh/v2/pkg/api" ) +const ( + codexReviewCommand = "@codex review" + codexReviewRequestRegistryContext = "SFL Codex Review Request Registry" +) + +var waitForRetryOrdering = func() { + time.Sleep(1100 * time.Millisecond) +} + +var waitForCodexReactionPoll = func() { + time.Sleep(2 * time.Second) +} + +var waitForReviewInvalidationPoll = func() { + time.Sleep(5 * time.Second) +} + type reviewOptions struct { - repo string - pr int + repo string + pr int + retry bool } type pullRequestShas struct { - BaseSHA string - HeadSHA string - State string + BaseSHA string + BaseRef string + HeadSHA string + BaseRepo string + HeadRepo string + State string + UpdatedAt string `json:"updated_at"` +} + +type reviewWorkflowRun struct { + ID int64 `json:"id"` + Event string `json:"event"` + Status string `json:"status"` + HeadSHA string `json:"head_sha"` + HeadBranch string `json:"head_branch"` + CreatedAt string `json:"created_at"` + PullRequests []struct { + Number int `json:"number"` + Head struct { + SHA string `json:"sha"` + } `json:"head"` + Base struct { + SHA string `json:"sha"` + } `json:"base"` + } `json:"pull_requests"` +} + +type reviewTriggerComment struct { + ID int64 `json:"id"` + Body string `json:"body"` + HTMLURL string `json:"html_url"` + CreatedAt string `json:"created_at"` + UpdatedAt string `json:"updated_at"` + Registered bool `json:"-"` + User struct { + Login string `json:"login"` + } `json:"user"` +} + +type reviewLifecycleEvent struct { + ID int64 `json:"id"` + Event string `json:"event"` +} + +type reviewCommentReaction struct { + Content string `json:"content"` + User struct { + ID int64 `json:"id"` + } `json:"user"` +} + +type reviewRequestStatus struct { + Context string `json:"context"` + TargetURL string `json:"target_url"` + Creator struct { + Login string `json:"login"` + } `json:"creator"` +} + +type reviewerCheckRun struct { + ID int64 `json:"id"` + Status string `json:"status"` + Conclusion string `json:"conclusion"` + CompletedAt string `json:"completed_at"` + ExternalID string `json:"external_id"` + App struct { + ID int64 `json:"id"` + } `json:"app"` } func runReview(args []string, stdout io.Writer, stderr io.Writer) error { @@ -34,81 +119,417 @@ func runReview(args []string, stdout io.Writer, stderr io.Writer) error { return err } - owner, repo, err := parseMutationTarget(opts.repo) + owner, repo, err := parseReviewTarget(opts.repo) if err != nil { return err } - defaultBranch, err := getDefaultBranch(owner, repo) + client, err := newRESTClient() if err != nil { - return err + return fmt.Errorf("creating GitHub REST client: %w", err) } - - lockContent, err := fetchFileRaw(owner, repo, ".github/workflows/sfl-pr-review.lock.yml", "") + defaultBranch, err := fetchRepositoryDefaultBranchWithClient(client, owner, repo) if err != nil { - if isNotFoundError(err) { - return fmt.Errorf("SFL is not installed in %s/%s — run 'gh sfl init --repo %s/%s' first", owner, repo, owner, repo) - } - return fmt.Errorf("reading SFL review workflow from %s/%s: %w", owner, repo, err) + return err + } + if err := requireCodexReviewObserver(client, owner, repo, defaultBranch); err != nil { + return err + } + if err := requireReviewerEnabled(client, owner, repo); err != nil { + return err } - pr, err := fetchPullRequestShas(owner, repo, opts.pr) + pr, err := fetchPullRequestShasWithClient(client, owner, repo, opts.pr) if err != nil { return err } if pr.State != "open" { return fmt.Errorf("pull request #%d in %s/%s is %s — reviews only run on open pull requests", opts.pr, owner, repo, pr.State) } + if pr.HeadRepo == "" { + return fmt.Errorf("pull request #%d in %s/%s has no available head repository — subscription-backed SFL reviews cannot run after the source repository is deleted", opts.pr, owner, repo) + } + if !strings.EqualFold(pr.HeadRepo, owner+"/"+repo) { + return fmt.Errorf("pull request #%d in %s/%s comes from fork %s — subscription-backed SFL reviews support same-repository branches only", opts.pr, owner, repo, pr.HeadRepo) + } + if !strings.EqualFold(pr.BaseRef, defaultBranch) { + return fmt.Errorf("pull request #%d in %s/%s targets %s — subscription-backed SFL reviews support only the default branch %s", opts.pr, owner, repo, pr.BaseRef, defaultBranch) + } + initialLifecycleToken, err := fetchPullRequestLifecycleToken(client, owner, repo, opts.pr) + if err != nil { + return fmt.Errorf("reading the pull request lifecycle before waiting for SFL invalidations: %w", err) + } + if err := waitForReviewInvalidations(client, owner, repo, opts.pr, defaultBranch, pr); err != nil { + return fmt.Errorf("waiting for SFL review invalidation: %w", err) + } + confirmedDefaultBranch, err := fetchRepositoryDefaultBranchWithClient(client, owner, repo) + if err != nil { + return err + } + confirmedPR, err := fetchPullRequestShasWithClient(client, owner, repo, opts.pr) + if err != nil { + return err + } + if confirmedDefaultBranch != defaultBranch || confirmedPR.State != "open" || + confirmedPR.HeadSHA != pr.HeadSHA || confirmedPR.BaseSHA != pr.BaseSHA || + confirmedPR.BaseRef != pr.BaseRef || + !strings.EqualFold(confirmedPR.HeadRepo, owner+"/"+repo) { + return fmt.Errorf("pull request #%d context changed while SFL invalidations were settling — rerun gh sfl review for the current head and base", opts.pr) + } + stableLifecycleToken, err := fetchPullRequestLifecycleToken(client, owner, repo, opts.pr) + if err != nil { + return fmt.Errorf("revalidating the pull request lifecycle after SFL invalidations: %w", err) + } + if stableLifecycleToken != initialLifecycleToken { + return fmt.Errorf("pull request #%d was closed or reopened while SFL invalidations were settling — rerun gh sfl review for the current pull request lifecycle", opts.pr) + } - awContext := fmt.Sprintf( - `{"item_type":"pull_request","item_number":%d,"base_sha":"%s","head_sha":"%s"}`, - opts.pr, pr.BaseSHA, pr.HeadSHA, + contextToken, err := fetchReviewContextToken(client, owner, repo, pr.HeadSHA) + if err != nil { + return fmt.Errorf("reading the current SFL review context: %w", err) + } + marker := codexReviewMarker(pr.HeadSHA, pr.BaseSHA, contextToken) + conflictingURL, conflictErr := findConflictingCodexBaseRequest(client, owner, repo, opts.pr, pr.HeadSHA, codexReviewBaseMarker(pr.HeadSHA, pr.BaseSHA)) + if conflictErr != nil { + return fmt.Errorf("checking prior Codex review bases: %w", conflictErr) + } + if conflictingURL != "" { + return fmt.Errorf("pull request #%d head %.10s was already requested against another base at %s — update the pull request branch to a new head before requesting review for the current base", opts.pr, pr.HeadSHA, conflictingURL) + } + requests, ownerRequests, findErr := findCodexReviewTriggers( + client, owner, repo, opts.pr, pr.HeadSHA, marker, ) + if findErr != nil { + return fmt.Errorf("checking existing Codex review requests: %w", findErr) + } + var existing reviewTriggerComment + untrackedExisting := false + retryNeedsCompletionWait := false + retryNeedsReactionMaterialization := false + if len(requests) > 0 { + existing = requests[len(requests)-1] + } + if len(ownerRequests) > 0 { + latestOwnerRequest := ownerRequests[len(ownerRequests)-1] + if existing.ID != latestOwnerRequest.ID { + existing = latestOwnerRequest + } + } + untrackedExisting = existing.HTMLURL != "" && !existing.Registered + if existing.HTMLURL != "" { + if !opts.retry { + fmt.Fprintf(stdout, "Codex review already requested for %s/%s#%d at %.10s: %s\nUse --retry if its result event was skipped.\n", + owner, repo, opts.pr, pr.HeadSHA, existing.HTMLURL) + return nil + } + completed := false + edited := reviewCommentWasEdited(existing, bodyForReviewRequest(marker)) + if !untrackedExisting { + if edited { + successful, successfulErr := findSuccessfulTerminalGateForRequestID( + client, owner, repo, opts.pr, pr.HeadSHA, pr.BaseSHA, contextToken, existing.ID, + ) + if successfulErr != nil { + return fmt.Errorf("checking the edited Codex review result: %w", successfulErr) + } + if successful { + return fmt.Errorf("the latest Codex review request for %s/%s#%d already passed — push a new commit before requesting another review so the successful gate cannot remain valid during a retry", owner, repo, opts.pr) + } + } else { + terminal, gateCompleted, completedErr := findTerminalGateForRequest( + client, owner, repo, opts.pr, pr.HeadSHA, pr.BaseSHA, existing, + ) + if completedErr != nil { + return fmt.Errorf("checking the prior Codex review result: %w", completedErr) + } + completed = gateCompleted + if completed && strings.EqualFold(terminal.Conclusion, "success") { + return fmt.Errorf("the latest Codex review request for %s/%s#%d already passed — push a new commit before requesting another review so the successful gate cannot remain valid during a retry", owner, repo, opts.pr) + } + } + } + if untrackedExisting || edited { + retryNeedsReactionMaterialization = true + } else if !completed { + recovered, recoverable, recoveryErr := canRecoverFromOverlappingRequest( + client, owner, repo, opts.pr, pr.HeadSHA, pr.BaseSHA, requests, + ) + if recoveryErr != nil { + return fmt.Errorf("checking overlapping Codex review requests: %w", recoveryErr) + } + if !recoverable { + return fmt.Errorf("the latest Codex review request for %s/%s#%d is still outstanding — wait for its result before using --retry", owner, repo, opts.pr) + } + if strings.EqualFold(recovered.Conclusion, "success") { + return fmt.Errorf("an overlapping Codex review request for %s/%s#%d already passed — push a new commit before requesting another review so the successful gate cannot remain valid during a retry", owner, repo, opts.pr) + } + retryNeedsReactionMaterialization = true + } + retryNeedsCompletionWait = true + } + if retryNeedsCompletionWait { + if err := waitForCodexRequestCompletion( + client, owner, repo, existing.ID, retryNeedsReactionMaterialization, + ); err != nil { + return fmt.Errorf("waiting for the prior Codex review to finish: %w", err) + } + // GitHub exposes request and gate timestamps at second resolution. Waiting + // past a full boundary keeps a retry strictly ordered after the prior + // request's completion evidence, while the observer fails closed on equal times. + waitForRetryOrdering() + } + finalDefaultBranch, err := fetchRepositoryDefaultBranchWithClient(client, owner, repo) + if err != nil { + return err + } + finalPR, err := fetchPullRequestShasWithClient(client, owner, repo, opts.pr) + if err != nil { + return err + } + finalContextToken, err := fetchReviewContextToken(client, owner, repo, pr.HeadSHA) + if err != nil { + return fmt.Errorf("revalidating the SFL review context: %w", err) + } + if finalDefaultBranch != defaultBranch || finalPR.State != "open" || + finalPR.HeadSHA != pr.HeadSHA || finalPR.BaseSHA != pr.BaseSHA || + finalPR.BaseRef != pr.BaseRef || + !strings.EqualFold(finalPR.HeadRepo, owner+"/"+repo) || + finalContextToken != contextToken { + return fmt.Errorf("pull request #%d context changed before the Codex review request could be posted — rerun gh sfl review for the current head and base", opts.pr) + } + finalLifecycleToken, lifecycleErr := fetchPullRequestLifecycleToken(client, owner, repo, opts.pr) + if lifecycleErr != nil { + return fmt.Errorf("revalidating the pull request lifecycle before posting: %w", lifecycleErr) + } + if finalLifecycleToken != stableLifecycleToken { + return fmt.Errorf("pull request #%d was closed or reopened before the Codex review request could be posted — rerun gh sfl review after SFL invalidations finish", opts.pr) + } + if err := requireReviewerEnabled(client, owner, repo); err != nil { + return err + } - dispatchArgs := []string{ - "api", "--method", "POST", - fmt.Sprintf("repos/%s/%s/actions/workflows/sfl-pr-review.lock.yml/dispatches", owner, repo), - "-f", "ref=" + defaultBranch, - "-f", fmt.Sprintf("inputs[item_number]=%d", opts.pr), - "-f", "inputs[aw_context]=" + awContext, + body := bodyForReviewRequest(marker) + payload, err := jsonBody(map[string]string{"body": body}) + if err != nil { + return fmt.Errorf("encoding Codex review request: %w", err) } - // Send exactly the inputs the deployed workflow declares — unknown inputs - // are rejected with HTTP 422 and missing required inputs fail the same - // way. Detection mirrors the grep checks in sfl-pr-review-auto.yml. - capabilities := detectDispatchInputs(lockContent) - if !capabilities.baseSHA { - return fmt.Errorf( - "%s/%s runs an SFL review workflow that cannot seal the base revision;\n"+ - "a base-branch advance during the review would not be detected.\n"+ - " Run 'gh sfl sync --repo %s/%s' to upgrade to v6.3.9+, then retry.", - owner, repo, owner, repo) + var created reviewTriggerComment + if err := client.Post( + fmt.Sprintf("repos/%s/%s/issues/%d/comments", owner, repo, opts.pr), + payload, + &created, + ); err != nil { + return fmt.Errorf("requesting Codex review: %w", err) } - if capabilities.headSHA { - dispatchArgs = append(dispatchArgs, "-f", "inputs[head_sha]="+pr.HeadSHA) + if created.ID < 1 || strings.TrimSpace(created.HTMLURL) == "" { + return errors.New("GitHub created the Codex review request without returning its ID and URL") } - dispatchArgs = append(dispatchArgs, "-f", "inputs[base_sha]="+pr.BaseSHA) - if capabilities.retryCount { - dispatchArgs = append(dispatchArgs, "-f", "inputs[retry_count]=0") + if err := registerCodexReviewRequest(client, owner, repo, opts.pr, pr.HeadSHA, created); err != nil { + deletePath := fmt.Sprintf("repos/%s/%s/issues/comments/%d", owner, repo, created.ID) + if deleteErr := client.Delete(deletePath, nil); deleteErr != nil { + return fmt.Errorf("registering the Codex review request: %v; deleting unregistered request comment %d: %w", err, created.ID, deleteErr) + } + return fmt.Errorf("registering the Codex review request: %w (deleted unregistered request comment %d)", err, created.ID) } - fmt.Fprintf(stdout, "Dispatching SFL review for %s/%s#%d (head %.7s)...\n", - owner, repo, opts.pr, pr.HeadSHA) + fmt.Fprintf(stdout, "Requested subscription-backed Codex review for %s/%s#%d at %.10s: %s\n", + owner, repo, opts.pr, pr.HeadSHA, created.HTMLURL) + return nil +} - // Captured before the dispatch call: GitHub creates the run before the - // POST returns, so a slow response must not push the window past the - // run's creation time. - dispatchedAt := time.Now() - _, stderrBuf, err := gh.Exec(dispatchArgs...) +func registerCodexReviewRequest( + client restAPI, + owner, repo string, + prNumber int, + headSHA string, + request reviewTriggerComment, +) error { + payload, err := jsonBody(map[string]string{ + "state": "success", + "context": codexReviewRequestRegistryContext, + "description": fmt.Sprintf("SFL Codex request comment %d for PR #%d", request.ID, prNumber), + "target_url": request.HTMLURL, + }) if err != nil { - return fmt.Errorf("dispatching review: %s: %w", stderrBuf.String(), err) + return err } + return client.Post( + fmt.Sprintf("repos/%s/%s/statuses/%s", owner, repo, headSHA), + payload, + nil, + ) +} - if runURL := waitForReviewRun(owner, repo, opts.pr, pr.BaseSHA, pr.HeadSHA, dispatchedAt); runURL != "" { - fmt.Fprintf(stdout, " ✓ Review started: %s\n", runURL) - } else { - fmt.Fprintf(stdout, " ✓ Dispatched. Watch progress: gh sfl list --repo %s/%s\n", owner, repo) +func waitForCodexRequestCompletion( + client restAPI, + owner, repo string, + commentID int64, + requireMaterialization bool, +) error { + const ( + codexConnectorUserID = int64(199175422) + attempts = 60 + ) + seenActive := false + for attempt := 0; attempt < attempts; attempt++ { + active := false + for page := 1; ; page++ { + var reactions []reviewCommentReaction + if err := client.Get( + fmt.Sprintf("repos/%s/%s/issues/comments/%d/reactions?per_page=100&page=%d", owner, repo, commentID, page), + &reactions, + ); err != nil { + return err + } + for _, reaction := range reactions { + if reaction.User.ID == codexConnectorUserID && reaction.Content == "eyes" { + active = true + break + } + } + if active || len(reactions) < 100 { + break + } + } + seenActive = seenActive || active + if !active && (!requireMaterialization || seenActive || attempt == attempts-1) { + return nil + } + if attempt < attempts-1 { + waitForCodexReactionPoll() + } } - return nil + return errors.New("Codex still has an active review reaction on the prior request") +} + +func waitForReviewInvalidations( + client restAPI, + owner, repo string, + prNumber int, + defaultBranch string, + pr pullRequestShas, +) error { + const ( + materializationAttempts = 6 + maximumAttempts = 60 + ) + contextChangedAt := time.Time{} + if strings.TrimSpace(pr.UpdatedAt) != "" { + var err error + contextChangedAt, err = time.Parse(time.RFC3339Nano, pr.UpdatedAt) + if err != nil { + return fmt.Errorf("pull request has invalid updated_at %q: %w", pr.UpdatedAt, err) + } + } + observedApplicableRun := false + for attempt := 0; attempt < maximumAttempts; attempt++ { + runs, err := fetchActiveReviewWorkflowRuns(client, owner, repo) + if err != nil { + return err + } + active := false + for _, run := range runs { + if !reviewWorkflowRunApplies(run, prNumber, defaultBranch, pr.HeadSHA, pr.BaseSHA) { + continue + } + active = true + } + observedApplicableRun = observedApplicableRun || active + if !active && (observedApplicableRun || contextChangedAt.IsZero() || attempt >= materializationAttempts-1) { + return nil + } + if attempt < maximumAttempts-1 { + waitForReviewInvalidationPoll() + } + } + return errors.New("an applicable SFL invalidation workflow is still active") +} + +func fetchPullRequestLifecycleToken(client restAPI, owner, repo string, prNumber int) (int64, error) { + var latestID int64 + for page := 1; ; page++ { + var events []reviewLifecycleEvent + path := fmt.Sprintf("repos/%s/%s/issues/%d/events?per_page=100&page=%d", owner, repo, prNumber, page) + if err := client.Get(path, &events); err != nil { + return 0, err + } + for _, event := range events { + if (event.Event == "closed" || event.Event == "reopened") && event.ID > latestID { + latestID = event.ID + } + } + if len(events) < 100 { + return latestID, nil + } + } +} + +func fetchActiveReviewWorkflowRuns(client restAPI, owner, repo string) ([]reviewWorkflowRun, error) { + activeStatuses := []string{"requested", "queued", "in_progress", "waiting", "pending"} + runs := make([]reviewWorkflowRun, 0) + for _, status := range activeStatuses { + for page := 1; ; page++ { + var response struct { + Runs []reviewWorkflowRun `json:"workflow_runs"` + } + path := fmt.Sprintf( + "repos/%s/%s/actions/workflows/sfl-pr-review-auto.yml/runs?status=%s&per_page=100&page=%d", + owner, + repo, + status, + page, + ) + if err := client.Get(path, &response); err != nil { + return nil, err + } + runs = append(runs, response.Runs...) + if len(response.Runs) < 100 { + break + } + } + } + return runs, nil +} + +func reviewWorkflowRunApplies( + run reviewWorkflowRun, + prNumber int, + defaultBranch, headSHA, baseSHA string, +) bool { + if run.Status == "completed" { + return false + } + if run.Event == "push" { + return strings.EqualFold(run.HeadBranch, defaultBranch) && strings.EqualFold(run.HeadSHA, baseSHA) + } + if run.Event != "pull_request_target" { + return false + } + for _, pull := range run.PullRequests { + if pull.Number == prNumber && strings.EqualFold(pull.Head.SHA, headSHA) && + strings.EqualFold(pull.Base.SHA, baseSHA) { + return true + } + } + return false +} + +func requireReviewerEnabled(client restAPI, owner, repo string) error { + var variable struct { + Value string `json:"value"` + } + err := client.Get(fmt.Sprintf("repos/%s/%s/actions/variables/SFL_ENABLED", owner, repo), &variable) + if err == nil { + if strings.EqualFold(strings.TrimSpace(variable.Value), "false") { + return fmt.Errorf("SFL is stopped in %s/%s — run 'gh sfl start --repo %s/%s' before requesting a review", owner, repo, owner, repo) + } + return nil + } + var httpErr *api.HTTPError + if errors.As(err, &httpErr) && httpErr.StatusCode == http.StatusNotFound { + return nil + } + return fmt.Errorf("checking SFL maintenance mode in %s/%s: %w", owner, repo, err) } func parseReviewOptions(args []string, stderr io.Writer) (reviewOptions, error) { @@ -120,6 +541,8 @@ func parseReviewOptions(args []string, stderr io.Writer) (reviewOptions, error) flags.StringVar(&opts.repo, "repo", "", "Target repository (OWNER/REPO)") flags.StringVar(&opts.repo, "R", "", "Target repository (OWNER/REPO)") + flags.IntVar(&opts.pr, "pr", 0, "Pull request number") + flags.BoolVar(&opts.retry, "retry", false, "Post another request for the current head") if err := flags.Parse(args); err != nil { if errors.Is(err, flag.ErrHelp) { @@ -128,121 +551,474 @@ func parseReviewOptions(args []string, stderr io.Writer) (reviewOptions, error) return opts, err } - if flags.NArg() != 1 { - return opts, errors.New("exactly one pull request number is required") + if flags.NArg() > 1 || (flags.NArg() == 1 && opts.pr != 0) { + return opts, errors.New("specify the pull request number exactly once") + } + if flags.NArg() == 1 { + number, err := strconv.Atoi(strings.TrimPrefix(flags.Arg(0), "#")) + if err != nil || number < 1 { + return opts, fmt.Errorf("invalid pull request number %q", flags.Arg(0)) + } + opts.pr = number } - number, err := strconv.Atoi(strings.TrimPrefix(flags.Arg(0), "#")) - if err != nil || number < 1 { - return opts, fmt.Errorf("invalid pull request number %q", flags.Arg(0)) + if opts.pr < 1 { + return opts, errors.New("a pull request number is required") } - opts.pr = number return opts, nil } -func fetchPullRequestShas(owner, repo string, number int) (pullRequestShas, error) { - var result pullRequestShas - stdoutBuf, stderrBuf, err := gh.Exec( - "api", fmt.Sprintf("repos/%s/%s/pulls/%d", owner, repo, number), - "--jq", `{base: .base.sha, head: .head.sha, state: .state}`, +func requireCodexReviewObserver(client restAPI, owner, repo, defaultBranch string) error { + var workflow struct { + Content string `json:"content"` + Encoding string `json:"encoding"` + } + err := client.Get( + fmt.Sprintf("repos/%s/%s/contents/.github/workflows/sfl-pr-review-auto.yml", owner, repo), + &workflow, ) + if err == nil { + if workflow.Encoding != "base64" { + return fmt.Errorf("checking the SFL Codex observer in %s/%s: unsupported content encoding %q", owner, repo, workflow.Encoding) + } + decoded, decodeErr := base64.StdEncoding.DecodeString(strings.ReplaceAll(workflow.Content, "\n", "")) + if decodeErr != nil { + return fmt.Errorf("checking the SFL Codex observer in %s/%s: %w", owner, repo, decodeErr) + } + content := string(decoded) + yamlBranch := strings.ReplaceAll(defaultBranch, "'", "''") + pushFilterValid := strings.Contains(content, " branches: ['"+yamlBranch+"']") || + strings.Contains(content, " branches: ["+defaultBranch+"]") + baseEnvironmentValid := strings.Contains(content, " SFL_REVIEW_BASE_BRANCH: '"+yamlBranch+"'") || + strings.Contains(content, " SFL_REVIEW_BASE_BRANCH: "+defaultBranch) + if strings.Contains(content, "name: SFL Codex Review Observer") && + strings.Contains(content, "github.event.sender.id == 199175422") && + pushFilterValid && baseEnvironmentValid { + var workflow struct { + State string `json:"state"` + } + if stateErr := client.Get( + fmt.Sprintf("repos/%s/%s/actions/workflows/sfl-pr-review-auto.yml", owner, repo), + &workflow, + ); stateErr != nil { + return fmt.Errorf("checking the SFL Codex observer state in %s/%s: %w", owner, repo, stateErr) + } + if workflow.State != "active" { + return fmt.Errorf("the subscription-backed SFL reviewer is %s in %s/%s — enable sfl-pr-review-auto.yml before requesting a Codex review", workflow.State, owner, repo) + } + return nil + } + return fmt.Errorf("%s/%s has a retired or stale SFL reviewer deployment — run 'gh sfl sync --repo %s/%s' before requesting a Codex review", owner, repo, owner, repo) + } + var httpErr *api.HTTPError + if errors.As(err, &httpErr) && httpErr.StatusCode == http.StatusNotFound { + return fmt.Errorf("the subscription-backed SFL reviewer is not installed in %s/%s — run 'gh sfl sync --repo %s/%s' first", owner, repo, owner, repo) + } + return fmt.Errorf("checking the SFL Codex observer in %s/%s: %w", owner, repo, err) +} + +func fetchPullRequestShasWithClient(client restAPI, owner, repo string, number int) (pullRequestShas, error) { + var response struct { + Base struct { + SHA string `json:"sha"` + Ref string `json:"ref"` + Repo struct { + FullName string `json:"full_name"` + } `json:"repo"` + } `json:"base"` + Head struct { + SHA string `json:"sha"` + Repo struct { + FullName string `json:"full_name"` + } `json:"repo"` + } `json:"head"` + State string `json:"state"` + UpdatedAt string `json:"updated_at"` + } + err := client.Get(fmt.Sprintf("repos/%s/%s/pulls/%d", owner, repo, number), &response) if err != nil { - if isNotFoundMessage(stderrBuf.String()) { - return result, fmt.Errorf("pull request #%d not found in %s/%s", number, owner, repo) + var httpErr *api.HTTPError + if errors.As(err, &httpErr) && httpErr.StatusCode == http.StatusNotFound { + return pullRequestShas{}, fmt.Errorf("pull request #%d not found in %s/%s", number, owner, repo) } - return result, fmt.Errorf("reading pull request #%d in %s/%s: %s", - number, owner, repo, strings.TrimSpace(stderrBuf.String())) + return pullRequestShas{}, fmt.Errorf("reading pull request #%d in %s/%s: %w", number, owner, repo, err) } + return pullRequestShas{ + BaseSHA: response.Base.SHA, + BaseRef: response.Base.Ref, + HeadSHA: response.Head.SHA, + BaseRepo: response.Base.Repo.FullName, + HeadRepo: response.Head.Repo.FullName, + State: response.State, + UpdatedAt: response.UpdatedAt, + }, nil +} - var parsed struct { - Base string `json:"base"` - Head string `json:"head"` - State string `json:"state"` +func fetchRepositoryDefaultBranchWithClient(client restAPI, owner, repo string) (string, error) { + var response struct { + DefaultBranch string `json:"default_branch"` + } + if err := client.Get(fmt.Sprintf("repos/%s/%s", owner, repo), &response); err != nil { + return "", fmt.Errorf("reading the default branch for %s/%s: %w", owner, repo, err) } - if err := json.Unmarshal(stdoutBuf.Bytes(), &parsed); err != nil { - return result, fmt.Errorf("parsing pull request #%d: %w", number, err) + if strings.TrimSpace(response.DefaultBranch) == "" { + return "", fmt.Errorf("repository %s/%s has no default branch", owner, repo) } - result.BaseSHA = parsed.Base - result.HeadSHA = parsed.Head - result.State = parsed.State - return result, nil + return response.DefaultBranch, nil +} + +func codexReviewMarker(headSHA, baseSHA, contextToken string) string { + return "" +} + +func bodyForReviewRequest(marker string) string { + return codexReviewCommand + "\n\n" + marker +} + +func codexReviewHeadMarker(headSHA string) string { + return "") + if end < 1 { + return "", false + } + return body[start : start+end], true +} + +func findTerminalGateForRequest( + client restAPI, + owner, repo string, + prNumber int, + headSHA, baseSHA string, + request reviewTriggerComment, +) (reviewerCheckRun, bool, error) { + requestTime := reviewCommentTime(request) + if requestTime.IsZero() { + return reviewerCheckRun{}, false, errors.New("latest Codex review request has no valid timestamp") + } + contextToken, ok := reviewContextTokenFromBody(request.Body) + if !ok { + return reviewerCheckRun{}, false, errors.New("latest Codex review request has no valid context token") + } + expected := fmt.Sprintf( + "sfl-codex-review:pull:%d:base:%s:context:%s:request:%d:at:%d", + prNumber, + strings.ToLower(baseSHA), + url.QueryEscape(contextToken), + request.ID, + requestTime.UnixMilli(), + ) + for page := 1; ; page++ { + var response struct { + CheckRuns []reviewerCheckRun `json:"check_runs"` + } + if err := client.Get( + fmt.Sprintf("repos/%s/%s/commits/%s/check-runs?check_name=SFL%%20Reviewer%%20Gate%%20Runner&filter=all&per_page=100&page=%d", owner, repo, headSHA, page), + &response, + ); err != nil { + return reviewerCheckRun{}, false, err + } + for _, check := range response.CheckRuns { + if check.App.ID == 15368 && check.Status == "completed" && + (check.ExternalID == expected || strings.HasPrefix(check.ExternalID, expected+":artifact:")) { + return check, true, nil + } + } + if len(response.CheckRuns) < 100 { + return reviewerCheckRun{}, false, nil + } + } +} + +func findSuccessfulTerminalGateForRequestID( + client restAPI, + owner, repo string, + prNumber int, + headSHA, baseSHA, contextToken string, + requestID int64, +) (bool, error) { + prefix := fmt.Sprintf( + "sfl-codex-review:pull:%d:base:%s:context:%s:request:%d:at:", + prNumber, + strings.ToLower(baseSHA), + url.QueryEscape(contextToken), + requestID, + ) + for page := 1; ; page++ { + var response struct { + CheckRuns []reviewerCheckRun `json:"check_runs"` + } + if err := client.Get( + fmt.Sprintf("repos/%s/%s/commits/%s/check-runs?check_name=SFL%%20Reviewer%%20Gate%%20Runner&filter=all&per_page=100&page=%d", owner, repo, headSHA, page), + &response, + ); err != nil { + return false, err + } + for _, check := range response.CheckRuns { + if check.App.ID == 15368 && check.Status == "completed" && + strings.EqualFold(check.Conclusion, "success") && + strings.HasPrefix(check.ExternalID, prefix) { + return true, nil + } + } + if len(response.CheckRuns) < 100 { + return false, nil + } + } +} + +func canRecoverFromOverlappingRequest( + client restAPI, + owner, repo string, + prNumber int, + headSHA, baseSHA string, + requests []reviewTriggerComment, +) (reviewerCheckRun, bool, error) { + if len(requests) < 2 { + return reviewerCheckRun{}, false, nil + } + latestTime := reviewCommentTime(requests[len(requests)-1]) + if latestTime.IsZero() { + return reviewerCheckRun{}, false, errors.New("latest Codex review request has no valid timestamp") + } + for index := len(requests) - 2; index >= 0; index-- { + check, found, err := findTerminalGateForRequest( + client, owner, repo, prNumber, headSHA, baseSHA, requests[index], + ) if err != nil { + return reviewerCheckRun{}, false, err + } + if !found { continue } - marker := fmt.Sprintf("#%d ", prNumber) - for _, r := range runs { - if strings.Contains(strings.ToLower(r.WorkflowName), "sfl pr review") && - r.CreatedAt.After(dispatchedAt.Add(-5*time.Second)) && - strings.Contains(r.DisplayTitle, marker) && - strings.Contains(r.DisplayTitle, baseSHA) && - strings.Contains(r.DisplayTitle, headSHA) { - return r.URL - } + completedAt, err := time.Parse(time.RFC3339Nano, check.CompletedAt) + if err != nil { + return reviewerCheckRun{}, false, fmt.Errorf("terminal gate for prior request %d has invalid completed_at: %w", requests[index].ID, err) } + return check, !completedAt.Before(latestTime), nil } - return "" + return reviewerCheckRun{}, false, nil } func writeReviewUsage(w io.Writer) { fmt.Fprint(w, reviewUsage) } -const reviewUsage = `Trigger a full-spectrum SFL review of a pull request. +const reviewUsage = `Request a subscription-backed Codex review of a pull request. -Resolves the pull request's base and head revisions and dispatches the SFL -PR Review workflow at the repository's default branch, sealing the review to -the exact current revisions. +Posts one authenticated @codex review request for the exact current head. The +deployed SFL observer validates the Codex result and publishes the required +SFL Reviewer Gate Runner check on that head. Usage: gh sfl review [flags] + gh sfl review [flags] --pr Flags: -R, --repo string Target repository (OWNER/REPO). Defaults to current repo. + --pr int Pull request number. + --retry Post another request when a prior result event was skipped. Examples: - gh sfl review 94 # Review PR #94 in the current repo - gh sfl review --repo owner/repo 94 # Review PR #94 in another repo + gh sfl review 94 + gh sfl review --repo HemSoft/hs-buddy --pr 94 + gh sfl review --repo HemSoft/hs-buddy --pr 94 --retry ` diff --git a/gh-sfl/review_effort_fixture_test.go b/gh-sfl/review_effort_fixture_test.go deleted file mode 100644 index 684b5ef9..00000000 --- a/gh-sfl/review_effort_fixture_test.go +++ /dev/null @@ -1,77 +0,0 @@ -package main - -import ( - "os" - "os/exec" - "path/filepath" - "strings" - "testing" -) - -func TestReviewEffortResolutionFixtures(t *testing.T) { - bash := reviewerFixtureBash(t) - workflows := []string{"sfl-pr-review-auto.yml"} - - for _, workflow := range workflows { - t.Run(workflow, func(t *testing.T) { - source, err := os.ReadFile(filepath.Join("..", "deployment", "infrastructure", workflow)) - if err != nil { - t.Fatalf("read %s: %v", workflow, err) - } - text := strings.ReplaceAll(string(source), "\r\n", "\n") - const begin = "# BEGIN TESTABLE REVIEW EFFORT" - const end = "# END TESTABLE REVIEW EFFORT" - start := strings.Index(text, begin) - finish := strings.Index(text, end) - if start < 0 || finish <= start { - t.Fatalf("%s review effort helper block is missing", workflow) - } - helper := text[start+len(begin) : finish] - - script := `#!/usr/bin/env bash -set -euo pipefail -` + helper + ` -resolve_review_effort "$(cat)" -` - scriptPath := filepath.Join(t.TempDir(), "review-effort.sh") - if err := os.WriteFile(scriptPath, []byte(script), 0o700); err != nil { - t.Fatalf("write review effort fixture: %v", err) - } - scriptArgument := filepath.ToSlash(scriptPath) - if volume := filepath.VolumeName(scriptPath); volume != "" { - scriptArgument = "/" + - strings.ToLower(strings.TrimSuffix(volume, ":")) + - strings.TrimPrefix(scriptArgument, filepath.ToSlash(volume)) - } - - for _, fixture := range []struct { - name string - labels string - want string - }{ - {name: "no label defaults low", want: "low"}, - {name: "low", labels: "sfl-effort:low", want: "low"}, - {name: "medium", labels: "sfl-effort:medium", want: "medium"}, - {name: "high", labels: "sfl-effort:high", want: "high"}, - { - name: "highest effort wins", - labels: "sfl-effort:low\nsfl-effort:high\nsfl-effort:medium", - want: "high", - }, - {name: "unrelated labels default low", labels: "sfl-review\nrisk:high", want: "low"}, - } { - t.Run(fixture.name, func(t *testing.T) { - cmd := exec.Command(bash, scriptArgument) - cmd.Stdin = strings.NewReader(fixture.labels) - output, err := cmd.CombinedOutput() - if err != nil { - t.Fatalf("execute review effort fixture: %v\n%s", err, output) - } - if got := strings.TrimSpace(string(output)); got != fixture.want { - t.Fatalf("effort=%q, want %q", got, fixture.want) - } - }) - } - }) - } -} diff --git a/gh-sfl/review_recovery_fixture_test.go b/gh-sfl/review_recovery_fixture_test.go deleted file mode 100644 index 9bcf85d2..00000000 --- a/gh-sfl/review_recovery_fixture_test.go +++ /dev/null @@ -1,257 +0,0 @@ -package main - -import ( - "os" - "os/exec" - "path/filepath" - "strings" - "testing" -) - -func TestReviewRecoveryPolicyFixtures(t *testing.T) { - bash := "" - if programFiles := os.Getenv("ProgramFiles"); programFiles != "" { - gitBash := filepath.Join(programFiles, "Git", "bin", "bash.exe") - if _, err := os.Stat(gitBash); err == nil { - bash = gitBash - } - } - if bash == "" { - var err error - bash, err = exec.LookPath("bash") - if err != nil { - t.Fatal("bash is required to execute review recovery fixtures") - } - } - - source, err := os.ReadFile( - filepath.Join("..", "deployment", "infrastructure", "sfl-pr-review-recovery.yml"), - ) - if err != nil { - t.Fatalf("read review recovery workflow: %v", err) - } - text := strings.ReplaceAll(string(source), "\r\n", "\n") - const begin = "# BEGIN TESTABLE REVIEW RECOVERY POLICY" - const end = "# END TESTABLE REVIEW RECOVERY POLICY" - start := strings.Index(text, begin) - finish := strings.Index(text, end) - if start < 0 || finish <= start { - t.Fatal("review recovery policy helper block is missing") - } - helper := text[start+len(begin) : finish] - - script := `#!/usr/bin/env bash -set -u -` + helper + ` -status=0 -review_count=$(reconcile_review_count "$2" "$3") -decision=$(decide_review_recovery "$1" "$review_count" "$4" "$5" "$6" "$7" "$8" "$9") || status=$? -printf '%s:%s\n' "$decision" "$status" -` - scriptPath := filepath.Join(t.TempDir(), "review-recovery.sh") - if err := os.WriteFile(scriptPath, []byte(script), 0o700); err != nil { - t.Fatalf("write review recovery fixture: %v", err) - } - scriptArgument := filepath.ToSlash(scriptPath) - if volume := filepath.VolumeName(scriptPath); volume != "" { - scriptArgument = "/" + - strings.ToLower(strings.TrimSuffix(volume, ":")) + - strings.TrimPrefix(scriptArgument, filepath.ToSlash(volume)) - } - - head := strings.Repeat("a", 40) - otherHead := strings.Repeat("b", 40) - base := strings.Repeat("c", 40) - otherBase := strings.Repeat("d", 40) - for _, fixture := range []struct { - name string - args []string - output string - }{ - { - name: "valid review output", - args: []string{"0", "1", "1", "0", head, head, base, base, "true"}, - output: "complete:0", - }, - { - name: "published review recovers missing manifest", - args: []string{"0", "0", "1", "0", head, head, base, base, "true"}, - output: "complete:0", - }, - { - name: "first missing output retries", - args: []string{"0", "0", "0", "0", head, head, base, base, "true"}, - output: "retry:0", - }, - { - name: "second missing output fails closed", - args: []string{"0", "0", "0", "1", head, head, base, base, "true"}, - output: "exhausted:1", - }, - { - name: "explicit incomplete output does not retry", - args: []string{"1", "0", "0", "0", head, head, base, base, "true"}, - output: "reported:0", - }, - { - name: "partial durable output retries", - args: []string{"0", "0", "0", "0", head, head, base, base, "true"}, - output: "retry:0", - }, - { - name: "changed head suppresses stale retry", - args: []string{"0", "0", "0", "0", head, otherHead, base, base, "true"}, - output: "stale:0", - }, - { - name: "changed base suppresses stale retry", - args: []string{"0", "0", "0", "0", head, head, base, otherBase, "true"}, - output: "stale:0", - }, - { - name: "ineligible pull request suppresses retry", - args: []string{"0", "0", "0", "0", head, head, base, base, "false"}, - output: "stale:0", - }, - { - name: "multiple reviews fail closed", - args: []string{"0", "0", "2", "0", head, head, base, base, "true"}, - output: "invalid:1", - }, - } { - t.Run(fixture.name, func(t *testing.T) { - args := append([]string{scriptArgument}, fixture.args...) - cmd := exec.Command(bash, args...) - output, err := cmd.CombinedOutput() - if err != nil { - t.Fatalf("execute review recovery fixture: %v\n%s", err, output) - } - if got := strings.TrimSpace(string(output)); got != fixture.output { - t.Fatalf("decision=%q, want %q", got, fixture.output) - } - }) - } -} - -func TestNewerRunSuppressionFixtures(t *testing.T) { - bash := "" - if programFiles := os.Getenv("ProgramFiles"); programFiles != "" { - gitBash := filepath.Join(programFiles, "Git", "bin", "bash.exe") - if _, err := os.Stat(gitBash); err == nil { - bash = gitBash - } - } - if bash == "" { - var err error - bash, err = exec.LookPath("bash") - if err != nil { - t.Fatal("bash is required to execute review recovery fixtures") - } - } - - source, err := os.ReadFile( - filepath.Join("..", "deployment", "infrastructure", "sfl-pr-review-recovery.yml"), - ) - if err != nil { - t.Fatalf("read review recovery workflow: %v", err) - } - text := strings.ReplaceAll(string(source), "\r\n", "\n") - const begin = "# BEGIN TESTABLE REVIEW RECOVERY POLICY" - const end = "# END TESTABLE REVIEW RECOVERY POLICY" - start := strings.Index(text, begin) - finish := strings.Index(text, end) - if start < 0 || finish <= start { - t.Fatal("review recovery policy helper block is missing") - } - helper := text[start+len(begin) : finish] - - script := `#!/usr/bin/env bash -set -u -` + helper + ` -newer_run_suppresses_retry "$1" "$2" "$3" "$4" "${5:-}" -` - scriptPath := filepath.Join(t.TempDir(), "newer-run-suppression.sh") - if err := os.WriteFile(scriptPath, []byte(script), 0o700); err != nil { - t.Fatalf("write newer run suppression fixture: %v", err) - } - scriptArgument := filepath.ToSlash(scriptPath) - if volume := filepath.VolumeName(scriptPath); volume != "" { - scriptArgument = "/" + - strings.ToLower(strings.TrimSuffix(volume, ":")) + - strings.TrimPrefix(scriptArgument, filepath.ToSlash(volume)) - } - - for _, fixture := range []struct { - name string - args []string - output string - }{ - { - name: "provenance match suppresses", - args: []string{"completed", "true", "true", "false"}, - output: "suppress", - }, - { - name: "in-flight provenance match suppresses", - args: []string{"in_progress", "true", "true", "false"}, - output: "suppress", - }, - { - name: "in-flight unavailable provenance with title match suppresses", - args: []string{"in_progress", "false", "false", "true"}, - output: "suppress", - }, - { - name: "queued unavailable provenance with title match suppresses", - args: []string{"queued", "false", "false", "true"}, - output: "suppress", - }, - { - name: "in-flight unavailable provenance without title match allows", - args: []string{"in_progress", "false", "false", "false"}, - output: "allow", - }, - { - name: "completed unavailable provenance with title match allows", - args: []string{"completed", "false", "false", "true"}, - output: "allow", - }, - { - name: "cancelled unavailable provenance with title match suppresses", - args: []string{"completed", "false", "false", "true", "cancelled"}, - output: "suppress", - }, - { - name: "cancelled unavailable provenance without title match allows", - args: []string{"completed", "false", "false", "false", "cancelled"}, - output: "allow", - }, - { - name: "completed run with unavailable provenance allows", - args: []string{"completed", "false", "false", "false"}, - output: "allow", - }, - { - name: "completed run with provenance mismatch allows", - args: []string{"completed", "true", "false", "false"}, - output: "allow", - }, - { - name: "in-flight run with provenance mismatch allows", - args: []string{"in_progress", "true", "false", "false"}, - output: "allow", - }, - } { - t.Run(fixture.name, func(t *testing.T) { - args := append([]string{scriptArgument}, fixture.args...) - cmd := exec.Command(bash, args...) - output, err := cmd.CombinedOutput() - if err != nil { - t.Fatalf("execute newer run suppression fixture: %v\n%s", err, output) - } - if got := strings.TrimSpace(string(output)); got != fixture.output { - t.Fatalf("suppression=%q, want %q", got, fixture.output) - } - }) - } -} diff --git a/gh-sfl/review_test.go b/gh-sfl/review_test.go index b0a29434..ca1a0b4d 100644 --- a/gh-sfl/review_test.go +++ b/gh-sfl/review_test.go @@ -1,82 +1,196 @@ package main import ( + "bytes" + "encoding/base64" + "encoding/json" + "errors" + "fmt" "io" "strings" "testing" + "time" ) -func TestDetectDispatchInputs(t *testing.T) { - lockV639 := ` - workflow_dispatch: - inputs: - aw_context: - required: true - type: string - base_sha: - required: true - type: string - head_sha: - required: true - type: string - item_number: - required: true - type: string - retry_count: - default: "0" - required: false - type: string -` - lockV638 := ` - workflow_dispatch: - inputs: - aw_context: - required: true - type: string - head_sha: - required: true - type: string - item_number: - required: true - type: string - retry_count: - default: "0" - required: false - type: string -` - lockLegacy := ` - workflow_dispatch: - inputs: - aw_context: - default: "" - type: string - item_number: - default: "" - type: string -` +type reviewREST struct { + comments []reviewTriggerComment + reactions []reviewCommentReaction + statuses []reviewRequestStatus + reactionGets int + clearReaction bool + checkRuns []map[string]any + checkRunPages map[int][]map[string]any + headRepo string + headRepoUnavailable bool + baseRef string + defaultBranch string + observerBranch string + observerState string + updatedAt string + workflowRunResponses [][]map[string]any + workflowRunGets int + lifecycleResponses [][]reviewLifecycleEvent + lifecycleGets int + reviewerEnabled []string + reviewerEnabledGets int + pullResponses []map[string]any + pullGets int + posts int + statusPosts int + statusPostErr error + statusBody map[string]string + postBody string + deletes int + deletePath string + deleteErr error +} - for _, tc := range []struct { - name string - lock string - headSHA bool - baseSHA bool - retryCount bool - }{ - {"v6.3.9 declares all provenance inputs", lockV639, true, true, true}, - {"v6.3.8 lacks base_sha only", lockV638, true, false, true}, - {"legacy lock declares neither", lockLegacy, false, false, false}, - {"commented mention is not a declaration", "# head_sha: see docs\n", false, false, false}, - } { - t.Run(tc.name, func(t *testing.T) { - got := detectDispatchInputs(tc.lock) - want := dispatchCapabilities{headSHA: tc.headSHA, baseSHA: tc.baseSHA, retryCount: tc.retryCount} - if got != want { - t.Errorf("detectDispatchInputs() = %+v, want %+v", got, want) +func (f *reviewREST) Get(path string, response interface{}) error { + switch { + case strings.Contains(path, "/contents/.github/workflows/sfl-pr-review-auto.yml"): + observerBranch := f.observerBranch + if observerBranch == "" { + observerBranch = f.defaultBranch + } + if observerBranch == "" { + observerBranch = "main" + } + yamlBranch := strings.ReplaceAll(observerBranch, "'", "''") + content := fmt.Sprintf("name: SFL Codex Review Observer\non:\n push:\n branches: ['%s']\nenv:\n SFL_REVIEW_BASE_BRANCH: '%s'\nif: github.event.sender.id == 199175422\n", yamlBranch, yamlBranch) + return decodeTestResponse(response, map[string]any{ + "content": base64.StdEncoding.EncodeToString([]byte(content)), + "encoding": "base64", + }) + case strings.Contains(path, "/actions/workflows/sfl-pr-review-auto.yml/runs?"): + f.workflowRunGets++ + if len(f.workflowRunResponses) == 0 { + return decodeTestResponse(response, map[string]any{"workflow_runs": []map[string]any{}}) + } + index := f.workflowRunGets - 1 + if index >= len(f.workflowRunResponses) { + index = len(f.workflowRunResponses) - 1 + } + return decodeTestResponse(response, map[string]any{"workflow_runs": f.workflowRunResponses[index]}) + case strings.Contains(path, "/actions/workflows/sfl-pr-review-auto.yml"): + state := f.observerState + if state == "" { + state = "active" + } + return decodeTestResponse(response, map[string]any{"state": state}) + case strings.Contains(path, "/actions/variables/SFL_ENABLED"): + f.reviewerEnabledGets++ + value := "true" + if len(f.reviewerEnabled) > 0 { + index := f.reviewerEnabledGets - 1 + if index >= len(f.reviewerEnabled) { + index = len(f.reviewerEnabled) - 1 + } + value = f.reviewerEnabled[index] + } + return decodeTestResponse(response, map[string]any{"value": value}) + case path == "repos/HemSoft/consumer": + defaultBranch := f.defaultBranch + if defaultBranch == "" { + defaultBranch = "main" + } + return decodeTestResponse(response, map[string]any{"default_branch": defaultBranch}) + case strings.Contains(path, "/check-runs?"): + for page, runs := range f.checkRunPages { + if strings.Contains(path, fmt.Sprintf("page=%d", page)) { + return decodeTestResponse(response, map[string]any{"check_runs": runs}) + } + } + return decodeTestResponse(response, map[string]any{"check_runs": f.checkRuns}) + case strings.Contains(path, "/statuses?"): + return decodeTestResponse(response, f.statuses) + case strings.Contains(path, "/issues/94/events?"): + f.lifecycleGets++ + if len(f.lifecycleResponses) == 0 { + return decodeTestResponse(response, []reviewLifecycleEvent{}) + } + index := f.lifecycleGets - 1 + if index >= len(f.lifecycleResponses) { + index = len(f.lifecycleResponses) - 1 + } + return decodeTestResponse(response, f.lifecycleResponses[index]) + case strings.Contains(path, "/pulls/"): + f.pullGets++ + if len(f.pullResponses) > 0 { + index := f.pullGets - 1 + if index >= len(f.pullResponses) { + index = len(f.pullResponses) - 1 } + return decodeTestResponse(response, f.pullResponses[index]) + } + var headRepo any = map[string]string{"full_name": f.headRepo} + if f.headRepo == "" { + headRepo = map[string]string{"full_name": "HemSoft/consumer"} + } + if f.headRepoUnavailable { + headRepo = nil + } + baseRef := f.baseRef + if baseRef == "" { + baseRef = "main" + } + return decodeTestResponse(response, map[string]any{ + "base": map[string]any{"sha": strings.Repeat("a", 40), "ref": baseRef, "repo": map[string]string{"full_name": "HemSoft/consumer"}}, + "head": map[string]any{"sha": strings.Repeat("b", 40), "repo": headRepo}, + "state": "open", + "updated_at": f.updatedAt, }) + case strings.Contains(path, "/comments?"): + return decodeTestResponse(response, f.comments) + case strings.Contains(path, "/reactions?"): + f.reactionGets++ + if f.clearReaction && f.reactionGets > 1 { + return decodeTestResponse(response, []reviewCommentReaction{}) + } + return decodeTestResponse(response, f.reactions) + default: + return fmt.Errorf("unexpected GET %s", path) } } +func (f *reviewREST) GetWithETag(string, interface{}) (string, error) { + return "", fmt.Errorf("unexpected GetWithETag") +} + +func (f *reviewREST) Post(path string, body io.Reader, response interface{}) error { + if strings.Contains(path, "/statuses/") { + f.statusPosts++ + if err := json.NewDecoder(body).Decode(&f.statusBody); err != nil { + return err + } + return f.statusPostErr + } + if !strings.HasSuffix(path, "/issues/94/comments") { + return fmt.Errorf("unexpected POST %s", path) + } + f.posts++ + var payload map[string]string + if err := json.NewDecoder(body).Decode(&payload); err != nil { + return err + } + f.postBody = payload["body"] + return decodeTestResponse(response, map[string]any{ + "id": 999, + "html_url": "https://github.test/HemSoft/consumer/pull/94#issuecomment-999", + }) +} + +func (f *reviewREST) Put(string, io.Reader, interface{}) error { + return fmt.Errorf("unexpected PUT") +} +func (f *reviewREST) Patch(string, io.Reader, interface{}) error { + return fmt.Errorf("unexpected PATCH") +} +func (f *reviewREST) Delete(path string, _ interface{}) error { + f.deletes++ + f.deletePath = path + return f.deleteErr +} + func TestParseReviewOptions(t *testing.T) { for _, tc := range []struct { name string @@ -84,12 +198,14 @@ func TestParseReviewOptions(t *testing.T) { wantPR int wantErr string }{ - {name: "pr number", args: []string{"94"}, wantPR: 94}, + {name: "positional PR", args: []string{"94"}, wantPR: 94}, {name: "hash prefix", args: []string{"#94"}, wantPR: 94}, - {name: "repo flag", args: []string{"--repo", "owner/repo", "94"}, wantPR: 94}, - {name: "short repo flag", args: []string{"-R", "owner/repo", "94"}, wantPR: 94}, - {name: "missing number", args: []string{}, wantErr: "exactly one pull request number"}, - {name: "two numbers", args: []string{"1", "2"}, wantErr: "exactly one pull request number"}, + {name: "PR flag", args: []string{"--pr", "94"}, wantPR: 94}, + {name: "repo and PR flags", args: []string{"--repo", "owner/repo", "--pr", "94"}, wantPR: 94}, + {name: "retry", args: []string{"--retry", "94"}, wantPR: 94}, + {name: "missing number", args: []string{}, wantErr: "required"}, + {name: "duplicate number", args: []string{"--pr", "94", "94"}, wantErr: "exactly once"}, + {name: "two numbers", args: []string{"1", "2"}, wantErr: "exactly once"}, {name: "not a number", args: []string{"abc"}, wantErr: "invalid pull request number"}, {name: "zero", args: []string{"0"}, wantErr: "invalid pull request number"}, } { @@ -107,9 +223,1009 @@ func TestParseReviewOptions(t *testing.T) { if opts.pr != tc.wantPR { t.Errorf("parseReviewOptions(%v) pr = %d, want %d", tc.args, opts.pr, tc.wantPR) } - if strings.Contains(tc.name, "repo flag") && opts.repo != "owner/repo" { - t.Errorf("parseReviewOptions(%v) repo = %q, want owner/repo", tc.args, opts.repo) - } }) } } + +func TestRunReviewPostsOneHeadBoundCodexRequest(t *testing.T) { + rest := &reviewREST{} + installReviewFakes(t, rest) + + var stdout bytes.Buffer + if err := runReview([]string{"--repo", "HemSoft/consumer", "--pr", "94"}, &stdout, io.Discard); err != nil { + t.Fatalf("runReview() unexpected error: %v", err) + } + if rest.posts != 1 { + t.Fatalf("Codex request posts = %d, want 1", rest.posts) + } + if rest.statusPosts != 1 || + rest.statusBody["context"] != codexReviewRequestRegistryContext || + rest.statusBody["target_url"] != "https://github.test/HemSoft/consumer/pull/94#issuecomment-999" { + t.Fatalf("request registry status = posts %d, body %#v", rest.statusPosts, rest.statusBody) + } + wantMarker := codexReviewMarker(strings.Repeat("b", 40), strings.Repeat("a", 40), "none") + if rest.postBody != codexReviewCommand+"\n\n"+wantMarker { + t.Fatalf("posted body = %q", rest.postBody) + } + if !strings.Contains(stdout.String(), "subscription-backed Codex review") || + !strings.Contains(stdout.String(), "#issuecomment-999") { + t.Fatalf("stdout = %q", stdout.String()) + } +} + +func TestRunReviewDeletesRequestWhenRegistrationFails(t *testing.T) { + rest := &reviewREST{statusPostErr: errors.New("status unavailable")} + installReviewFakes(t, rest) + + err := runReview([]string{"--repo", "HemSoft/consumer", "--pr", "94"}, io.Discard, io.Discard) + if err == nil || !strings.Contains(err.Error(), "deleted unregistered request comment 999") { + t.Fatalf("runReview() registration error = %v", err) + } + if rest.deletes != 1 || rest.deletePath != "repos/HemSoft/consumer/issues/comments/999" { + t.Fatalf("request cleanup = deletes %d, path %q", rest.deletes, rest.deletePath) + } +} + +func TestRunReviewReportsRequestCleanupFailure(t *testing.T) { + rest := &reviewREST{ + statusPostErr: errors.New("status unavailable"), + deleteErr: errors.New("delete unavailable"), + } + installReviewFakes(t, rest) + + err := runReview([]string{"--repo", "HemSoft/consumer", "--pr", "94"}, io.Discard, io.Discard) + if err == nil || !strings.Contains(err.Error(), "status unavailable") || + !strings.Contains(err.Error(), "deleting unregistered request comment 999: delete unavailable") { + t.Fatalf("runReview() cleanup error = %v", err) + } +} + +func TestRunReviewWaitsForCurrentInvalidationBeforeReadingContext(t *testing.T) { + updatedAt := "2026-08-19T00:00:00Z" + head := strings.Repeat("b", 40) + base := strings.Repeat("a", 40) + run := func(status string) map[string]any { + return map[string]any{ + "id": 77, + "event": "pull_request_target", + "status": status, + "created_at": updatedAt, + "pull_requests": []map[string]any{{ + "number": 94, + "head": map[string]string{"sha": head}, + "base": map[string]string{"sha": base}, + }}, + } + } + contextToken := "sfl-codex-review:pull-context:at:1787097600000:77" + rest := &reviewREST{ + updatedAt: updatedAt, + workflowRunResponses: [][]map[string]any{ + {}, + {}, + {}, + {}, + {}, + {run("queued")}, + {}, + }, + checkRuns: []map[string]any{{ + "id": 5, + "external_id": contextToken, + "app": map[string]any{"id": 15368}, + }}, + } + installReviewFakes(t, rest) + + if err := runReview([]string{"--repo", "HemSoft/consumer", "94"}, io.Discard, io.Discard); err != nil { + t.Fatalf("runReview() invalidation barrier error = %v", err) + } + if rest.workflowRunGets != 15 { + t.Fatalf("workflow run GETs = %d, want 15", rest.workflowRunGets) + } + wantMarker := codexReviewMarker(head, base, contextToken) + if !strings.Contains(rest.postBody, wantMarker) { + t.Fatalf("posted body = %q, want current context marker %q", rest.postBody, wantMarker) + } +} + +func TestRunReviewRevalidatesPullAfterRetryWait(t *testing.T) { + head := strings.Repeat("b", 40) + base := strings.Repeat("a", 40) + changedBase := strings.Repeat("c", 40) + pull := func(baseSHA string) map[string]any { + return map[string]any{ + "base": map[string]any{"sha": baseSHA, "ref": "main", "repo": map[string]string{"full_name": "HemSoft/consumer"}}, + "head": map[string]any{"sha": head, "repo": map[string]string{"full_name": "HemSoft/consumer"}}, + "state": "open", + } + } + rest := &reviewREST{ + pullResponses: []map[string]any{pull(base), pull(base), pull(changedBase)}, + comments: []reviewTriggerComment{{ + ID: 123, + Body: bodyForReviewRequest(codexReviewMarker(head, base, "none")), + HTMLURL: "https://github.test/retry-before-base-change", + CreatedAt: "2026-08-19T00:00:00Z", + User: struct { + Login string `json:"login"` + }{Login: "HemSoft"}, + }}, + } + installReviewFakes(t, rest) + + err := runReview([]string{"--repo", "HemSoft/consumer", "--retry", "94"}, io.Discard, io.Discard) + if err == nil || !strings.Contains(err.Error(), "context changed before the Codex review request could be posted") { + t.Fatalf("runReview() post-wait context error = %v", err) + } + if rest.posts != 0 { + t.Fatalf("post-wait context change posts = %d, want 0", rest.posts) + } +} + +func TestRunReviewRechecksMaintenanceModeBeforePosting(t *testing.T) { + head := strings.Repeat("b", 40) + base := strings.Repeat("a", 40) + rest := &reviewREST{ + reviewerEnabled: []string{"true", "false"}, + comments: []reviewTriggerComment{{ + ID: 123, + Body: bodyForReviewRequest(codexReviewMarker(head, base, "none")), + HTMLURL: "https://github.test/retry-before-stop", + CreatedAt: "2026-08-19T00:00:00Z", + User: struct { + Login string `json:"login"` + }{Login: "HemSoft"}, + }}, + } + installReviewFakes(t, rest) + + err := runReview([]string{"--repo", "HemSoft/consumer", "--retry", "94"}, io.Discard, io.Discard) + if err == nil || !strings.Contains(err.Error(), "SFL is stopped") { + t.Fatalf("runReview() post-wait maintenance error = %v", err) + } + if rest.posts != 0 || rest.reviewerEnabledGets != 2 { + t.Fatalf("post-wait maintenance posts/enabled GETs = %d/%d, want 0/2", rest.posts, rest.reviewerEnabledGets) + } +} + +func TestRunReviewRejectsCloseReopenDuringRetryWait(t *testing.T) { + head := strings.Repeat("b", 40) + base := strings.Repeat("a", 40) + rest := &reviewREST{ + lifecycleResponses: [][]reviewLifecycleEvent{ + {{ID: 10, Event: "reopened"}}, + {{ID: 10, Event: "reopened"}}, + {{ID: 10, Event: "reopened"}, {ID: 11, Event: "closed"}, {ID: 12, Event: "reopened"}}, + }, + comments: []reviewTriggerComment{{ + ID: 123, + Body: bodyForReviewRequest(codexReviewMarker(head, base, "none")), + HTMLURL: "https://github.test/retry-before-reopen", + CreatedAt: "2026-08-19T00:00:00Z", + User: struct { + Login string `json:"login"` + }{Login: "HemSoft"}, + }}, + } + installReviewFakes(t, rest) + + err := runReview([]string{"--repo", "HemSoft/consumer", "--retry", "94"}, io.Discard, io.Discard) + if err == nil || !strings.Contains(err.Error(), "was closed or reopened") { + t.Fatalf("runReview() lifecycle error = %v", err) + } + if rest.posts != 0 || rest.lifecycleGets != 3 { + t.Fatalf("close/reopen posts/lifecycle GETs = %d/%d, want 0/3", rest.posts, rest.lifecycleGets) + } +} + +func TestRunReviewRejectsCloseReopenDuringInitialInvalidationWait(t *testing.T) { + rest := &reviewREST{ + lifecycleResponses: [][]reviewLifecycleEvent{ + {{ID: 10, Event: "reopened"}}, + {{ID: 10, Event: "reopened"}, {ID: 11, Event: "closed"}, {ID: 12, Event: "reopened"}}, + }, + } + installReviewFakes(t, rest) + + err := runReview([]string{"--repo", "HemSoft/consumer", "94"}, io.Discard, io.Discard) + if err == nil || !strings.Contains(err.Error(), "was closed or reopened while SFL invalidations were settling") { + t.Fatalf("runReview() initial lifecycle error = %v", err) + } + if rest.posts != 0 || rest.lifecycleGets != 2 { + t.Fatalf("initial close/reopen posts/lifecycle GETs = %d/%d, want 0/2", rest.posts, rest.lifecycleGets) + } +} + +func TestReviewWorkflowRunAppliesOnlyToExactContext(t *testing.T) { + head := strings.Repeat("b", 40) + base := strings.Repeat("a", 40) + run := reviewWorkflowRun{ + Event: "pull_request_target", + Status: "in_progress", + CreatedAt: "2026-08-19T00:00:00Z", + } + run.PullRequests = append(run.PullRequests, struct { + Number int `json:"number"` + Head struct { + SHA string `json:"sha"` + } `json:"head"` + Base struct { + SHA string `json:"sha"` + } `json:"base"` + }{Number: 94}) + run.PullRequests[0].Head.SHA = head + run.PullRequests[0].Base.SHA = base + + if !reviewWorkflowRunApplies(run, 94, "main", head, base) { + t.Fatal("exact pull-request invalidation did not apply") + } + if reviewWorkflowRunApplies(run, 95, "main", head, base) { + t.Fatal("another pull request invalidation applied") + } + run.Event = "push" + run.HeadBranch = "main" + run.HeadSHA = base + if !reviewWorkflowRunApplies(run, 94, "main", head, base) { + t.Fatal("exact default-branch push invalidation did not apply") + } + run.Status = "completed" + if reviewWorkflowRunApplies(run, 94, "main", head, base) { + t.Fatal("completed invalidation run remained active") + } +} + +func TestFetchActiveReviewWorkflowRunsPaginatesEveryStatus(t *testing.T) { + firstPage := make([]map[string]any, 100) + for index := range firstPage { + firstPage[index] = map[string]any{"id": index + 1, "status": "requested"} + } + rest := &reviewREST{workflowRunResponses: [][]map[string]any{ + firstPage, + {{"id": 101, "status": "requested"}}, + {}, + }} + + runs, err := fetchActiveReviewWorkflowRuns(rest, "HemSoft", "consumer") + if err != nil { + t.Fatalf("fetchActiveReviewWorkflowRuns() error = %v", err) + } + if len(runs) != 101 { + t.Fatalf("active workflow runs = %d, want 101", len(runs)) + } + if rest.workflowRunGets != 6 { + t.Fatalf("workflow run GETs = %d, want 6", rest.workflowRunGets) + } +} + +func TestRunReviewRejectsForkPullRequest(t *testing.T) { + rest := &reviewREST{headRepo: "contributor/consumer"} + installReviewFakes(t, rest) + + err := runReview([]string{"--repo", "HemSoft/consumer", "94"}, io.Discard, io.Discard) + if err == nil || !strings.Contains(err.Error(), "same-repository branches only") || + !strings.Contains(err.Error(), "contributor/consumer") { + t.Fatalf("runReview() fork error = %v", err) + } + if rest.posts != 0 { + t.Fatalf("fork review posts = %d, want 0", rest.posts) + } +} + +func TestRunReviewRejectsMissingHeadRepository(t *testing.T) { + rest := &reviewREST{headRepoUnavailable: true} + installReviewFakes(t, rest) + + err := runReview([]string{"--repo", "HemSoft/consumer", "94"}, io.Discard, io.Discard) + if err == nil || !strings.Contains(err.Error(), "no available head repository") { + t.Fatalf("runReview() missing head repository error = %v", err) + } + if rest.posts != 0 { + t.Fatalf("missing-head review posts = %d, want 0", rest.posts) + } +} + +func TestRunReviewRejectsNonDefaultBaseBranch(t *testing.T) { + rest := &reviewREST{baseRef: "release/next", defaultBranch: "main"} + installReviewFakes(t, rest) + + err := runReview([]string{"--repo", "HemSoft/consumer", "94"}, io.Discard, io.Discard) + if err == nil || !strings.Contains(err.Error(), "support only the default branch main") || + !strings.Contains(err.Error(), "release/next") { + t.Fatalf("runReview() non-default base error = %v", err) + } + if rest.posts != 0 { + t.Fatalf("non-default-base review posts = %d, want 0", rest.posts) + } +} + +func TestRunReviewRejectsDisabledObserver(t *testing.T) { + rest := &reviewREST{observerState: "disabled_manually"} + installReviewFakes(t, rest) + + err := runReview([]string{"--repo", "HemSoft/consumer", "94"}, io.Discard, io.Discard) + if err == nil || !strings.Contains(err.Error(), "disabled_manually") || + !strings.Contains(err.Error(), "enable sfl-pr-review-auto.yml") { + t.Fatalf("runReview() disabled observer error = %v", err) + } + if rest.posts != 0 { + t.Fatalf("disabled-observer review posts = %d, want 0", rest.posts) + } +} + +func TestRunReviewRejectsStaleObserverBaseBranch(t *testing.T) { + rest := &reviewREST{defaultBranch: "main", observerBranch: "trunk"} + installReviewFakes(t, rest) + + err := runReview([]string{"--repo", "HemSoft/consumer", "94"}, io.Discard, io.Discard) + if err == nil || !strings.Contains(err.Error(), "retired or stale") || + !strings.Contains(err.Error(), "gh sfl sync") { + t.Fatalf("runReview() stale observer error = %v", err) + } + if rest.posts != 0 { + t.Fatalf("stale-observer review posts = %d, want 0", rest.posts) + } +} + +func TestRunReviewAcceptsYamlEscapedObserverBaseBranch(t *testing.T) { + branch := "release/o'brien" + rest := &reviewREST{defaultBranch: branch, baseRef: branch} + installReviewFakes(t, rest) + + if err := runReview([]string{"--repo", "HemSoft/consumer", "94"}, io.Discard, io.Discard); err != nil { + t.Fatalf("runReview() quoted observer branch error = %v", err) + } + if rest.posts != 1 { + t.Fatalf("quoted-observer review posts = %d, want 1", rest.posts) + } +} + +func TestRunReviewRejectsSameHeadRequestedAgainstAnotherBase(t *testing.T) { + head := strings.Repeat("b", 40) + rest := &reviewREST{comments: []reviewTriggerComment{{ + Body: codexReviewCommand + "\n\n" + codexReviewMarker(head, strings.Repeat("c", 40), "none"), + HTMLURL: "https://github.test/old-base-request", + User: struct { + Login string `json:"login"` + }{Login: "HemSoft"}, + }}} + installReviewFakes(t, rest) + + err := runReview([]string{"--repo", "HemSoft/consumer", "94"}, io.Discard, io.Discard) + if err == nil || !strings.Contains(err.Error(), "already requested against another base") || + !strings.Contains(err.Error(), "update the pull request branch to a new head") { + t.Fatalf("runReview() cross-base head reuse error = %v", err) + } + if rest.posts != 0 { + t.Fatalf("cross-base head reuse posts = %d, want 0", rest.posts) + } +} + +func TestFetchReviewContextTokenUsesLatestInvalidation(t *testing.T) { + rest := &reviewREST{checkRuns: []map[string]any{{ + "id": 2, + "external_id": "sfl-codex-review:pull-context:at:123:456", + "app": map[string]any{"id": 15368}, + }, { + "id": 1, + "external_id": "sfl-codex-review:base-advance:at:100:400:94", + "app": map[string]any{"id": 15368}, + }}} + token, err := fetchReviewContextToken(rest, "HemSoft", "consumer", strings.Repeat("b", 40)) + if err != nil { + t.Fatalf("fetchReviewContextToken() error = %v", err) + } + if token != "sfl-codex-review:pull-context:at:123:456" { + t.Fatalf("context token = %q", token) + } +} + +func TestFetchReviewContextTokenPaginatesPastTerminalHistory(t *testing.T) { + firstPage := make([]map[string]any, 100) + for index := range firstPage { + firstPage[index] = map[string]any{ + "id": index + 1, + "external_id": fmt.Sprintf("sfl-codex-review:pull:94:request:%d", index+1), + "app": map[string]any{"id": 15368}, + } + } + want := "sfl-codex-review:pull-context:at:123:456" + rest := &reviewREST{checkRunPages: map[int][]map[string]any{ + 1: firstPage, + 2: {{ + "id": 101, + "external_id": want, + "app": map[string]any{"id": 15368}, + }}, + }} + + token, err := fetchReviewContextToken(rest, "HemSoft", "consumer", strings.Repeat("b", 40)) + if err != nil { + t.Fatalf("fetchReviewContextToken() error = %v", err) + } + if token != want { + t.Fatalf("context token = %q, want paginated %q", token, want) + } +} + +func TestRunReviewDeduplicatesCurrentHeadRequest(t *testing.T) { + head := strings.Repeat("b", 40) + rest := &reviewREST{comments: []reviewTriggerComment{{ + Body: codexReviewCommand + "\n\n" + codexReviewMarker(head, strings.Repeat("a", 40), "none"), + HTMLURL: "https://github.test/existing", + User: struct { + Login string `json:"login"` + }{Login: "HemSoft"}, + }}} + installReviewFakes(t, rest) + + var stdout bytes.Buffer + if err := runReview([]string{"--repo", "HemSoft/consumer", "94"}, &stdout, io.Discard); err != nil { + t.Fatalf("runReview() unexpected error: %v", err) + } + if rest.posts != 0 { + t.Fatalf("Codex request posts = %d, want 0", rest.posts) + } + if !strings.Contains(stdout.String(), "already requested") || + !strings.Contains(stdout.String(), "https://github.test/existing") || + !strings.Contains(stdout.String(), "--retry") { + t.Fatalf("stdout = %q", stdout.String()) + } +} + +func TestRunReviewRetryPostsAnotherCurrentHeadRequest(t *testing.T) { + head := strings.Repeat("b", 40) + requestTime := "2026-08-19T00:00:00Z" + requestMillis := time.Date(2026, 8, 19, 0, 0, 0, 0, time.UTC).UnixMilli() + rest := &reviewREST{comments: []reviewTriggerComment{{ + ID: 123, + Body: codexReviewCommand + "\n\n" + codexReviewMarker(head, strings.Repeat("a", 40), "none"), + HTMLURL: "https://github.test/existing", + CreatedAt: requestTime, + User: struct { + Login string `json:"login"` + }{Login: "HemSoft"}, + }}, statuses: []reviewRequestStatus{registeredReviewRequestStatus(123)}, checkRuns: []map[string]any{{ + "status": "completed", + "external_id": fmt.Sprintf("sfl-codex-review:pull:94:base:%s:context:none:request:123:at:%d:artifact:r456", strings.Repeat("a", 40), requestMillis), + "app": map[string]any{"id": 15368}, + }}} + installReviewFakes(t, rest) + + var stdout bytes.Buffer + if err := runReview([]string{"--repo", "HemSoft/consumer", "--retry", "94"}, &stdout, io.Discard); err != nil { + t.Fatalf("runReview() unexpected error: %v", err) + } + if rest.posts != 1 { + t.Fatalf("Codex retry posts = %d, want 1", rest.posts) + } + if rest.postBody != codexReviewCommand+"\n\n"+codexReviewMarker(head, strings.Repeat("a", 40), "none") { + t.Fatalf("posted retry body = %q", rest.postBody) + } +} + +func TestRunReviewRejectsRetryAfterSuccessfulGate(t *testing.T) { + head := strings.Repeat("b", 40) + base := strings.Repeat("a", 40) + requestTime := "2026-08-19T00:00:00Z" + requestMillis := time.Date(2026, 8, 19, 0, 0, 0, 0, time.UTC).UnixMilli() + rest := &reviewREST{comments: []reviewTriggerComment{{ + ID: 123, + Body: codexReviewCommand + "\n\n" + codexReviewMarker(head, base, "none"), + HTMLURL: "https://github.test/existing", + CreatedAt: requestTime, + User: struct { + Login string `json:"login"` + }{Login: "HemSoft"}, + }}, statuses: []reviewRequestStatus{registeredReviewRequestStatus(123)}, checkRuns: []map[string]any{{ + "status": "completed", + "conclusion": "success", + "external_id": fmt.Sprintf("sfl-codex-review:pull:94:base:%s:context:none:request:123:at:%d:artifact:r456", base, requestMillis), + "app": map[string]any{"id": 15368}, + }}} + installReviewFakes(t, rest) + + err := runReview([]string{"--repo", "HemSoft/consumer", "--retry", "94"}, io.Discard, io.Discard) + if err == nil || !strings.Contains(err.Error(), "already passed") || + !strings.Contains(err.Error(), "push a new commit") { + t.Fatalf("runReview() successful retry error = %v", err) + } + if rest.posts != 0 { + t.Fatalf("successful-gate retry posts = %d, want 0", rest.posts) + } +} + +func TestRunReviewRetryRecoversFromOverlappingRequest(t *testing.T) { + head := strings.Repeat("b", 40) + base := strings.Repeat("a", 40) + firstCreated := "2026-08-19T00:00:00Z" + secondCreated := "2026-08-19T00:00:01Z" + firstMillis := time.Date(2026, 8, 19, 0, 0, 0, 0, time.UTC).UnixMilli() + marker := codexReviewCommand + "\n\n" + codexReviewMarker(head, base, "none") + comment := func(id int64, createdAt string) reviewTriggerComment { + return reviewTriggerComment{ + ID: id, + Body: marker, + HTMLURL: fmt.Sprintf("https://github.test/request/%d", id), + CreatedAt: createdAt, + User: struct { + Login string `json:"login"` + }{Login: "HemSoft"}, + } + } + rest := &reviewREST{ + comments: []reviewTriggerComment{ + comment(123, firstCreated), + comment(124, secondCreated), + }, + statuses: []reviewRequestStatus{ + registeredReviewRequestStatus(123), + registeredReviewRequestStatus(124), + }, + checkRuns: []map[string]any{{ + "status": "completed", + "completed_at": "2026-08-19T00:00:01.500Z", + "external_id": fmt.Sprintf("sfl-codex-review:pull:94:base:%s:context:none:request:123:at:%d:artifact:r456", base, firstMillis), + "app": map[string]any{"id": 15368}, + }}, + } + installReviewFakes(t, rest) + + if err := runReview([]string{"--repo", "HemSoft/consumer", "--retry", "94"}, io.Discard, io.Discard); err != nil { + t.Fatalf("runReview() overlapping retry error = %v", err) + } + if rest.posts != 1 { + t.Fatalf("overlapping retry posts = %d, want 1", rest.posts) + } +} + +func TestRunReviewRejectsOverlapRecoveryFromSuccessfulGate(t *testing.T) { + head := strings.Repeat("b", 40) + base := strings.Repeat("a", 40) + firstCreated := "2026-08-19T00:00:00Z" + secondCreated := "2026-08-19T00:00:01Z" + firstMillis := time.Date(2026, 8, 19, 0, 0, 0, 0, time.UTC).UnixMilli() + request := func(id int64, createdAt string) reviewTriggerComment { + return reviewTriggerComment{ + ID: id, + Body: bodyForReviewRequest(codexReviewMarker(head, base, "none")), + HTMLURL: fmt.Sprintf("https://github.test/request/%d", id), + CreatedAt: createdAt, + User: struct { + Login string `json:"login"` + }{Login: "HemSoft"}, + } + } + rest := &reviewREST{ + comments: []reviewTriggerComment{request(123, firstCreated), request(124, secondCreated)}, + statuses: []reviewRequestStatus{ + registeredReviewRequestStatus(123), + registeredReviewRequestStatus(124), + }, + checkRuns: []map[string]any{{ + "status": "completed", + "conclusion": "success", + "completed_at": "2026-08-19T00:00:01.500Z", + "external_id": fmt.Sprintf("sfl-codex-review:pull:94:base:%s:context:none:request:123:at:%d:artifact:r456", base, firstMillis), + "app": map[string]any{"id": 15368}, + }}, + } + installReviewFakes(t, rest) + + err := runReview([]string{"--repo", "HemSoft/consumer", "--retry", "94"}, io.Discard, io.Discard) + if err == nil || !strings.Contains(err.Error(), "overlapping Codex review request") || + !strings.Contains(err.Error(), "already passed") { + t.Fatalf("runReview() successful overlap recovery error = %v", err) + } + if rest.posts != 0 { + t.Fatalf("successful overlap recovery posts = %d, want 0", rest.posts) + } +} + +func TestRunReviewRetryRecoversFromSameSecondOverlap(t *testing.T) { + head := strings.Repeat("b", 40) + base := strings.Repeat("a", 40) + firstCreated := "2026-08-19T00:00:00Z" + secondCreated := "2026-08-19T00:00:01Z" + firstMillis := time.Date(2026, 8, 19, 0, 0, 0, 0, time.UTC).UnixMilli() + marker := codexReviewCommand + "\n\n" + codexReviewMarker(head, base, "none") + comment := func(id int64, createdAt string) reviewTriggerComment { + return reviewTriggerComment{ + ID: id, + Body: marker, + HTMLURL: fmt.Sprintf("https://github.test/request/%d", id), + CreatedAt: createdAt, + User: struct { + Login string `json:"login"` + }{Login: "HemSoft"}, + } + } + rest := &reviewREST{ + comments: []reviewTriggerComment{ + comment(123, firstCreated), + comment(124, secondCreated), + }, + statuses: []reviewRequestStatus{ + registeredReviewRequestStatus(123), + registeredReviewRequestStatus(124), + }, + checkRuns: []map[string]any{{ + "status": "completed", + "completed_at": secondCreated, + "external_id": fmt.Sprintf("sfl-codex-review:pull:94:base:%s:context:none:request:123:at:%d:artifact:r456", base, firstMillis), + "app": map[string]any{"id": 15368}, + }}, + } + installReviewFakes(t, rest) + waits := 0 + waitForRetryOrdering = func() { waits++ } + + if err := runReview([]string{"--repo", "HemSoft/consumer", "--retry", "94"}, io.Discard, io.Discard); err != nil { + t.Fatalf("runReview() same-second overlap error = %v", err) + } + if rest.posts != 1 { + t.Fatalf("same-second overlap posts = %d, want 1", rest.posts) + } + if waits != 1 { + t.Fatalf("same-second overlap waits = %d, want 1", waits) + } +} + +func TestRunReviewRetryRejectsOutstandingRequest(t *testing.T) { + head := strings.Repeat("b", 40) + rest := &reviewREST{comments: []reviewTriggerComment{{ + ID: 123, + Body: codexReviewCommand + "\n\n" + codexReviewMarker(head, strings.Repeat("a", 40), "none"), + HTMLURL: "https://github.test/existing", + CreatedAt: "2026-08-19T00:00:00Z", + User: struct { + Login string `json:"login"` + }{Login: "HemSoft"}, + }}, statuses: []reviewRequestStatus{registeredReviewRequestStatus(123)}} + installReviewFakes(t, rest) + + err := runReview([]string{"--repo", "HemSoft/consumer", "--retry", "94"}, io.Discard, io.Discard) + if err == nil || !strings.Contains(err.Error(), "still outstanding") { + t.Fatalf("runReview() outstanding retry error = %v", err) + } + if rest.posts != 0 { + t.Fatalf("outstanding retry posts = %d, want 0", rest.posts) + } +} + +func TestRunReviewRetryRecoversFromEditedRequest(t *testing.T) { + head := strings.Repeat("b", 40) + base := strings.Repeat("a", 40) + rest := &reviewREST{comments: []reviewTriggerComment{{ + ID: 123, + Body: codexReviewCommand + "\n\n" + codexReviewMarker(head, base, "none"), + HTMLURL: "https://github.test/edited", + CreatedAt: "2026-08-19T00:00:00Z", + UpdatedAt: "2026-08-19T00:00:01Z", + User: struct { + Login string `json:"login"` + }{Login: "HemSoft"}, + }}} + installReviewFakes(t, rest) + + if err := runReview([]string{"--repo", "HemSoft/consumer", "--retry", "94"}, io.Discard, io.Discard); err != nil { + t.Fatalf("runReview() edited-request retry error = %v", err) + } + if rest.posts != 1 { + t.Fatalf("edited-request retry posts = %d, want 1", rest.posts) + } + if rest.reactionGets != 60 { + t.Fatalf("edited-request reaction GETs = %d, want 60", rest.reactionGets) + } +} + +func TestRunReviewRetryRecoversFromSameSecondBodyMutation(t *testing.T) { + head := strings.Repeat("b", 40) + base := strings.Repeat("a", 40) + timestamp := "2026-08-19T00:00:00Z" + rest := &reviewREST{comments: []reviewTriggerComment{{ + ID: 123, + Body: "please " + codexReviewCommand + "\n\n" + codexReviewMarker(head, base, "none"), + HTMLURL: "https://github.test/edited-same-second", + CreatedAt: timestamp, + UpdatedAt: timestamp, + User: struct { + Login string `json:"login"` + }{Login: "HemSoft"}, + }}, statuses: []reviewRequestStatus{registeredReviewRequestStatus(123)}} + installReviewFakes(t, rest) + + if err := runReview([]string{"--repo", "HemSoft/consumer", "--retry", "94"}, io.Discard, io.Discard); err != nil { + t.Fatalf("runReview() same-second mutation retry error = %v", err) + } + if rest.posts != 1 || rest.reactionGets != 60 { + t.Fatalf("same-second mutation posts/reaction GETs = %d/%d, want 1/60", rest.posts, rest.reactionGets) + } +} + +func TestRunReviewRejectsEditedRequestAfterSuccessfulGate(t *testing.T) { + head := strings.Repeat("b", 40) + base := strings.Repeat("a", 40) + createdAt := "2026-08-19T00:00:00Z" + createdMillis := time.Date(2026, 8, 19, 0, 0, 0, 0, time.UTC).UnixMilli() + rest := &reviewREST{ + comments: []reviewTriggerComment{{ + ID: 123, + Body: "please " + codexReviewCommand + "\n\n" + codexReviewMarker(head, base, "none"), + HTMLURL: "https://github.test/edited-after-success", + CreatedAt: createdAt, + UpdatedAt: createdAt, + User: struct { + Login string `json:"login"` + }{Login: "HemSoft"}, + }}, + statuses: []reviewRequestStatus{registeredReviewRequestStatus(123)}, + checkRuns: []map[string]any{{ + "status": "completed", + "conclusion": "success", + "completed_at": "2026-08-19T00:00:01Z", + "external_id": fmt.Sprintf("sfl-codex-review:pull:94:base:%s:context:none:request:123:at:%d:artifact:c456", base, createdMillis), + "app": map[string]any{"id": 15368}, + }}, + } + installReviewFakes(t, rest) + + err := runReview([]string{"--repo", "HemSoft/consumer", "--retry", "94"}, io.Discard, io.Discard) + if err == nil || !strings.Contains(err.Error(), "already passed") { + t.Fatalf("runReview() edited successful request error = %v", err) + } + if rest.posts != 0 { + t.Fatalf("edited successful request posts = %d, want 0", rest.posts) + } +} + +func TestRunReviewAllowsEditedRecoveryAfterContextInvalidation(t *testing.T) { + head := strings.Repeat("b", 40) + base := strings.Repeat("a", 40) + createdAt := "2026-08-19T00:00:00Z" + createdMillis := time.Date(2026, 8, 19, 0, 0, 0, 0, time.UTC).UnixMilli() + currentContext := "sfl-codex-review:pull-context:at:1787097600000:77" + rest := &reviewREST{ + comments: []reviewTriggerComment{{ + ID: 123, + Body: codexReviewCommand + "\n\n" + codexReviewMarker(head, base, "none"), + HTMLURL: "https://github.test/pre-invalidation-success", + CreatedAt: createdAt, + UpdatedAt: createdAt, + User: struct { + Login string `json:"login"` + }{Login: "HemSoft"}, + }}, + statuses: []reviewRequestStatus{registeredReviewRequestStatus(123)}, + checkRuns: []map[string]any{ + { + "id": 77, + "status": "completed", + "external_id": currentContext, + "app": map[string]any{"id": 15368}, + }, + { + "status": "completed", + "conclusion": "success", + "external_id": fmt.Sprintf("sfl-codex-review:pull:94:base:%s:context:none:request:123:at:%d:artifact:c456", base, createdMillis), + "app": map[string]any{"id": 15368}, + }, + }, + } + installReviewFakes(t, rest) + + if err := runReview([]string{"--repo", "HemSoft/consumer", "--retry", "94"}, io.Discard, io.Discard); err != nil { + t.Fatalf("runReview() invalidated success recovery error = %v", err) + } + if rest.posts != 1 || rest.reactionGets != 60 { + t.Fatalf("invalidated success posts/reaction GETs = %d/%d, want 1/60", rest.posts, rest.reactionGets) + } +} + +func TestRunReviewRetryRecoversAfterEditedMarkerIsRemoved(t *testing.T) { + rest := &reviewREST{comments: []reviewTriggerComment{{ + ID: 123, + Body: codexReviewCommand + "\n\nmarker removed", + HTMLURL: "https://github.test/edited-without-marker", + CreatedAt: "2026-08-19T00:00:00Z", + UpdatedAt: "2026-08-19T00:00:01Z", + User: struct { + Login string `json:"login"` + }{Login: "HemSoft"}, + }}, statuses: []reviewRequestStatus{registeredReviewRequestStatus(123)}} + installReviewFakes(t, rest) + + if err := runReview([]string{"--repo", "HemSoft/consumer", "--retry", "94"}, io.Discard, io.Discard); err != nil { + t.Fatalf("runReview() marker-removed retry error = %v", err) + } + if rest.posts != 1 { + t.Fatalf("marker-removed retry posts = %d, want 1", rest.posts) + } + if rest.reactionGets != 60 { + t.Fatalf("marker-removed reaction GETs = %d, want 60", rest.reactionGets) + } +} + +func TestRunReviewIgnoresPriorHeadRequest(t *testing.T) { + oldHead := strings.Repeat("c", 40) + rest := &reviewREST{comments: []reviewTriggerComment{{ + ID: 123, + Body: codexReviewCommand + "\n\n" + codexReviewMarker(oldHead, strings.Repeat("a", 40), "none"), + HTMLURL: "https://github.test/old-head", + CreatedAt: "2026-08-19T00:00:00Z", + UpdatedAt: "2026-08-19T00:00:00Z", + User: struct { + Login string `json:"login"` + }{Login: "HemSoft"}, + }}} + installReviewFakes(t, rest) + + if err := runReview([]string{"--repo", "HemSoft/consumer", "94"}, io.Discard, io.Discard); err != nil { + t.Fatalf("runReview() prior-head request error = %v", err) + } + if rest.posts != 1 || rest.reactionGets != 0 { + t.Fatalf("prior-head request posts/reaction GETs = %d/%d, want 1/0", rest.posts, rest.reactionGets) + } +} + +func TestRunReviewRetryRecoversAfterEditedCommandIsRemoved(t *testing.T) { + rest := &reviewREST{comments: []reviewTriggerComment{{ + ID: 123, + Body: "request edited beyond recognition", + HTMLURL: "https://github.test/edited-without-command", + CreatedAt: "2026-08-19T00:00:00Z", + UpdatedAt: "2026-08-19T00:00:01Z", + User: struct { + Login string `json:"login"` + }{Login: "HemSoft"}, + }}, statuses: []reviewRequestStatus{registeredReviewRequestStatus(123)}} + installReviewFakes(t, rest) + + if err := runReview([]string{"--repo", "HemSoft/consumer", "--retry", "94"}, io.Discard, io.Discard); err != nil { + t.Fatalf("runReview() command-removed retry error = %v", err) + } + if rest.posts != 1 { + t.Fatalf("command-removed retry posts = %d, want 1", rest.posts) + } + if rest.reactionGets != 60 { + t.Fatalf("command-removed reaction GETs = %d, want 60", rest.reactionGets) + } +} + +func TestRunReviewRejectsDeletedRegisteredRequest(t *testing.T) { + rest := &reviewREST{statuses: []reviewRequestStatus{registeredReviewRequestStatus(123)}} + installReviewFakes(t, rest) + + err := runReview([]string{"--repo", "HemSoft/consumer", "--retry", "94"}, io.Discard, io.Discard) + if err == nil || !strings.Contains(err.Error(), "registered Codex review request comment 123") || + !strings.Contains(err.Error(), "was deleted") || + !strings.Contains(err.Error(), "push a new commit") { + t.Fatalf("runReview() deleted request error = %v", err) + } + if rest.posts != 0 { + t.Fatalf("deleted request posts = %d, want 0", rest.posts) + } +} + +func TestRunReviewIgnoresUnregisteredEditedDiscussionComment(t *testing.T) { + rest := &reviewREST{comments: []reviewTriggerComment{{ + ID: 123, + Body: "ordinary edited discussion", + HTMLURL: "https://github.test/discussion", + CreatedAt: "2026-08-19T00:00:00Z", + UpdatedAt: "2026-08-19T00:00:01Z", + User: struct { + Login string `json:"login"` + }{Login: "HemSoft"}, + }}} + installReviewFakes(t, rest) + + if err := runReview([]string{"--repo", "HemSoft/consumer", "94"}, io.Discard, io.Discard); err != nil { + t.Fatalf("runReview() ordinary edited comment error = %v", err) + } + if rest.posts != 1 || rest.reactionGets != 0 { + t.Fatalf("ordinary edited comment posts/reaction GETs = %d/%d, want 1/0", rest.posts, rest.reactionGets) + } +} + +func registeredReviewRequestStatus(commentID int64) reviewRequestStatus { + status := reviewRequestStatus{ + Context: codexReviewRequestRegistryContext, + TargetURL: fmt.Sprintf("https://github.test/HemSoft/consumer/pull/94#issuecomment-%d", commentID), + } + status.Creator.Login = "HemSoft" + return status +} + +func TestFindRegisteredCodexRequestIDsMatchesCanonicalURLCase(t *testing.T) { + rest := &reviewREST{statuses: []reviewRequestStatus{registeredReviewRequestStatus(123)}} + + got, err := findRegisteredCodexRequestIDs( + rest, "hemsoft", "consumer", 94, strings.Repeat("b", 40), + ) + if err != nil { + t.Fatalf("findRegisteredCodexRequestIDs() error = %v", err) + } + if !got[123] { + t.Fatalf("registered IDs = %#v, want 123", got) + } +} + +func TestReviewCommentTimeUsesImmutableCreatedAt(t *testing.T) { + created := "2026-08-19T00:00:00Z" + comment := reviewTriggerComment{ + CreatedAt: created, + UpdatedAt: "2026-08-19T01:00:00Z", + } + + if got := reviewCommentTime(comment); !got.Equal(time.Date(2026, 8, 19, 0, 0, 0, 0, time.UTC)) { + t.Fatalf("reviewCommentTime() = %s, want immutable created_at %s", got, created) + } +} + +func TestWaitForCodexRequestCompletionRecognizesFinishedRequest(t *testing.T) { + rest := &reviewREST{reactions: []reviewCommentReaction{{Content: "eyes"}}} + rest.reactions[0].User.ID = 12345 + + if err := waitForCodexRequestCompletion(rest, "HemSoft", "consumer", 123, false); err != nil { + t.Fatalf("waitForCodexRequestCompletion() error = %v", err) + } +} + +func TestWaitForCodexRequestCompletionWaitsForActiveReactionToClear(t *testing.T) { + rest := &reviewREST{ + reactions: []reviewCommentReaction{{Content: "eyes"}}, + clearReaction: true, + } + rest.reactions[0].User.ID = 199175422 + oldPoll := waitForCodexReactionPoll + waitForCodexReactionPoll = func() {} + t.Cleanup(func() { waitForCodexReactionPoll = oldPoll }) + + if err := waitForCodexRequestCompletion(rest, "HemSoft", "consumer", 123, true); err != nil { + t.Fatalf("waitForCodexRequestCompletion() error = %v", err) + } + if rest.reactionGets != 2 { + t.Fatalf("reaction GETs = %d, want 2", rest.reactionGets) + } +} + +func TestWaitForCodexRequestCompletionAllowsMaterializationGrace(t *testing.T) { + rest := &reviewREST{} + oldPoll := waitForCodexReactionPoll + waitForCodexReactionPoll = func() {} + t.Cleanup(func() { waitForCodexReactionPoll = oldPoll }) + + if err := waitForCodexRequestCompletion(rest, "HemSoft", "consumer", 123, true); err != nil { + t.Fatalf("waitForCodexRequestCompletion() error = %v", err) + } + if rest.reactionGets != 60 { + t.Fatalf("reaction GETs = %d, want 60", rest.reactionGets) + } +} + +func installReviewFakes(t *testing.T, rest restAPI) { + t.Helper() + oldREST := newRESTClient + oldGHExec := ghExec + oldWaitForRetryOrdering := waitForRetryOrdering + oldWaitForCodexReactionPoll := waitForCodexReactionPoll + oldWaitForReviewInvalidationPoll := waitForReviewInvalidationPoll + newRESTClient = func() (restAPI, error) { return rest, nil } + waitForRetryOrdering = func() {} + waitForCodexReactionPoll = func() {} + waitForReviewInvalidationPoll = func() {} + ghExec = func(args ...string) (bytes.Buffer, bytes.Buffer, error) { + if strings.Join(args, " ") != "api user --jq .login" { + return bytes.Buffer{}, bytes.Buffer{}, fmt.Errorf("unexpected gh call: %v", args) + } + return *bytes.NewBufferString("HemSoft\n"), bytes.Buffer{}, nil + } + t.Cleanup(func() { + newRESTClient = oldREST + ghExec = oldGHExec + waitForRetryOrdering = oldWaitForRetryOrdering + waitForCodexReactionPoll = oldWaitForCodexReactionPoll + waitForReviewInvalidationPoll = oldWaitForReviewInvalidationPoll + }) +} diff --git a/gh-sfl/reviewer_contract_test.go b/gh-sfl/reviewer_contract_test.go index 7bfbe1e6..a299a2cc 100644 --- a/gh-sfl/reviewer_contract_test.go +++ b/gh-sfl/reviewer_contract_test.go @@ -1,882 +1,422 @@ package main import ( - "bytes" + "encoding/json" "os" + "os/exec" "path/filepath" - "regexp" "strings" "testing" - - "gopkg.in/yaml.v3" ) -func TestReviewerDeploymentContract(t *testing.T) { - t.Helper() - - root := filepath.Clean("..") - assertFilesEqual(t, - filepath.Join(root, "deployment", "workflows", "sfl-pr-review.md"), - filepath.Join(root, ".github", "workflows", "sfl-pr-review.md"), - ) - assertFilesEqual(t, - filepath.Join(root, "deployment", "infrastructure", "sfl-pr-review-auto.yml"), - filepath.Join(root, ".github", "workflows", "sfl-pr-review-auto.yml"), - ) - assertFilesEqual(t, - filepath.Join(root, "deployment", "infrastructure", "sfl-pr-review-recovery.yml"), - filepath.Join(root, ".github", "workflows", "sfl-pr-review-recovery.yml"), - ) - - source := readContractFile(t, filepath.Join(root, "deployment", "workflows", "sfl-pr-review.md")) - sourceText := normalizeLineEndings(source) - for _, required := range []string{ - "base_sha:\n description: Expected pull request base commit", - "head_sha:\n description: Expected pull request head commit", - "dispatch_id:\n description: Unique wrapper or recovery dispatch identifier", - "retry_count:\n description: Missing-review-output retry counter", - "review_effort:\n description: Audit effort marker retained in provenance and recovery", - "default: low\n type: choice", - "options:\n - low\n - medium\n - high", - "SFL_REVIEW_EFFORT: ${{ inputs.review_effort || 'low' }}", - "pre-steps:", - "Validate trusted review context", - "Install ripgrep with bounded diagnostics", - "timeout --signal=TERM --kill-after=15s 180s", - "Retain ripgrep setup diagnostics", - "sfl-ripgrep-setup-${{ github.run_id }}-${{ github.run_attempt }}", - "Install threat-detection ripgrep with bounded diagnostics", - "Retain threat-detection ripgrep setup diagnostics", - "sfl-threat-detection-ripgrep-setup-${{ github.run_id }}-${{ github.run_attempt }}", - `--argjson item_number "$ITEM_NUMBER"`, - `item_type: "pull_request"`, - `item_number: $item_number`, - `base_sha: $base_sha`, - `head_sha: $head_sha`, - "Untrusted or inconsistent review context", - `case "$REVIEW_EFFORT" in`, - `low|medium|high) ;;`, - "-f review_effort=low", - `run-name: "SFL PR Review #${{ inputs.item_number }} ${{ inputs.base_sha }}:${{ inputs.head_sha }} retry=${{ inputs.retry_count }} dispatch=${{ inputs.dispatch_id }}"`, - "publish_review_provenance:", - "Initialize review evidence", - "status: 'in_progress'", - "`sfl-review:${pullNumber}:${baseSha}:${headSha}:${runId}`", - "SFL_RUN_ID: ${{ github.run_id }}", - "evidence-check-run-id: ${{ steps.initialize-evidence.outputs.check-run-id }}", - "name: sfl-review-run-provenance", - "workflow_run_id: $workflow_run_id", - "base_sha: $base_sha", - `[[ "$PR_NUMBER" =~ ^[1-9][0-9]*$ ]]`, - "continue-on-error: false", - "commit-id: \"${{ inputs.head_sha }}\"", - "resolve-sfl-review-thread:", - "if: needs.safe_outputs.result == 'success'", - "pull-requests: write", - "Thread ${threadId} is not an obsolete SFL finding on PR", - "Verify pull request base and head before safe outputs", - "still-applicable unresolved SFL", - "`side: LEFT` for a deleted line", - "countUnresolvedSflFindings", - "replace(/\\[bot\\]$/i, '')", - "name: safe-outputs-items", - "item => item.type === 'submit_pull_request_review'", - "submittedReview.metadata?.review_id", - "Review body contains unresolved template placeholder", - "const publishFailure = async ({ reason }) =>", - "name: 'SFL Review Evidence'", - "github.rest.checks.get", - "github.rest.checks.update", - "for (let attempt = 1; attempt <= 3; attempt += 1)", - "Initialized SFL review evidence does not match this run", - "external_id: gateExternalId", - "core.setFailed(summary)", - "github-token: ${{ steps.app-token.outputs.token }}", - "github-token: ${{ github.token }}", - "checks: write", - "permission-contents: read", - "'replacement-retry-eligible'", - "SFL_AGENT_OUTPUT: /tmp/gh-aw/agent_output.json", - "Download reviewer agent output", - "fs.readFileSync(process.env.SFL_AGENT_OUTPUT", - "item.type === 'missing_data'", - "item.type === 'missing_tool'", - "item.type === 'report_incomplete'", - "let replacementRetryEligible = false", - "Expected exactly one immutable agent submitted-review item", - "(agentSubmittedReview.repo ?? configuredRepo) !== configuredRepo", - "(item.repo ?? configuredRepo) !== configuredRepo ||", - "Published review-comment count ${publishedReviewComments.length} does not match immutable agent count ${agentReviewComments.length}", - "const originalBody = agentSubmittedReview.body || ''", - "Published review manifest does not match the immutable agent verdict", - "const immutableFindingCount = agentReviewComments.length", - "publishedFindingCount !== immutableFindingCount", - "replacement_retry_eligible=${replacementRetryEligible}", - "SFL_REPLACEMENT_RETRY_ELIGIBLE === 'true'", - "-f base_sha=PULL_REQUEST_BASE_SHA", - `"base_sha":"PULL_REQUEST_BASE_SHA"`, - "make exactly one successful `submit_pull_request_review` call", - "call `missing_data` with the specific blocker", - "`noop` and never silently stop", - "Run the embedded Unslop pass on every user-facing sentence", - "Protocol literals override the Unslop rules", - `Ask, "What makes this obviously AI generated?"`, - "immutable-head `SFL Reviewer Approval` check", - } { - if !strings.Contains(sourceText, required) { - t.Errorf("canonical reviewer is missing contract text %q", required) - } - } +type observerResult struct { + Action string `json:"action"` + Reason string `json:"reason"` +} - for _, placeholder := range reviewerTemplatePlaceholders { - if !strings.Contains(sourceText, "'"+placeholder+"'") { - t.Errorf("canonical reviewer does not validate template placeholder %q", placeholder) - } - } - if strings.Contains(sourceText, "require('@actions/github')") { - t.Error("review metadata dynamically imports an unavailable github-script module") - } - if strings.Contains(sourceText, "permission-checks: write") { - t.Error("shared SFL App token still requests forgeable Checks write access") +func readContractFile(t *testing.T, path string) []byte { + t.Helper() + content, err := os.ReadFile(path) + if err != nil { + t.Fatalf("read %s: %v", path, err) } + return content +} - compiled := readContractFile(t, filepath.Join(root, ".github", "workflows", "sfl-pr-review.lock.yml")) - compiledText := normalizeLineEndings(compiled) - for _, required := range []string{ - "GH_AW_INPUTS_BASE_SHA: ${{ inputs.base_sha }}", - "GH_AW_INPUTS_HEAD_SHA: ${{ inputs.head_sha }}", - "SFL_REVIEW_EFFORT: ${{ inputs.review_effort || 'low' }}", - "Validate trusted review context", - "Untrusted or inconsistent review context", - "Install ripgrep with bounded diagnostics", - "timeout --signal=TERM --kill-after=15s 180s", - "Retain ripgrep setup diagnostics", - "sfl-ripgrep-setup-${{ github.run_id }}-${{ github.run_attempt }}", - "Install threat-detection ripgrep with bounded diagnostics", - "Retain threat-detection ripgrep setup diagnostics", - "sfl-threat-detection-ripgrep-setup-${{ github.run_id }}-${{ github.run_attempt }}", - `run-name: "SFL PR Review #${{ inputs.item_number }} ${{ inputs.base_sha }}:${{ inputs.head_sha }} retry=${{ inputs.retry_count }} dispatch=${{ inputs.dispatch_id }}"`, - "{{#runtime-import .github/workflows/sfl-pr-review.md}}", - "publish_review_provenance:", - "Initialize review evidence", - "status: 'in_progress'", - "`sfl-review:${pullNumber}:${baseSha}:${headSha}:${runId}`", - "SFL_RUN_ID: ${{ github.run_id }}", - "evidence-check-run-id: ${{ steps.initialize-evidence.outputs.check-run-id }}", - "name: sfl-review-run-provenance", - "RETRY_COUNT: ${{ inputs.retry_count }}", - "\"commit_id\":\"${GH_AW_INPUT_HEAD_SHA}\"", - "GH_AW_DETECTION_CONTINUE_ON_ERROR: \"false\"", - "Verify pull request base and head before safe outputs", - "Could not enumerate unresolved SFL findings", - "SFL_BASE_SHA: ${{ inputs.base_sha }}", - "SFL_HEAD_SHA: ${{ inputs.head_sha }}", - "SFL_SAFE_OUTPUT_ITEMS: /tmp/sfl-review-safe-outputs/safe-output-items.jsonl", - "const publishFailure = async ({ reason }) =>", - "name: 'SFL Review Evidence'", - "github.rest.checks.get", - "github.rest.checks.update", - "for (let attempt = 1; attempt <= 3; attempt += 1)", - "Initialized SFL review evidence does not match this run", - "external_id: gateExternalId", - "core.setFailed(summary)", - "permission-contents: read", - "'replacement-retry-eligible'", - "SFL_AGENT_OUTPUT: /tmp/gh-aw/agent_output.json", - "Download reviewer agent output", - "fs.readFileSync(process.env.SFL_AGENT_OUTPUT", - "let replacementRetryEligible = false", - "Expected exactly one immutable agent submitted-review item", - "agentSubmittedReview.repo ?? configuredRepo", - "const originalBody = agentSubmittedReview.body || ''", - "const immutableFindingCount = agentReviewComments.length", - "replacement_retry_eligible=${replacementRetryEligible}", - "SFL_REPLACEMENT_RETRY_ELIGIBLE === 'true'", - } { - if !strings.Contains(compiledText, required) { - t.Errorf("compiled reviewer is missing contract text %q", required) - } - } - if strings.Contains(compiledText, "require('@actions/github')") { - t.Error("compiled review metadata dynamically imports an unavailable github-script module") - } - if strings.Contains(compiledText, "permission-checks: write") { - t.Error("compiled reviewer still requests forgeable Checks write access for the shared SFL App") - } - if !strings.Contains( - compiledText, - "agent:\n needs:\n - activation\n - publish_review_provenance", - ) { - t.Error("reviewer agent can run before current review evidence is initialized") - } - validationIndex := strings.Index(compiledText, "name: Validate trusted review context") - if validationIndex < 0 { - t.Fatal("compiled reviewer is missing trusted review context validation") - } - for _, laterStep := range []string{ - "name: Checkout repository", - "name: Checkout PR branch", - "name: Execute GitHub Copilot CLI", - } { - stepIndex := strings.Index(compiledText, laterStep) - if stepIndex < 0 { - t.Errorf("compiled reviewer is missing ordering target %q", laterStep) - } else if validationIndex > stepIndex { - t.Errorf("trusted review context validation runs after %q", laterStep) - } - } - agentIndex := strings.Index(compiledText, "\n agent:\n") - detectionIndex := strings.Index(compiledText, "\n detection:\n") - if agentIndex < 0 { - t.Fatal("compiled reviewer is missing the agent job") - } - if detectionIndex < 0 { - t.Fatal("compiled reviewer is missing the threat-detection job") - } - agentText := compiledText[agentIndex:detectionIndex] - boundedAgentSetupIndex := strings.Index( - agentText, - "name: Install ripgrep with bounded diagnostics", - ) - generatedAgentSetupIndex := strings.Index(agentText, "name: Install ripgrep\n") - if boundedAgentSetupIndex < 0 || generatedAgentSetupIndex < 0 { - t.Fatal("compiled agent job is missing bounded or generated ripgrep setup") - } - if boundedAgentSetupIndex > generatedAgentSetupIndex { - t.Error("bounded agent ripgrep setup runs after the generated installer") - } - detectionText := compiledText[detectionIndex:] - boundedDetectionSetupIndex := strings.Index( - detectionText, - "name: Install threat-detection ripgrep with bounded diagnostics", - ) - generatedDetectionSetupIndex := strings.Index(detectionText, "name: Install ripgrep\n") - if boundedDetectionSetupIndex < 0 || generatedDetectionSetupIndex < 0 { - t.Fatal("compiled threat-detection job is missing bounded or generated ripgrep setup") - } - if boundedDetectionSetupIndex > generatedDetectionSetupIndex { - t.Error("bounded threat-detection ripgrep setup runs after the generated installer") - } - boundedDetectionStepEnd := strings.Index( - detectionText, - "name: Retain threat-detection ripgrep setup diagnostics", - ) - if boundedDetectionStepEnd < 0 || boundedDetectionStepEnd <= boundedDetectionSetupIndex { - t.Fatal("compiled threat-detection job is missing the bounded setup diagnostics step") - } - boundedDetectionStep := detectionText[boundedDetectionSetupIndex:boundedDetectionStepEnd] - if !strings.Contains(boundedDetectionStep, "if: always()") { - t.Error("bounded threat-detection ripgrep setup is skipped before the unconditional generated installer") - } - if strings.Contains(boundedDetectionStep, "run_detection") { - t.Error("bounded threat-detection ripgrep setup still inherits the detection guard") - } +func normalizeLineEndings(content []byte) string { + return strings.ReplaceAll(string(content), "\r\n", "\n") +} - trigger := readContractFile(t, filepath.Join(root, "deployment", "infrastructure", "sfl-pr-review-auto.yml")) - triggerText := normalizeLineEndings(trigger) - var triggerWorkflow any - if err := yaml.Unmarshal(trigger, &triggerWorkflow); err != nil { - t.Fatalf("parse automatic reviewer workflow: %v", err) - } - for _, required := range []string{ - "issue_comment:", - "types: [created]", - "pull_request_target:", - "pull_request_review:", - "BASE_SHA: ${{ github.event.pull_request.base.sha }}", - "HEAD_SHA: ${{ github.event.pull_request.head.sha }}", - `if [ "$BASE_REF" != "$DEFAULT_BRANCH" ]; then`, - "types: [opened, synchronize, reopened, ready_for_review, edited, review_requested, labeled]", - "github.event.action != 'labeled'", - "github.event.label.name == 'sfl-review'", - "github.event.comment.body == '@sfl-app review'", - `contains(fromJSON('["OWNER","MEMBER","COLLABORATOR"]'), github.event.comment.author_association)`, - "github.event_name != 'issue_comment'", - "Validate command and seal pull request state", - "sfl-review-command:${COMMAND_COMMENT_ID}", - `DISPATCH_ID="command-${COMMAND_COMMENT_ID}"`, - "find_command_acknowledgement", - "find_command_run", - `select(.display_title | endswith(" dispatch=" + $dispatch_id))`, - "command_acknowledgement_complete", - "command_acknowledgement_run_id", - `test("\\[Actions run [1-9][0-9]*\\]\\(")`, - `(capture("\\[Actions run (?[1-9][0-9]*)\\]\\(")? | .id) // empty`, - `test("^sfl-app\\[bot\\]$"; "i")`, - "Command comment ${COMMAND_COMMENT_ID} was already acknowledged", - "for ATTEMPT in $(seq 1 120); do", - `-f "inputs[dispatch_id]=${DISPATCH_ID}"`, - `-f "inputs[review_effort]=${REVIEW_EFFORT}"`, - "expected-review-run-id: ${{ steps.request-review.outputs.review-run-id }}", - "needs: [comment-command, dispatch]", - "needs.comment-command.outputs.expected-review-run-id", - "Could not resolve the exact comment-dispatched SFL review run", - "SFL review was requested for `%s`, but its Actions run could not be correlated.", - "[Command run](%s)", - "SFL review requested for `%s`: [Actions run %s](%s)", - "vars.SFL_ENABLED != 'false'", - "cancel-in-progress: false", - "name: SFL Reviewer Gate Runner", - "name: Resolve immutable approval context", - `PR_NUMBER: ${{ github.event.pull_request.number || needs.comment-command.outputs.pr-number || github.event.issue.number }}`, - `PR=$(gh api "repos/${REPOSITORY}/pulls/${PR_NUMBER}")`, - "Could not resolve immutable SFL approval context", - "BASE_SHA: ${{ steps.approval-context.outputs.base-sha }}", - "HEAD_SHA: ${{ steps.approval-context.outputs.head-sha }}", - "if: always()", - "SFL_ENABLED: ${{ vars.SFL_ENABLED }}", - `if [ "$SFL_ENABLED" = "false" ]; then`, - "required reviewer gate fails closed", - "DISPATCH_RESULT: ${{ needs.dispatch.result }}", - `if [ "$EVENT_NAME" != "issue_comment" ]`, - `SFL dispatch job concluded ${DISPATCH_RESULT}; refusing to bootstrap`, - "timeout-minutes: 120", - "for ATTEMPT in $(seq 1 440); do", - "Wait for exact SFL review run", - "validate_review_run", - `.path == ".github/workflows/sfl-pr-review.lock.yml"`, - `.event == "workflow_dispatch"`, - ".head_repository.full_name == $repository", - ".head_branch == $default_branch", - `actions/runs/${EXPECTED_REVIEW_RUN_ID}`, - "--paginate --jq '.workflow_runs[]'", - "Actions run lookup failed", - "Exact SFL review run concluded", - "expected-review-run-id:", - "review-run-id=", - "permission-contents: read", - "permission-pull-requests: write", - "Reset stale SFL review state after a push", - `issues/${PR_NUMBER}/labels?per_page=100`, - "--paginate", - "contains(github.event.pull_request.labels.*.name, 'sfl-pr')", - `grep -Eq '^(sfl-done|sfl-needs-work)$'`, - `labels[]=sfl-ready-for-review`, - "grep -qE '^\\s+base_sha:\\s*$'", - "dispatch_args+=(-f \"inputs[base_sha]=${BASE_SHA}\")", - "grep -qE '^\\s+head_sha:\\s*$'", - "-f \"ref=${TRUSTED_REF}\"", - "dispatch_args+=(-f \"inputs[head_sha]=${HEAD_SHA}\")", - "grep -qE '^\\s+retry_count:\\s*$'", - "dispatch_args+=(-f \"inputs[retry_count]=0\")", - `EVENT_ACTION: ${{ github.event.action }}`, - "grep -qE '^\\s+review_effort:\\s*$'", - `REVIEW_EFFORT=$(resolve_review_effort "$PR_LABELS")`, - `dispatch_args+=(-f "inputs[review_effort]=${REVIEW_EFFORT}")`, - `DISPATCH_ID="auto-${GITHUB_RUN_ID}"`, - `dispatch_args+=(-f "inputs[dispatch_id]=${DISPATCH_ID}")`, - `select(.display_title | endswith(" dispatch=" + $dispatch_id))`, - `issues/${PR_NUMBER}/labels/sfl-review`, - "consume_review_label", - `consume_review_label "$PR_LABELS"`, - `grep -Fxq "sfl-review"`, - `"${EXPECTED_TITLE}" "$REVIEW_LABEL_PRESENT"`, - "selected at request time, even", - "stale queued labeled event whose trigger was consumed", - "cat > /dev/null", - `grep -q 'HTTP 404'`, - "sfl-review trigger label is already absent", - "Could not consume sfl-review trigger label", - `EXPECTED_TITLE="SFL PR Review #${PR_NUMBER} ${BASE_SHA}:${HEAD_SHA} retry="`, - `BEFORE_RUN_ID=$(gh api`, - `--argjson before_run_id "$BEFORE_RUN_ID"`, - `select(.id > $before_run_id)`, - "skipping duplicate dispatch", - "for STATUS in $(reusable_review_statuses)", - `runs?status=${STATUS}&per_page=100`, - "correctness hazard worse than an occasional duplicate", - "# BEGIN TESTABLE DISPATCH DEDUP", - "find_reusable_review_run", - } { - if !strings.Contains(triggerText, required) { - t.Errorf("automatic reviewer trigger is missing contract text %q", required) - } - } - if strings.Contains(triggerText, "ref=${BASE_REF}") { - t.Error("automatic reviewer trigger still dispatches from the pull request base ref") - } - if strings.Contains(triggerText, "created_at >= $dispatched_at") { - t.Error("automatic reviewer trigger still trusts the runner clock to resolve a dispatched run") - } - if count := strings.Count(triggerText, "github.event.label.name == 'sfl-review'"); count != 3 { - t.Errorf("sfl-review label gate appears %d times, want concurrency, dispatch, and approval guards", count) - } - if count := strings.Count(triggerText, "github.event.action != 'labeled'"); count != 2 { - t.Errorf("unrelated-label guard appears %d times, want dispatch and approval guards", count) - } - if count := strings.Count(triggerText, "consume_review_label"); count != 3 { - t.Errorf("sfl-review label consumer appears %d times, want definition plus active and new dispatch paths", count) +func TestCodexObserverCanonicalAndStagedMatch(t *testing.T) { + root := filepath.Join("..") + canonicalBytes := readContractFile(t, filepath.Join(root, "deployment", "infrastructure", "sfl-pr-review-auto.yml")) + staged := readContractFile(t, filepath.Join(root, ".github", "workflows", "sfl-pr-review-auto.yml")) + if normalizeLineEndings(canonicalBytes) != normalizeLineEndings(staged) { + t.Fatal("staged Codex observer differs from canonical deployment source") } + canonical := string(canonicalBytes) - recovery := normalizeLineEndings(readContractFile(t, - filepath.Join(root, "deployment", "infrastructure", "sfl-pr-review-recovery.yml"), - )) for _, required := range []string{ - `workflows: ["SFL PR Review"]`, + "name: SFL Codex Review Observer", + "format('SFL Codex review request #{0}', github.event.issue.number)", + "types: [opened, reopened, edited, synchronize]", + "invalidate-base-advance:", + "github.event.sender.id == 199175422", + "Fork pull requests are unsupported", + "SFL reviews require the default branch", + "appId = 1144995", + "appSlug = \"chatgpt-codex-connector\"", + "appOwner = \"openai\"", + "name: \"SFL Reviewer Gate Runner\"", + "check.app.id === 15368", + "Codex artifact is stale", + "Codex reported review findings", + "SFL Codex review invalidated", + "requestMatchesCurrentBase", + "invalidate-review-request:", "vars.SFL_ENABLED != 'false'", - "github.event.workflow_run.conclusion == 'failure'", - "github.event.workflow_run.conclusion == 'timed_out'", - "github.event.workflow_run.conclusion == 'cancelled'", - "github.event.workflow_run.path == '.github/workflows/sfl-pr-review.lock.yml'", - "github.event.workflow_run.event == 'workflow_dispatch'", - "github.event.workflow_run.head_repository.full_name == github.repository", - "github.event.workflow_run.head_branch == github.event.repository.default_branch", - "Finalize interrupted review evidence", - "FAILED_RUN_TITLE: ${{ github.event.workflow_run.display_title }}", - "const checkRuns = await github.paginate(", - "github.rest.checks.listForRef", - "check.external_id === externalId && check.app?.id === 15368", - "status: 'completed'", - "conclusion: 'failure'", - "if: github.event.workflow_run.conclusion != 'cancelled'", - "--name sfl-review-run-provenance", - "--name safe-outputs-items", - "--name agent", - "agent_output.json", - `SFL_APP_SLUG: ${{ steps.app-token.outputs.app-slug }}`, - `pulls/${PR_NUMBER}/reviews?per_page=100`, - `contains("/actions/runs/" + $workflow_run_id)`, - `reconcile_review_count`, - `PUBLISHED_REVIEW_COUNT`, - `reconciling published reviews`, - `select(.type == "submit_pull_request_review")`, - `.type == "missing_data"`, - `.type == "report_incomplete"`, - `if type == "array" then`, - `elif type == "object" and (.items | type) == "array" then`, - `error("agent output must be an array or contain an items array")`, - `terminal_signal_count`, - `echo "reported"`, - `if [ "$retry_count" -eq 0 ]; then`, - `echo "exhausted"`, - `(.base_sha | test("^[0-9a-f]{40}$"))`, - `(.review_effort | test("^(low|medium|high)$"))`, - `inputs[base_sha]=${BASE_SHA}`, - `inputs[retry_count]=1`, - `REVIEW_EFFORT=$(jq -r '.review_effort' "$PROVENANCE")`, - `dispatch_args+=(-f "inputs[review_effort]=${REVIEW_EFFORT}")`, - `dispatch_args+=(-f "inputs[dispatch_id]=recovery-${GITHUB_RUN_ID}")`, - `A newer review run already owns PR`, - `--name sfl-review-run-provenance`, - `--paginate`, - `--slurp`, - `.workflow_runs[]`, - `NEWER_PROVENANCE_ATTEMPTS=12`, - `NEWER_PROVENANCE_LOADED="false"`, - `newer_run_suppresses_retry`, - "The sealed title is computed first", - `EXPECTED_REVIEW_TITLE_PREFIX="SFL PR Review #${PR_NUMBER} ${BASE_SHA}:${HEAD_SHA} retry="`, - `NEWER_TITLE_MATCH`, - `A newer matching review for PR #${PR_NUMBER} at ${BASE_SHA}:${HEAD_SHA} suppresses duplicate retry`, - `PR #${PR_NUMBER} is no longer eligible`, + "SFL Codex Review Request Registry", + "const terminalRequestIdentity = `:request:${commentId}:at:`", + "registrations.has(comment.id)", + "openPullCount", + "if: github.event_name == 'push'", } { - if !strings.Contains(recovery, required) { - t.Errorf("review recovery workflow is missing contract text %q", required) + if !strings.Contains(canonical, required) { + t.Errorf("Codex observer is missing %q", required) } } - if strings.Contains(recovery, `.conclusion != "cancelled"`) { - t.Error("recovery ignores newer cancelled reviews and can dispatch an unwanted replacement") - } - -} -func TestResolverTokenSeparation(t *testing.T) { - t.Helper() - - root := filepath.Clean("..") - const tokenStepName = "Generate SFL App token for thread validation" - const identityStepName = "Resolve SFL App identity" - const resolverStepName = "Validate and resolve requested SFL threads" - - sourcePath := filepath.Join(root, "deployment", "workflows", "sfl-pr-review.md") - source := normalizeLineEndings(readContractFile(t, sourcePath)) - if count := strings.Count(source, tokenStepName); count != 1 { - t.Fatalf("%s contains %d resolver token steps, want 1", sourcePath, count) - } - tokenStepStart := strings.Index(source, tokenStepName) - tokenStepEnd := strings.Index(source[tokenStepStart:], resolverStepName) - if tokenStepEnd < 0 { - t.Fatalf("%s resolver token step boundary is missing", sourcePath) - } - tokenStep := source[tokenStepStart : tokenStepStart+tokenStepEnd] - for _, permission := range []string{ - "permission-contents: read", - "permission-pull-requests: read", + for _, forbidden := range []string{ + "OPENROUTER_API_KEY", + "openrouter.ai", + "moonshotai/kimi", + "sfl-pr-review.lock.yml", + "SFL_APP_PRIVATE_KEY", } { - if !strings.Contains(tokenStep, permission) { - t.Errorf("%s resolver token step is missing %q", sourcePath, permission) + if strings.Contains(canonical, forbidden) { + t.Errorf("Codex observer retained forbidden legacy reviewer text %q", forbidden) } } - for _, contract := range []string{ - "contents: write", - "github-token: ${{ steps.validation-token.outputs.token }}", - "SFL_APP_LOGIN: ${{ steps.app-identity.outputs.login }}", - "github-token: ${{ github.token }}", - "isOutdated", - "(!thread.isResolved && !thread.isOutdated)", - "const unresolvedOutdatedThreadIds = []", - "for (const threadId of unresolvedOutdatedThreadIds)", - "github.rest.repos.getContent", - "no longer exists at head ${expectedHead}; human resolution is required", - "const failedThreadIds = []", - "for (let attempt = 1; attempt <= 3; attempt += 1)", - "failedThreadIds.push(threadId)", - } { - if !strings.Contains(source, contract) { - t.Errorf("%s resolver is missing %q", sourcePath, contract) - } - } - - compiledPath := filepath.Join(root, ".github", "workflows", "sfl-pr-review.lock.yml") - var compiled struct { - Jobs map[string]struct { - Permissions map[string]string `yaml:"permissions"` - Steps []struct { - ID string `yaml:"id"` - Name string `yaml:"name"` - Env map[string]any `yaml:"env"` - Uses string `yaml:"uses"` - With map[string]any `yaml:"with"` - } `yaml:"steps"` - } `yaml:"jobs"` - } - if err := yaml.Unmarshal(readContractFile(t, compiledPath), &compiled); err != nil { - t.Fatalf("parse compiled reviewer workflow: %v", err) - } - job, ok := compiled.Jobs["resolve_sfl_review_thread"] - if !ok { - t.Fatal("compiled reviewer is missing resolve_sfl_review_thread job") - } - for permission, expected := range map[string]string{ - "contents": "write", - "pull-requests": "write", + for _, required := range []string{ + `status: "in_progress"`, + "const confirmedState = await publicationState();", + "const postSuccessState = await publicationState();", + "await failPublishedSuccess(publicationChangeReason(postSuccessState));", } { - if actual := job.Permissions[permission]; actual != expected { - t.Errorf("compiled resolver job permission %s = %q, want %q", permission, actual, expected) + if !strings.Contains(canonical, required) { + t.Errorf("Codex observer is missing two-phase publication contract %q", required) } } - - var tokenSteps, identitySteps, resolverSteps int - for _, step := range job.Steps { - switch step.Name { - case tokenStepName: - tokenSteps++ - if step.ID != "validation-token" { - t.Errorf("compiled resolver token step id = %q, want validation-token", step.ID) - } - const tokenAction = "actions/create-github-app-token@bcd2ba49218906704ab6c1aa796996da409d3eb1" - if step.Uses != tokenAction { - t.Errorf("compiled resolver token step uses %q, want %q", step.Uses, tokenAction) - } - expectedPermissions := map[string]string{ - "permission-contents": "read", - "permission-pull-requests": "read", - } - var permissionCount int - for permission, actual := range step.With { - if !strings.HasPrefix(permission, "permission-") { - continue - } - permissionCount++ - expected, ok := expectedPermissions[permission] - if !ok { - t.Errorf("compiled resolver token has unexpected permission %q", permission) - continue - } - if actual != expected { - t.Errorf("compiled resolver token %s = %v, want %q", permission, actual, expected) - } - } - if permissionCount != len(expectedPermissions) { - t.Errorf("compiled resolver token has %d permission inputs, want %d", permissionCount, len(expectedPermissions)) - } - for permission, expected := range expectedPermissions { - if actual := step.With[permission]; actual != expected { - t.Errorf("compiled resolver token %s = %v, want %q", permission, actual, expected) - } - } - case identityStepName: - identitySteps++ - const expectedToken = "${{ steps.validation-token.outputs.token }}" - if actual := step.With["github-token"]; actual != expectedToken { - t.Errorf("compiled identity github-token = %v, want %q", actual, expectedToken) - } - case resolverStepName: - resolverSteps++ - const expectedToken = "${{ github.token }}" - if actual := step.With["github-token"]; actual != expectedToken { - t.Errorf("compiled resolver github-token = %v, want %q", actual, expectedToken) - } - const expectedLogin = "${{ steps.app-identity.outputs.login }}" - if actual := step.Env["SFL_APP_LOGIN"]; actual != expectedLogin { - t.Errorf("compiled resolver SFL_APP_LOGIN = %v, want %q", actual, expectedLogin) - } - } - } - if tokenSteps != 1 { - t.Errorf("compiled resolver has %d token steps, want 1", tokenSteps) - } - if identitySteps != 1 { - t.Errorf("compiled resolver has %d identity steps, want 1", identitySteps) - } - if resolverSteps != 1 { - t.Errorf("compiled resolver has %d mutation steps, want 1", resolverSteps) - } } -func TestAutoTriggerTokenPermissions(t *testing.T) { - t.Helper() - - root := filepath.Clean("..") - path := filepath.Join(root, "deployment", "infrastructure", "sfl-pr-review-auto.yml") - var workflow struct { - Permissions map[string]string `yaml:"permissions"` - Jobs map[string]struct { - Permissions map[string]string `yaml:"permissions"` - Steps []struct { - ID string `yaml:"id"` - Name string `yaml:"name"` - Uses string `yaml:"uses"` - With map[string]any `yaml:"with"` - } `yaml:"steps"` - } `yaml:"jobs"` - } - if err := yaml.Unmarshal(readContractFile(t, path), &workflow); err != nil { - t.Fatalf("parse auto-trigger workflow: %v", err) - } - if len(workflow.Permissions) != 0 { - t.Errorf("auto-trigger workflow has workflow-level permissions %v, want none", workflow.Permissions) - } - job, ok := workflow.Jobs["dispatch"] - if !ok { - t.Fatal("auto-trigger workflow is missing dispatch job") - } - if actual := job.Permissions["contents"]; actual != "read" { - t.Errorf("auto-trigger dispatch contents permission = %q, want read", actual) - } - - const tokenAction = "actions/create-github-app-token@bcd2ba49218906704ab6c1aa796996da409d3eb1" - expectedPermissions := map[string]string{ - "permission-actions": "write", - "permission-contents": "read", - "permission-pull-requests": "write", - } - var tokenSteps int - for _, step := range job.Steps { - if step.Name != "Mint SFL GitHub App token" { - continue - } - tokenSteps++ - if step.ID != "app-token" { - t.Errorf("auto-trigger token step id = %q, want app-token", step.ID) - } - if step.Uses != tokenAction { - t.Errorf("auto-trigger token step uses %q, want %q", step.Uses, tokenAction) - } - var permissionCount int - for permission, actual := range step.With { - if !strings.HasPrefix(permission, "permission-") { - continue - } - permissionCount++ - expected, ok := expectedPermissions[permission] - if !ok { - t.Errorf("auto-trigger token has unexpected permission %q", permission) - continue - } - if actual != expected { - t.Errorf("auto-trigger token %s = %v, want %q", permission, actual, expected) +func TestCodexObserverClassificationFixtures(t *testing.T) { + const head = "a47ba21b6916090642092f03c8b77b24bf658a46" + codexUser := map[string]any{ + "id": 199175422, + "login": "chatgpt-codex-connector[bot]", + } + codexApp := map[string]any{ + "id": 1144995, + "slug": "chatgpt-codex-connector", + "owner": map[string]any{"login": "openai"}, + } + + tests := []struct { + name string + input map[string]any + wantAction string + }{ + { + name: "clean current-head comment passes", + input: map[string]any{ + "eventName": "issue_comment", + "currentHead": head, + "resolvedSha": head, + "inlineCount": 0, + "artifact": map[string]any{ + "user": userCopy(codexUser), + "performed_via_github_app": codexApp, + "body": "Codex Review: Didn't find any major issues. Keep it up!\n\n**Reviewed commit:** `a47ba21b69`", + }, + }, + wantAction: "success", + }, + { + name: "current-head finding review fails", + input: map[string]any{ + "eventName": "pull_request_review", + "currentHead": head, + "resolvedSha": head, + "reviewCommit": head, + "inlineCount": 1, + "artifact": map[string]any{ + "user": userCopy(codexUser), + "state": "COMMENTED", + "body": "Here are some automated review suggestions.\n\n**Reviewed commit:** `a47ba21b69`", + }, + }, + wantAction: "failure", + }, + { + name: "stale clean result is ignored", + input: map[string]any{ + "eventName": "issue_comment", + "currentHead": head, + "resolvedSha": "048484a6ced8de230284f03c7214d76f76987318", + "inlineCount": 0, + "artifact": map[string]any{ + "user": userCopy(codexUser), + "performed_via_github_app": codexApp, + "body": "Codex Review: Didn't find any major issues.\n\n**Reviewed commit:** `048484a6ce`", + }, + }, + wantAction: "ignore", + }, + { + name: "spoofed comment is ignored", + input: map[string]any{ + "eventName": "issue_comment", + "currentHead": head, + "resolvedSha": head, + "inlineCount": 0, + "artifact": map[string]any{ + "user": map[string]any{"id": 42, "login": "chatgpt-codex-connector[bot]"}, + "performed_via_github_app": codexApp, + "body": "Codex Review: Didn't find any major issues.\n\n**Reviewed commit:** `a47ba21b69`", + }, + }, + wantAction: "ignore", + }, + { + name: "malformed comment without a commit binding is ignored", + input: map[string]any{ + "eventName": "issue_comment", + "currentHead": head, + "resolvedSha": "", + "inlineCount": 0, + "artifact": map[string]any{ + "user": userCopy(codexUser), + "performed_via_github_app": codexApp, + "body": "Codex Review returned an unexpected response.", + }, + }, + wantAction: "ignore", + }, + { + name: "malformed current-head review fails closed", + input: map[string]any{ + "eventName": "pull_request_review", + "currentHead": head, + "resolvedSha": "", + "reviewCommit": head, + "inlineCount": 0, + "artifact": map[string]any{ + "user": userCopy(codexUser), + "state": "COMMENTED", + "body": "Codex returned an unexpected review response.", + }, + }, + wantAction: "failure", + }, + { + name: "stale malformed review is ignored", + input: map[string]any{ + "eventName": "pull_request_review", + "currentHead": head, + "resolvedSha": "", + "reviewCommit": "048484a6ced8de230284f03c7214d76f76987318", + "inlineCount": 1, + "artifact": map[string]any{ + "user": userCopy(codexUser), + "state": "COMMENTED", + "body": "Here are some automated review suggestions without a commit marker.", + }, + }, + wantAction: "ignore", + }, + { + name: "current-head result for another base is ignored", + input: map[string]any{ + "eventName": "pull_request_review", + "currentHead": head, + "resolvedSha": head, + "reviewCommit": head, + "requestMatchesCurrentBase": false, + "inlineCount": 0, + "artifact": map[string]any{ + "user": userCopy(codexUser), + "state": "COMMENTED", + "body": "Codex Review: Didn't find any major issues.", + }, + }, + wantAction: "ignore", + }, + { + name: "shared current head fails closed", + input: map[string]any{ + "eventName": "issue_comment", + "currentHead": head, + "resolvedSha": head, + "openPullCount": 2, + "inlineCount": 0, + "artifact": map[string]any{ + "user": userCopy(codexUser), + "performed_via_github_app": codexApp, + "body": "Codex Review: Didn't find any major issues.", + }, + }, + wantAction: "failure", + }, + } + + for _, tc := range tests { + t.Run(tc.name, func(t *testing.T) { + result := runObserverFixture(t, tc.input) + if result.Action != tc.wantAction { + t.Fatalf("observer action = %q (%s), want %q", result.Action, result.Reason, tc.wantAction) } - } - if permissionCount != len(expectedPermissions) { - t.Errorf("auto-trigger token has %d permission inputs, want %d", permissionCount, len(expectedPermissions)) - } - for permission, expected := range expectedPermissions { - if actual := step.With[permission]; actual != expected { - t.Errorf("auto-trigger token %s = %v, want %q", permission, actual, expected) - } - } - } - if tokenSteps != 1 { - t.Errorf("auto-trigger has %d token steps, want 1", tokenSteps) + }) } } -// The approval gate must be published with the repository-scoped github.token, -// never the shared SFL App token. A global substring assertion cannot prove -// this because other compiled steps also receive a github-token input; parse -// the compiled workflow and assert the credential on the exact publication -// steps so the trust-boundary contract fails exactly when either step changes. -func TestCompiledEvidencePublishesWithRepositoryToken(t *testing.T) { - t.Helper() - - root := filepath.Clean("..") - compiled := readContractFile(t, filepath.Join(root, ".github", "workflows", "sfl-pr-review.lock.yml")) - - var workflow struct { - Jobs map[string]struct { - Steps []struct { - Name string `yaml:"name"` - With map[string]any `yaml:"with"` - } `yaml:"steps"` - } `yaml:"jobs"` - } - if err := yaml.Unmarshal(compiled, &workflow); err != nil { - t.Fatalf("parse compiled reviewer workflow: %v", err) - } - - expectedSteps := map[string]int{ - "Initialize review evidence": 0, - "Finalize SFL review evidence": 0, - } - for jobName, job := range workflow.Jobs { - for _, step := range job.Steps { - if _, ok := expectedSteps[step.Name]; !ok { - continue +func TestCodexObserverActiveInvalidationFixtures(t *testing.T) { + tests := []struct { + name string + run map[string]any + pullNumber int + want bool + }{ + { + name: "active push can invalidate every reviewed pull", + run: map[string]any{"event": "push", "status": "in_progress"}, + pullNumber: 42, + want: true, + }, + { + name: "matching active pull context can invalidate", + run: map[string]any{ + "event": "pull_request_target", + "status": "queued", + "pull_requests": []map[string]any{{"number": 42}}, + }, + pullNumber: 42, + want: true, + }, + { + name: "other pull context is irrelevant", + run: map[string]any{ + "event": "pull_request_target", + "status": "waiting", + "pull_requests": []map[string]any{{"number": 41}}, + }, + pullNumber: 42, + want: false, + }, + { + name: "completed push is no longer active", + run: map[string]any{"event": "push", "status": "completed"}, + pullNumber: 42, + want: false, + }, + { + name: "active owner request invalidation blocks observation", + run: map[string]any{ + "event": "issue_comment", "status": "in_progress", + "actor": map[string]any{"login": "HemSoft"}, + "display_title": "SFL Codex review request #42", + }, + pullNumber: 42, + want: true, + }, + { + name: "owner request for another pull is irrelevant", + run: map[string]any{ + "event": "issue_comment", "status": "in_progress", + "actor": map[string]any{"login": "HemSoft"}, + "display_title": "SFL Codex review request #41", + }, + pullNumber: 42, + want: false, + }, + { + name: "ordinary owner comment is not an invalidation", + run: map[string]any{ + "event": "issue_comment", "status": "queued", + "actor": map[string]any{"login": "HemSoft"}, + "display_title": "SFL issue_comment event", + }, + pullNumber: 42, + want: false, + }, + { + name: "Codex result comment is not a request invalidation", + run: map[string]any{ + "event": "issue_comment", "status": "queued", + "actor": map[string]any{"login": "chatgpt-codex-connector[bot]"}, + }, + pullNumber: 42, + want: false, + }, + } + + for _, tc := range tests { + t.Run(tc.name, func(t *testing.T) { + if got := runActiveInvalidationFixture(t, tc.run, tc.pullNumber); got != tc.want { + t.Fatalf("isActiveInvalidationRun() = %t, want %t", got, tc.want) } - expectedSteps[step.Name]++ - token, ok := step.With["github-token"].(string) - if !ok { - t.Errorf("job %q evidence-publication step has no github-token input", jobName) - continue - } - if token != "${{ github.token }}" { - t.Errorf( - "job %q evidence-publication step github-token = %q, want repository-scoped ${{ github.token }}", - jobName, token, - ) - } - } - } - for stepName, matches := range expectedSteps { - if matches != 1 { - t.Fatalf("expected exactly one %s step, found %d", stepName, matches) - } + }) } } -func TestIssueBranchCorrelationPattern(t *testing.T) { - pattern := regexp.MustCompile(`^agent-fix/issue-36(-|$)`) - for _, branch := range []string{ - "agent-fix/issue-36", - "agent-fix/issue-36-retry-output", - } { - if !pattern.MatchString(branch) { - t.Errorf("expected branch %q to match issue correlation", branch) - } - } - for _, branch := range []string{ - "agent-fix/issue-360", - "agent-fix/issue-36retry-output", - "feature/issue-36", - } { - if pattern.MatchString(branch) { - t.Errorf("expected branch %q not to match issue correlation", branch) - } - } +func userCopy(user map[string]any) map[string]any { + return map[string]any{"id": user["id"], "login": user["login"]} } -func assertRunBlocksWithinLimit(t *testing.T, workflow string, limit int) { +func runObserverFixture(t *testing.T, input map[string]any) observerResult { t.Helper() - - runBlock := regexp.MustCompile(`^\s+run:\s*[|>]`) - lines := strings.Split(workflow, "\n") - for index := 0; index < len(lines); index++ { - line := lines[index] - if !runBlock.MatchString(line) { - continue - } - - indent := len(line) - len(strings.TrimLeft(line, " ")) - end := index + 1 - for ; end < len(lines); end++ { - candidate := lines[end] - if strings.TrimSpace(candidate) == "" { - continue - } - candidateIndent := len(candidate) - len(strings.TrimLeft(candidate, " ")) - if candidateIndent <= indent { - break - } - } - if length := len(strings.Join(lines[index+1:end], "\n")); length > limit { - t.Errorf( - "workflow run block at line %d has %d characters, exceeding GitHub's %d-character expression limit", - index+1, - length, - limit, - ) - } - index = end - 1 + if _, ok := input["requestMatchesCurrentBase"]; !ok { + input["requestMatchesCurrentBase"] = true } -} - -var reviewerTemplatePlaceholders = []string{ - "{verdict_icon}", - "{VERDICT}", - "{verdict_reason}", - "{security_critical_or_dash}", - "{security_high_or_dash}", - "{security_medium_or_dash}", - "{security_low_or_dash}", - "{accuracy_critical_or_dash}", - "{accuracy_high_or_dash}", - "{accuracy_medium_or_dash}", - "{accuracy_low_or_dash}", - "{quality_maintainability_critical_or_dash}", - "{quality_maintainability_high_or_dash}", - "{quality_maintainability_medium_or_dash}", - "{quality_maintainability_low_or_dash}", - "{total_critical_or_dash}", - "{total_high_or_dash}", - "{total_medium_or_dash}", - "{total_low_or_dash}", - `{Top 3 most impactful current findings with their severity prefixes, or "No findings."}`, -} - -func TestReviewerTemplatePlaceholderDetection(t *testing.T) { - t.Helper() - - validBody := `Key finding: use {side: "LEFT"} for deleted lines.` - if placeholder := unresolvedReviewerTemplatePlaceholder(validBody); placeholder != "" { - t.Fatalf("ordinary brace-delimited code was treated as a placeholder: %s", placeholder) + if _, ok := input["openPullCount"]; !ok { + input["openPullCount"] = 1 } - - for _, placeholder := range reviewerTemplatePlaceholders { - if actual := unresolvedReviewerTemplatePlaceholder("Review body " + placeholder); actual != placeholder { - t.Errorf("placeholder %q was not detected; got %q", placeholder, actual) - } + workflow := string(readContractFile(t, filepath.Join("..", "deployment", "infrastructure", "sfl-pr-review-auto.yml"))) + startMarker := "// BEGIN TESTABLE CODEX OBSERVER" + endMarker := "// END TESTABLE CODEX OBSERVER" + start := strings.Index(workflow, startMarker) + end := strings.Index(workflow, endMarker) + if start < 0 || end <= start { + t.Fatal("could not locate testable Codex observer block") } -} + source := workflow[start+len(startMarker) : end] + source += "\nconsole.log(JSON.stringify(classifyCodexArtifact(JSON.parse(process.argv[2]))));\n" -func unresolvedReviewerTemplatePlaceholder(body string) string { - for _, placeholder := range reviewerTemplatePlaceholders { - if strings.Contains(body, placeholder) { - return placeholder - } + temp := filepath.Join(t.TempDir(), "observer.js") + if err := os.WriteFile(temp, []byte(source), 0o600); err != nil { + t.Fatalf("write observer fixture: %v", err) } - return "" -} - -func assertFilesEqual(t *testing.T, sourcePath, deployedPath string) { - t.Helper() - - source := readContractFile(t, sourcePath) - deployed := readContractFile(t, deployedPath) - if !bytes.Equal(source, deployed) { - t.Errorf("%s differs from %s", deployedPath, sourcePath) + payload, err := json.Marshal(input) + if err != nil { + t.Fatalf("marshal observer fixture: %v", err) + } + output, err := exec.Command("node", temp, string(payload)).CombinedOutput() + if err != nil { + t.Fatalf("run observer fixture: %v\n%s", err, output) + } + var result observerResult + if err := json.Unmarshal(output, &result); err != nil { + t.Fatalf("parse observer result %q: %v", output, err) } + return result } -func readContractFile(t *testing.T, path string) []byte { +func runActiveInvalidationFixture(t *testing.T, run map[string]any, pullNumber int) bool { t.Helper() - - content, err := os.ReadFile(path) + workflow := string(readContractFile(t, filepath.Join("..", "deployment", "infrastructure", "sfl-pr-review-auto.yml"))) + startMarker := "// BEGIN TESTABLE CODEX OBSERVER" + endMarker := "// END TESTABLE CODEX OBSERVER" + start := strings.Index(workflow, startMarker) + end := strings.Index(workflow, endMarker) + if start < 0 || end <= start { + t.Fatal("could not locate testable Codex observer block") + } + source := workflow[start+len(startMarker) : end] + source += "\nconst fixture = JSON.parse(process.argv[2]); console.log(JSON.stringify(isActiveInvalidationRun(fixture.run, fixture.pullNumber, \"HemSoft\")));\n" + + temp := filepath.Join(t.TempDir(), "active-invalidation.js") + if err := os.WriteFile(temp, []byte(source), 0o600); err != nil { + t.Fatalf("write active invalidation fixture: %v", err) + } + payload, err := json.Marshal(map[string]any{"run": run, "pullNumber": pullNumber}) if err != nil { - t.Fatalf("read %s: %v", path, err) + t.Fatalf("marshal active invalidation fixture: %v", err) } - return content -} - -func normalizeLineEndings(content []byte) string { - return strings.ReplaceAll(string(content), "\r\n", "\n") + output, err := exec.Command("node", temp, string(payload)).CombinedOutput() + if err != nil { + t.Fatalf("run active invalidation fixture: %v\n%s", err, output) + } + var result bool + if err := json.Unmarshal(output, &result); err != nil { + t.Fatalf("parse active invalidation result %q: %v", output, err) + } + return result } diff --git a/gh-sfl/reviewer_gate_fixture_test.go b/gh-sfl/reviewer_gate_fixture_test.go deleted file mode 100644 index b24b8c75..00000000 --- a/gh-sfl/reviewer_gate_fixture_test.go +++ /dev/null @@ -1,127 +0,0 @@ -package main - -import ( - "os" - "os/exec" - "path/filepath" - "strings" - "testing" -) - -func reviewerFixtureBash(t *testing.T) string { - t.Helper() - bash := "" - if programFiles := os.Getenv("ProgramFiles"); programFiles != "" { - gitBash := filepath.Join(programFiles, "Git", "bin", "bash.exe") - if _, err := os.Stat(gitBash); err == nil { - bash = gitBash - } - } - if bash == "" { - var err error - bash, err = exec.LookPath("bash") - if err != nil { - t.Fatal("bash is required to execute reviewer fixtures") - } - } - return bash -} - -func TestAutoGateRunValidationFixtures(t *testing.T) { - bash := reviewerFixtureBash(t) - - source, err := os.ReadFile( - filepath.Join("..", "deployment", "infrastructure", "sfl-pr-review-auto.yml"), - ) - if err != nil { - t.Fatalf("read automatic review workflow: %v", err) - } - text := strings.ReplaceAll(string(source), "\r\n", "\n") - const begin = "# BEGIN TESTABLE AUTO GATE RUN VALIDATION" - const end = "# END TESTABLE AUTO GATE RUN VALIDATION" - start := strings.Index(text, begin) - finish := strings.Index(text, end) - if start < 0 || finish <= start { - t.Fatal("automatic gate run validation helper block is missing") - } - helper := text[start+len(begin) : finish] - - script := `#!/usr/bin/env bash -set -u -` + helper + ` -if printf '%s' "$1" | validate_review_run "$2" "$3" "$4"; then - echo valid -else - echo invalid -fi -` - scriptPath := filepath.Join(t.TempDir(), "auto-gate-run.sh") - if err := os.WriteFile(scriptPath, []byte(script), 0o700); err != nil { - t.Fatalf("write automatic gate run fixture: %v", err) - } - scriptArgument := filepath.ToSlash(scriptPath) - if volume := filepath.VolumeName(scriptPath); volume != "" { - scriptArgument = "/" + - strings.ToLower(strings.TrimSuffix(volume, ":")) + - strings.TrimPrefix(scriptArgument, filepath.ToSlash(volume)) - } - - const validRun = `{ - "path":".github/workflows/sfl-pr-review.lock.yml", - "event":"workflow_dispatch", - "head_repository":{"full_name":"acme/repo"}, - "head_branch":"main", - "display_title":"SFL PR Review #69 base:head retry=0", - "status":"completed", - "conclusion":"success" -}` - for _, fixture := range []struct { - name string - input string - output string - }{ - { - name: "accepts exact trusted reviewer run", - input: validRun, - output: "valid", - }, - { - name: "rejects another workflow", - input: strings.Replace(validRun, "sfl-pr-review.lock.yml", "forged.yml", 1), - output: "invalid", - }, - { - name: "rejects another repository", - input: strings.Replace(validRun, "acme/repo", "acme/other", 1), - output: "invalid", - }, - { - name: "rejects an untrusted branch", - input: strings.Replace(validRun, `"head_branch":"main"`, `"head_branch":"feature"`, 1), - output: "invalid", - }, - { - name: "rejects another pull request state", - input: strings.Replace(validRun, "#69 base:head", "#70 other:head", 1), - output: "invalid", - }, - } { - t.Run(fixture.name, func(t *testing.T) { - cmd := exec.Command( - bash, - scriptArgument, - fixture.input, - "acme/repo", - "main", - "SFL PR Review #69 base:head retry=", - ) - output, err := cmd.CombinedOutput() - if err != nil { - t.Fatalf("execute automatic gate run fixture: %v\n%s", err, output) - } - if got := strings.TrimSpace(string(output)); got != fixture.output { - t.Fatalf("run validation=%q, want %q", got, fixture.output) - } - }) - } -} diff --git a/gh-sfl/reviewer_preflight.go b/gh-sfl/reviewer_preflight.go index 690d4fce..fd05857a 100644 --- a/gh-sfl/reviewer_preflight.go +++ b/gh-sfl/reviewer_preflight.go @@ -7,8 +7,8 @@ import ( "strings" ) -var reviewerRequiredVariables = []string{"SFL_APP_CLIENT_ID"} -var reviewerRequiredSecrets = []string{"SFL_APP_PRIVATE_KEY", "OPENROUTER_API_KEY"} +var reviewerRequiredVariables = []string{} +var reviewerRequiredSecrets = []string{} type reviewerRolloutHealth struct { DefaultBranch string @@ -72,23 +72,12 @@ func inspectReviewerRolloutWithClient( return reviewerRolloutHealth{}, err } - variables, err := repositoryActionNames(client, owner, repo, "variables") - if err != nil { - return reviewerRolloutHealth{}, fmt.Errorf("listing Actions variables: %w", err) - } - secrets, err := repositoryActionNames(client, owner, repo, "secrets") - if err != nil { - return reviewerRolloutHealth{}, fmt.Errorf("listing Actions secrets: %w", err) - } - return reviewerRolloutHealth{ DefaultBranch: repository.DefaultBranch, ActionsEnabled: actionsPermissions.Enabled, ActionsIssues: actionsIssues, - AppNotice: "GitHub CLI OAuth cannot inspect App installation permissions in this path; " + - "verify scope and permissions with the App-authenticated credential bootstrap", - MissingVariables: missingNames(reviewerRequiredVariables, variables), - MissingSecrets: missingNames(reviewerRequiredSecrets, secrets), + AppNotice: "The Codex GitHub App installation is verified by an authenticated review smoke test; " + + "GitHub CLI cannot inspect the owner's ChatGPT subscription connection", }, nil } @@ -100,7 +89,7 @@ func reviewerActionsPolicyIssues( case "all": return nil, nil case "local_only": - return []string{"Actions policy allows only local actions; reviewer requires pinned GitHub-owned actions"}, nil + return []string{"Actions policy allows only local actions; reviewer requires the pinned GitHub-owned actions/github-script action"}, nil case "selected": var selected struct { GitHubOwnedAllowed bool `json:"github_owned_allowed"` @@ -114,9 +103,7 @@ func reviewerActionsPolicyIssues( if selected.GitHubOwnedAllowed { return nil, nil } - return []string{ - "selected Actions policy must allow GitHub-owned actions required by the reviewer; exact action patterns are not a supported rollout policy", - }, nil + return []string{"selected Actions policy must allow GitHub-owned actions for the reviewer"}, nil case "": return []string{"Actions policy did not report allowed_actions"}, nil default: @@ -189,8 +176,8 @@ func assertReviewerRolloutReady(owner, repo string, stdout io.Writer) error { if len(issues) > 0 { return fmt.Errorf("reviewer rollout preflight failed for %s/%s:\n - %s", owner, repo, strings.Join(issues, "\n - ")) } - fmt.Fprintf(stdout, " Reviewer rollout preflight: default branch %s, Actions policy, variable, and secret metadata ready ✓\n", health.DefaultBranch) - fmt.Fprintln(stdout, " Reviewer App installation: rely on the App-authenticated credential bootstrap check") + fmt.Fprintf(stdout, " Reviewer rollout preflight: default branch %s and Actions policy ready ✓\n", health.DefaultBranch) + fmt.Fprintln(stdout, " Codex App connection: verify with a current-head review after deployment") fmt.Fprintln(stdout) return nil } diff --git a/gh-sfl/status.go b/gh-sfl/status.go index dc40dd74..401b2ae2 100644 --- a/gh-sfl/status.go +++ b/gh-sfl/status.go @@ -48,11 +48,18 @@ type repositoryRuleset struct { } type workflowRunSummary struct { + ID int64 `json:"id"` Status string `json:"status"` Conclusion string `json:"conclusion"` URL string `json:"html_url"` } +type workflowJobSummary struct { + Name string `json:"name"` + Status string `json:"status"` + Conclusion string `json:"conclusion"` +} + // SFL state labels in priority order var sflStateLabels = []string{ "sfl-processing", @@ -262,6 +269,7 @@ func printReviewerHealth(stdout io.Writer, styler tableStyler, owner, repo strin fmt.Fprintf(stdout, "\n Reviewer package:\n") missing := 0 drifted := 0 + defaultBranch, defaultBranchErr := getDefaultBranch(owner, repo) if manifest.SourceSHA == "" { drifted++ fmt.Fprintf(stdout, " %s Manifest is missing immutable sourceSha\n", @@ -282,8 +290,15 @@ func printReviewerHealth(stdout io.Writer, styler tableStyler, owner, repo strin sourceWorkflowPath(workflow), manifest.SourceSHA, ) - expected, renderErr := renderHemSoftWorkflow(workflow, source, manifest.Version) - if manifest.SourceSHA == "" || sourceErr != nil || renderErr != nil || + preparedSource, prepareErr := prepareWorkflowSource( + workflow, + source, + manifest.SourceSHA, + owner+"/"+repo, + defaultBranch, + ) + expected, renderErr := renderHemSoftWorkflow(workflow, preparedSource, manifest.Version) + if manifest.SourceSHA == "" || defaultBranchErr != nil || sourceErr != nil || prepareErr != nil || renderErr != nil || content != expected { drifted++ fmt.Fprintf(stdout, " %s %s differs from pinned source %s\n", @@ -358,7 +373,7 @@ func printReviewerPrerequisites( health reviewerRolloutHealth, healthErr error, ) { - fmt.Fprintf(stdout, "\n Reviewer App:\n") + fmt.Fprintf(stdout, "\n Codex App:\n") if healthErr != nil { fmt.Fprintf(stdout, " %s Could not inspect reviewer rollout prerequisites: %v\n", styler.colored("!", termenv.ANSIYellow).styled, healthErr) @@ -367,7 +382,7 @@ func printReviewerPrerequisites( styler.colored("!", termenv.ANSIYellow).styled, health.AppNotice) } - fmt.Fprintf(stdout, "\n Reviewer credentials and Actions:\n") + fmt.Fprintf(stdout, "\n Reviewer prerequisites:\n") if healthErr != nil { fmt.Fprintf(stdout, " %s Could not inspect credential metadata or Actions state\n", styler.colored("!", termenv.ANSIYellow).styled) @@ -622,21 +637,51 @@ func latestReviewerRun(owner, repo string) (workflowRunSummary, error) { if err != nil { return workflowRunSummary{}, err } - var response struct { - Runs []workflowRunSummary `json:"workflow_runs"` - } - path := fmt.Sprintf( - "repos/%s/%s/actions/workflows/sfl-pr-review.lock.yml/runs?per_page=1", - owner, - repo, - ) - if err := client.Get(path, &response); err != nil { - return workflowRunSummary{}, err - } - if len(response.Runs) == 0 { - return workflowRunSummary{}, nil + return latestReviewerRunWithClient(client, owner, repo) +} + +func latestReviewerRunWithClient(client restAPI, owner, repo string) (workflowRunSummary, error) { + return latestReviewerRunWithPageSize(client, owner, repo, 100) +} + +func latestReviewerRunWithPageSize(client restAPI, owner, repo string, pageSize int) (workflowRunSummary, error) { + for page := 1; ; page++ { + var response struct { + Runs []workflowRunSummary `json:"workflow_runs"` + } + path := fmt.Sprintf( + "repos/%s/%s/actions/workflows/sfl-pr-review-auto.yml/runs?actor=chatgpt-codex-connector%%5Bbot%%5D&per_page=%d&page=%d", + owner, + repo, + pageSize, + page, + ) + if err := client.Get(path, &response); err != nil { + return workflowRunSummary{}, err + } + for _, run := range response.Runs { + var jobs struct { + Jobs []workflowJobSummary `json:"jobs"` + } + jobsPath := fmt.Sprintf( + "repos/%s/%s/actions/runs/%d/jobs?filter=latest&per_page=100", + owner, + repo, + run.ID, + ) + if err := client.Get(jobsPath, &jobs); err != nil { + return workflowRunSummary{}, err + } + for _, job := range jobs.Jobs { + if job.Name == "Observe authenticated Codex review" && job.Conclusion != "skipped" { + return run, nil + } + } + } + if len(response.Runs) < pageSize { + return workflowRunSummary{}, nil + } } - return response.Runs[0], nil } func parseStatusOptions(args []string, stderr io.Writer) (statusOptions, error) { diff --git a/gh-sfl/status_reviewer_run_test.go b/gh-sfl/status_reviewer_run_test.go new file mode 100644 index 00000000..535f5a0f --- /dev/null +++ b/gh-sfl/status_reviewer_run_test.go @@ -0,0 +1,120 @@ +package main + +import ( + "fmt" + "io" + "strings" + "testing" +) + +type reviewerRunREST struct { + gets []string +} + +func (f *reviewerRunREST) Get(path string, response interface{}) error { + f.gets = append(f.gets, path) + switch { + case strings.Contains(path, "/workflows/sfl-pr-review-auto.yml/runs?"): + return decodeTestResponse(response, map[string]any{"workflow_runs": []map[string]any{ + {"id": 2, "status": "in_progress", "conclusion": "", "html_url": "https://github.test/runs/2"}, + {"id": 1, "status": "completed", "conclusion": "failure", "html_url": "https://github.test/runs/1"}, + }}) + case strings.Contains(path, "/actions/runs/2/jobs?"): + return decodeTestResponse(response, map[string]any{"jobs": []map[string]any{ + {"name": "Observe authenticated Codex review", "status": "completed", "conclusion": "skipped"}, + }}) + case strings.Contains(path, "/actions/runs/1/jobs?"): + return decodeTestResponse(response, map[string]any{"jobs": []map[string]any{ + {"name": "Observe authenticated Codex review", "status": "completed", "conclusion": "failure"}, + }}) + default: + return fmt.Errorf("unexpected GET %s", path) + } +} + +func (f *reviewerRunREST) GetWithETag(string, interface{}) (string, error) { + return "", fmt.Errorf("unexpected GetWithETag") +} + +func (f *reviewerRunREST) Post(string, io.Reader, interface{}) error { + return fmt.Errorf("unexpected POST") +} + +func (f *reviewerRunREST) Put(string, io.Reader, interface{}) error { + return fmt.Errorf("unexpected PUT") +} + +func (f *reviewerRunREST) Patch(string, io.Reader, interface{}) error { + return fmt.Errorf("unexpected PATCH") +} + +func (f *reviewerRunREST) Delete(string, interface{}) error { + return fmt.Errorf("unexpected DELETE") +} + +func TestLatestReviewerRunSkipsRunsWithoutExecutedObserver(t *testing.T) { + rest := &reviewerRunREST{} + run, err := latestReviewerRunWithClient(rest, "HemSoft", "consumer") + if err != nil { + t.Fatalf("latestReviewerRunWithClient() unexpected error: %v", err) + } + if run.ID != 1 || run.Conclusion != "failure" { + t.Fatalf("latest actual observer run = %+v, want run 1", run) + } + if len(rest.gets) != 3 || !strings.Contains(rest.gets[0], "actor=chatgpt-codex-connector%5Bbot%5D") || + !strings.Contains(rest.gets[0], "page=1") { + t.Fatalf("reviewer run queries = %v", rest.gets) + } +} + +type paginatedReviewerRunREST struct{} + +func (f *paginatedReviewerRunREST) Get(path string, response interface{}) error { + switch { + case strings.Contains(path, "/workflows/sfl-pr-review-auto.yml/runs?") && strings.Contains(path, "page=1"): + return decodeTestResponse(response, map[string]any{"workflow_runs": []map[string]any{ + {"id": 3, "status": "completed", "conclusion": "skipped", "html_url": "https://github.test/runs/3"}, + {"id": 2, "status": "completed", "conclusion": "skipped", "html_url": "https://github.test/runs/2"}, + }}) + case strings.Contains(path, "/workflows/sfl-pr-review-auto.yml/runs?") && strings.Contains(path, "page=2"): + return decodeTestResponse(response, map[string]any{"workflow_runs": []map[string]any{ + {"id": 1, "status": "completed", "conclusion": "success", "html_url": "https://github.test/runs/1"}, + }}) + case strings.Contains(path, "/actions/runs/3/jobs?"), strings.Contains(path, "/actions/runs/2/jobs?"): + return decodeTestResponse(response, map[string]any{"jobs": []map[string]any{ + {"name": "Observe authenticated Codex review", "status": "completed", "conclusion": "skipped"}, + }}) + case strings.Contains(path, "/actions/runs/1/jobs?"): + return decodeTestResponse(response, map[string]any{"jobs": []map[string]any{ + {"name": "Observe authenticated Codex review", "status": "completed", "conclusion": "success"}, + }}) + default: + return fmt.Errorf("unexpected GET %s", path) + } +} + +func (f *paginatedReviewerRunREST) GetWithETag(string, interface{}) (string, error) { + return "", fmt.Errorf("unexpected GetWithETag") +} +func (f *paginatedReviewerRunREST) Post(string, io.Reader, interface{}) error { + return fmt.Errorf("unexpected POST") +} +func (f *paginatedReviewerRunREST) Put(string, io.Reader, interface{}) error { + return fmt.Errorf("unexpected PUT") +} +func (f *paginatedReviewerRunREST) Patch(string, io.Reader, interface{}) error { + return fmt.Errorf("unexpected PATCH") +} +func (f *paginatedReviewerRunREST) Delete(string, interface{}) error { + return fmt.Errorf("unexpected DELETE") +} + +func TestLatestReviewerRunPaginatesPastSkippedObserverRuns(t *testing.T) { + run, err := latestReviewerRunWithPageSize(&paginatedReviewerRunREST{}, "HemSoft", "consumer", 2) + if err != nil { + t.Fatalf("latestReviewerRunWithPageSize() unexpected error: %v", err) + } + if run.ID != 1 || run.Conclusion != "success" { + t.Fatalf("latest paginated observer run = %+v, want run 1", run) + } +} diff --git a/gh-sfl/sync.go b/gh-sfl/sync.go index 6808342f..cfb0c0e4 100644 --- a/gh-sfl/sync.go +++ b/gh-sfl/sync.go @@ -6,6 +6,7 @@ import ( "flag" "fmt" "io" + "slices" "strings" "time" ) @@ -56,6 +57,8 @@ func runSync(args []string, stdout io.Writer, stderr io.Writer) error { shortLatestSHA = shortLatestSHA[:12] } + installedTier := canonicalDeploymentTier(manifest.Tier) + normalizeManifestForSync(manifest, installedTier) workflows, err := workflowsForInstalledManifest(manifest) if err != nil { return fmt.Errorf("resolving installed workflows: %w", err) @@ -65,7 +68,6 @@ func runSync(args []string, stdout io.Writer, stderr io.Writer) error { return err } } - installedTier := canonicalDeploymentTier(manifest.Tier) if manifest.SourceSHA == latestSHA { if opts.dryRun { fmt.Fprintf(stdout, " ✓ Source revision is current (version %s, SHA %s)\n", latestVersion, shortLatestSHA) @@ -107,6 +109,10 @@ func runSync(args []string, stdout io.Writer, stderr io.Writer) error { if fetchErr != nil { return fmt.Errorf("fetching %s: %w", srcPath, fetchErr) } + content, prepareErr := prepareWorkflowSource(wf, content, sourceRef, owner+"/"+repo, defaultBranch) + if prepareErr != nil { + return prepareErr + } rendered, renderErr := renderHemSoftWorkflow(wf, content, latestVersion) if renderErr != nil { return fmt.Errorf("applying HemSoft engine policy to %s: %w", wf, renderErr) @@ -120,6 +126,10 @@ func runSync(args []string, stdout io.Writer, stderr io.Writer) error { if fetchErr != nil { return fmt.Errorf("fetching add-on %s: %w", srcPath, fetchErr) } + content, prepareErr := prepareWorkflowSource(wf, content, sourceRef, owner+"/"+repo, defaultBranch) + if prepareErr != nil { + return prepareErr + } rendered, renderErr := renderHemSoftWorkflow(wf, content, latestVersion) if renderErr != nil { return fmt.Errorf("applying HemSoft engine policy to add-on %s: %w", wf, renderErr) @@ -154,10 +164,6 @@ func runSync(args []string, stdout io.Writer, stderr io.Writer) error { // Update manifest manifest.Version = release.Version manifest.SourceSHA = latestSHA - if manifest.Tier == "review" { - manifest.Tier = "reviewer" - manifest.Components = tierComponents["reviewer"] - } manifest.DeployedAt = deployedAt manifest.DeployedBy = deployedBy manifest.EnginePolicy = hemSoftEnginePolicyManifestForFileMap(fileMap) @@ -221,6 +227,28 @@ func runSync(args []string, stdout io.Writer, stderr io.Writer) error { return nil } +func normalizeManifestForSync(manifest *sflManifest, installedTier string) { + hadLegacyReviewer := slices.Contains(manifest.Components, "sfl-pr-review") + components := manifest.Components[:0] + for _, component := range manifest.Components { + if component != "sfl-pr-review" && component != "sfl-pr-review-recovery" { + components = append(components, component) + } + } + manifest.Components = components + + if (installedTier == "full" || (installedTier == "custom" && hadLegacyReviewer)) && + !slices.Contains(manifest.Components, "sfl-pr-review-auto") { + manifest.Components = append(manifest.Components, "sfl-pr-review-auto") + } + if installedTier != "reviewer" { + return + } + + manifest.Tier = "reviewer" + manifest.Components = append([]string(nil), tierComponents["reviewer"]...) +} + func shouldPreflightReviewerSync(dryRun bool, workflowSets ...[]string) bool { return !dryRun && workflowsIncludeReviewer(workflowSets...) } diff --git a/gh-sfl/toggle.go b/gh-sfl/toggle.go index 8e2c05e7..3a4d0b6c 100644 --- a/gh-sfl/toggle.go +++ b/gh-sfl/toggle.go @@ -67,10 +67,10 @@ func parseToggleOptions(cmd string, args []string, errw io.Writer) (toggleOption fs.Usage = func() { action := "Stop" - desc := "Pauses automatic SFL dispatch and recovery while the required reviewer gate fails closed." + desc := "Pauses Codex result observation while the required reviewer gate fails closed." if cmd == "start" { action = "Start" - desc = "Re-enables automatic SFL dispatch and recovery by setting SFL_ENABLED=true." + desc = "Re-enables Codex result observation by setting SFL_ENABLED=true." } fmt.Fprintf(errw, "%s SFL in a repository.\n%s\n\nUsage:\n gh sfl %s [--repo OWNER/REPO]\n\nFlags:\n", action, desc, cmd) fs.PrintDefaults() diff --git a/kimi-review-smoke.txt b/kimi-review-smoke.txt deleted file mode 100644 index b38260c1..00000000 --- a/kimi-review-smoke.txt +++ /dev/null @@ -1 +0,0 @@ -Temporary pull request used to verify the OpenRouter Kimi K3 SFL reviewer. diff --git a/sfl.json b/sfl.json index 78ec7639..cac847f4 100644 --- a/sfl.json +++ b/sfl.json @@ -13,9 +13,7 @@ "repo-audit", "issue-processor", "simplisticate", - "sfl-pr-review", "sfl-pr-review-auto", - "sfl-pr-review-recovery", "pr-analyzer-general", "pr-analyzer-quality", "pr-analyzer-security", @@ -74,23 +72,6 @@ "OPENAI_API_KEY" ] }, - { - "name": "sfl-pr-review", - "profile": "openrouter-kimi-k3-high", - "provider": "copilot", - "model": "moonshotai/kimi-k3", - "renderedModel": "moonshotai/kimi-k3", - "requiredSecretsAnyOf": [ - "OPENROUTER_API_KEY" - ], - "environment": { - "COPILOT_PROVIDER_BASE_URL": "https://openrouter.ai/api/v1", - "COPILOT_PROVIDER_API_KEY": "${{ secrets.OPENROUTER_API_KEY }}", - "COPILOT_PROVIDER_TYPE": "openai", - "COPILOT_PROVIDER_WIRE_API": "responses", - "COPILOT_MODEL": "moonshotai/kimi-k3" - } - }, { "name": "pr-analyzer-general", "profile": "codex-gpt-55-high",