From 86b2c006bbb70c3b2060c36cac5b1fd09961e886 Mon Sep 17 00:00:00 2001 From: AppSolves Date: Sat, 14 Nov 2026 08:28:02 +0100 Subject: [PATCH 1/2] fix: recover isolated reviews and track verified PR progress --- .github/workflows/cache-benchmark.yml | 72 ++++++++++ .github/workflows/ci.yml | 6 + .github/workflows/review.yml | 77 ++++++++++- CHANGELOG.md | 5 + README.md | 8 +- docs/architecture.md | 2 + docs/authentication.md | 2 + docs/configuration.md | 2 +- docs/deployment.md | 2 + docs/development.md | 2 + docs/installation.md | 7 + docs/operations.md | 4 +- docs/review-rollout.md | 52 +++++++ docs/security.md | 2 + docs/validation.md | 12 +- examples/codexbot.yml | 2 +- migrations/0003_review_progress.sql | 17 +++ scripts/operations.mjs | 9 +- src/core/diagnostics.ts | 46 +++++++ src/runner/broker.ts | 31 ++++- src/runner/main.ts | 58 +++++--- src/setup.ts | 9 +- src/worker/index.ts | 86 ++++++++++-- src/worker/oidc.ts | 20 ++- src/worker/progress.ts | 121 +++++++++++++++++ src/worker/recovery.ts | 5 +- src/worker/store.ts | 72 +++++++++- src/worker/webhook.ts | 6 +- tests/broker.test.ts | 48 +++++++ tests/core.test.ts | 5 + tests/github-fixture.ts | 18 ++- tests/oidc.test.ts | 4 +- tests/progress.test.ts | 187 ++++++++++++++++++++++++++ tests/setup.test.ts | 5 +- tests/worker.test.ts | 41 +++++- tests/workers-runtime.mjs | 10 +- tests/workflows.test.ts | 37 ++++- wrangler.jsonc | 3 +- 38 files changed, 1026 insertions(+), 69 deletions(-) create mode 100644 .github/workflows/cache-benchmark.yml create mode 100644 docs/review-rollout.md create mode 100644 migrations/0003_review_progress.sql create mode 100644 src/core/diagnostics.ts create mode 100644 src/worker/progress.ts create mode 100644 tests/broker.test.ts create mode 100644 tests/progress.test.ts diff --git a/.github/workflows/cache-benchmark.yml b/.github/workflows/cache-benchmark.yml new file mode 100644 index 0000000..6aa7853 --- /dev/null +++ b/.github/workflows/cache-benchmark.yml @@ -0,0 +1,72 @@ +name: Trusted image cache benchmark +on: + workflow_dispatch: + workflow_call: +permissions: + contents: read +jobs: + benchmark: + runs-on: ubuntu-24.04 + timeout-minutes: 10 + env: + DOCKER_BUILD_RECORD_UPLOAD: 'false' + DOCKER_BUILD_SUMMARY: 'false' + steps: + - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 + with: { persist-credentials: false } + - uses: actions/setup-node@949feb2413d6458794dcd2491c4babbbce0c15c1 # v7.1.0 + with: { node-version: '24.22.3', package-manager-cache: false } + - run: npm ci --ignore-scripts + - run: npm run build + - name: Measure ordinary build + shell: bash + run: | + cache_start=$(date +%s) + docker build --tag codexbot-benchmark:baseline . + echo "$(( $(date +%s) - cache_start ))" > "$RUNNER_TEMP/baseline-seconds" + docker image rm codexbot-benchmark:baseline + date +%s > "$RUNNER_TEMP/cold-start" + - uses: docker/setup-buildx-action@f87e5991a6d7451dcb8d9637bfbc97413f497069 # v4.4.1 + with: + name: codexbot-cache-cold + cleanup: false + version: v0.39.0 + driver-opts: image=moby/buildkit@sha256:c5f8862f52499773af85bc07695b2379ef08e8002352b0df2c4864c91d78dabe + - uses: docker/build-push-action@c3c9e263c25d99ce0380d002d59b67737d91b0dc # v7.4.0 + with: + context: . + load: true + push: false + tags: codexbot-benchmark:cold + cache-to: type=gha,version=2,scope=codexbot-benchmark-${{ github.run_id }}-${{ github.run_attempt }},mode=min,timeout=90s + - name: Discard builder and loaded image before measuring a cache restore + shell: bash + run: | + echo "$(( $(date +%s) - $(cat "$RUNNER_TEMP/cold-start") ))" > "$RUNNER_TEMP/cold-seconds" + docker buildx rm codexbot-cache-cold + docker image rm codexbot-benchmark:cold + date +%s > "$RUNNER_TEMP/warm-start" + - uses: docker/setup-buildx-action@f87e5991a6d7451dcb8d9637bfbc97413f497069 # v4.4.1 + with: + name: codexbot-cache-warm + version: v0.39.0 + driver-opts: image=moby/buildkit@sha256:c5f8862f52499773af85bc07695b2379ef08e8002352b0df2c4864c91d78dabe + - uses: docker/build-push-action@c3c9e263c25d99ce0380d002d59b67737d91b0dc # v7.4.0 + with: + context: . + load: true + push: false + tags: codexbot-benchmark:warm + cache-from: type=gha,version=2,scope=codexbot-benchmark-${{ github.run_id }}-${{ github.run_attempt }},timeout=90s + cache-to: type=gha,version=2,scope=codexbot-benchmark-${{ github.run_id }}-${{ github.run_attempt }},mode=min,timeout=90s + - name: Report measured timings + shell: bash + run: | + echo "$(( $(date +%s) - $(cat "$RUNNER_TEMP/warm-start") ))" > "$RUNNER_TEMP/warm-seconds" + node --input-type=module <<'JS' + import {readFileSync, appendFileSync} from 'node:fs'; + const read = name => Number(readFileSync(`${process.env.RUNNER_TEMP}/${name}-seconds`, 'utf8')); + const baseline=read('baseline'), cold=read('cold'), warm=read('warm'); + console.log(JSON.stringify({event:'cache_benchmark',baseline_seconds:baseline,cold_seconds:cold,warm_seconds:warm})); + appendFileSync(process.env.GITHUB_STEP_SUMMARY, `Baseline: ${baseline}s; cold cache: ${cold}s; restored cache: ${warm}s. Includes builder setup and load/export. Same-run builder-image reuse is optimistic; repeat on independent runners before enabling cache. No provider or credential access.\n`); + JS diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 67e4591..5db583a 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -2,9 +2,15 @@ name: CI on: push: pull_request: + workflow_dispatch: + inputs: + cache_benchmark: { type: boolean, default: false } permissions: contents: read jobs: + cache-benchmark: + if: ${{ github.event_name == 'workflow_dispatch' && inputs.cache_benchmark }} + uses: ./.github/workflows/cache-benchmark.yml check: runs-on: ubuntu-24.04 timeout-minutes: 10 diff --git a/.github/workflows/review.yml b/.github/workflows/review.yml index 6d76c39..16fe330 100644 --- a/.github/workflows/review.yml +++ b/.github/workflows/review.yml @@ -6,6 +6,11 @@ on: credential_owner: { required: true, type: string } installation_id: { required: true, type: string } backend: { required: true, type: string } + credential_check_only: { required: false, type: boolean, default: false } + docker_cache: { required: false, type: boolean, default: false } + secrets: + CODEXBOT_VAULT_KEY: { required: false } + CODEXBOT_API_KEY: { required: false } permissions: {} jobs: source: @@ -43,6 +48,22 @@ jobs: contents: read id-token: write steps: + - name: Verify Environment credential availability + shell: bash + run: | + node --input-type=module <<'JS' + const vault = Boolean(process.env.CODEXBOT_VAULT_KEY); + const api = Boolean(process.env.CODEXBOT_API_KEY); + if (vault === api) { + console.error(JSON.stringify({event: 'review_failed', stage: 'environment', code: vault ? 'ambiguous_environment_credentials' : 'missing_environment_credentials'})); + process.exitCode = 1; + } else { + console.log(JSON.stringify({event: 'credential_availability_verified', mode: vault ? 'chatgpt' : 'api-key'})); + } + JS + env: + CODEXBOT_VAULT_KEY: ${{ secrets.CODEXBOT_VAULT_KEY }} + CODEXBOT_API_KEY: ${{ secrets.CODEXBOT_API_KEY }} - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 with: repository: ${{ needs.source.outputs.repository }} @@ -51,23 +72,70 @@ jobs: - uses: actions/setup-node@949feb2413d6458794dcd2491c4babbbce0c15c1 # v7.1.0 with: node-version: '24.22.3' + package-manager-cache: false - run: npm ci --ignore-scripts - run: npm run build - - name: Build trusted review image - run: docker build --tag codexbot-review:local . + - name: Verify read-only OIDC and Environment diagnostic + if: ${{ inputs.credential_check_only }} + run: node dist/src/runner/main.js check + env: + CODEXBOT_BACKEND: ${{ inputs.backend }} + CODEXBOT_VAULT_KEY: ${{ secrets.CODEXBOT_VAULT_KEY }} + CODEXBOT_API_KEY: ${{ secrets.CODEXBOT_API_KEY }} + - name: Build trusted review image without cache overhead + if: ${{ !inputs.credential_check_only && !inputs.docker_cache }} + run: docker build --tag "codexbot-review:$TRUSTED_REV" . + env: + TRUSTED_REV: ${{ needs.source.outputs.ref }} + - uses: docker/setup-buildx-action@f87e5991a6d7451dcb8d9637bfbc97413f497069 # v4.4.1 + if: ${{ !inputs.credential_check_only && inputs.docker_cache }} + with: + version: v0.39.0 + driver-opts: image=moby/buildkit@sha256:c5f8862f52499773af85bc07695b2379ef08e8002352b0df2c4864c91d78dabe + - name: Build trusted review image with scoped cache + if: ${{ !inputs.credential_check_only && inputs.docker_cache }} + uses: docker/build-push-action@c3c9e263c25d99ce0380d002d59b67737d91b0dc # v7.4.0 + with: + context: . + file: Dockerfile + load: true + push: false + tags: codexbot-review:${{ needs.source.outputs.ref }} + cache-from: type=gha,version=2,scope=codexbot-${{ github.repository_id }}-${{ needs.source.outputs.ref }}-${{ hashFiles('Dockerfile', 'package.json', 'package-lock.json', '.dockerignore') }},timeout=10s + cache-to: type=gha,version=2,scope=codexbot-${{ github.repository_id }}-${{ needs.source.outputs.ref }}-${{ hashFiles('Dockerfile', 'package.json', 'package-lock.json', '.dockerignore') }},mode=min,timeout=10s,ignore-error=true + env: + DOCKER_BUILD_RECORD_UPLOAD: 'false' + DOCKER_BUILD_SUMMARY: 'false' + - id: image + name: Bind execution to the built image ID + if: ${{ !inputs.credential_check_only }} + shell: bash + run: | + node --input-type=module <<'JS' + import {execFileSync} from 'node:child_process'; + import {appendFileSync} from 'node:fs'; + const ref = process.env.TRUSTED_REV; + if (!/^[a-f0-9]{40}$/.test(ref)) throw new Error('Invalid trusted source revision'); + const id = execFileSync('docker', ['image', 'inspect', `codexbot-review:${ref}`, '--format', '{{.Id}}'], {encoding:'utf8'}).trim(); + if (!/^sha256:[a-f0-9]{64}$/.test(id)) throw new Error('Invalid built image identity'); + appendFileSync(process.env.GITHUB_OUTPUT, `id=${id}\n`); + JS + env: + TRUSTED_REV: ${{ needs.source.outputs.ref }} - name: Review exact commit in isolated container + if: ${{ !inputs.credential_check_only }} run: node dist/src/runner/main.js review env: CODEXBOT_BACKEND: ${{ inputs.backend }} CODEXBOT_REQUEST_ID: ${{ inputs.request_id }} CODEXBOT_CREDENTIAL_OWNER: ${{ inputs.credential_owner }} CODEXBOT_INSTALLATION_ID: ${{ inputs.installation_id }} - CODEXBOT_IMAGE: codexbot-review:local + CODEXBOT_IMAGE: ${{ steps.image.outputs.id }} CODEXBOT_VAULT_KEY: ${{ secrets.CODEXBOT_VAULT_KEY }} CODEXBOT_API_KEY: ${{ secrets.CODEXBOT_API_KEY }} publish: needs: [source, review] - if: ${{ needs.review.result == 'success' }} + if: ${{ always() && !inputs.credential_check_only && needs.source.result == 'success' && (needs.review.result == 'success' || needs.review.result == 'failure') }} runs-on: ubuntu-24.04 timeout-minutes: 5 environment: codexbot-publish-${{ inputs.installation_id }} @@ -90,3 +158,4 @@ jobs: env: CODEXBOT_BACKEND: ${{ inputs.backend }} CODEXBOT_REQUEST_ID: ${{ inputs.request_id }} + CODEXBOT_REVIEW_JOB_SUCCEEDED: ${{ needs.review.result == 'success' }} diff --git a/CHANGELOG.md b/CHANGELOG.md index 8686a28..2885ca3 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,11 @@ ## Unreleased — security remediation pass +- Explicitly forward only named Environment credential grants; add a credential-only availability diagnostic, safe runner/broker/OIDC failure codes and failed-job publication safeguards. +- Add migration 0003 with recoverable, App-owned progress comments, verified run status, failure metadata and bounded operator reconciliation. Existing sessions remain valid. +- Add opt-in source-scoped Buildx caching and a credential-free cache benchmark; execute review images by content ID. Keep cache off until measurements justify its cost. +- New setup triggers use @CBReview plus repository ID; document independent OpenAI connector settings without changing App identity or removing legacy parsing. + - Generate Cloudflare Env from the pinned Wrangler configuration before every build/typecheck. CI tests an independent source checkout without generated declarations. - Resolve commit objects to tree SHAs; fetch essential changed files/instructions first. Separate complete diff, essential context and bounded supplementary coverage. Cache immutable trees/blobs only inside the authorized collection. - Add migration 0002: durable webhook/outbox receipts, stable dispatch attempts, exact run-name reconciliation, quotas, publication leases/checkpoints and installation-bound sessions. Existing sessions require reconnect; pending legacy requests are invalidated. See docs/operations.md. diff --git a/README.md b/README.md index 7cf35cc..068644f 100644 --- a/README.md +++ b/README.md @@ -3,13 +3,15 @@ An Apache-2.0 GitHub App for Codex pull request reviews on repository-owned GitHub Actions runners. The Cloudflare Worker authenticates requests and publishes native reviews; it never runs Codex. ```text -@codexbot review --effort xhigh --merge +@CBReview review --effort xhigh --merge Review authentication, authorization, migrations and compatibility. ``` Configure a trigger plus up to five aliases matching `@[A-Za-z0-9]{1,39}`. Matching ignores case. These are text commands and can notify unrelated GitHub users with the same name; choose distinctive aliases. Quoted examples and fenced commands do not trigger reviews. Model and reasoning effort are separate; omitted effort preserves Codex's default. +Set `bot.trigger: "@CBReview"` to use the example above. The legacy default `@codexbot` remains supported; new setup chooses `@CBReview`. Use a name without “codex” when another Codex integration is installed. CodexBot cannot control that independent connector; see [trigger isolation and rollout](docs/review-rollout.md). + ## Status **Pre-release implementation; no public hosted App has been deployed by this repository setup.** Unit/integration tests exercise signed webhooks, official OAuth contracts, OIDC, actual SQLite persistence, setup PR creation and GitHub publishing against mocked external APIs. The pinned official Codex binary is tested without credentials. A separate Docker check uses synthetic inference; it does not claim to have performed a real AI review. See [validation and live acceptance](docs/validation.md). @@ -59,7 +61,9 @@ npm run cli -- connect-api-key --repository OWNER/REPO --backend https://YOUR-WO The ChatGPT flow displays **Continue with ChatGPT** and opens an official authorization URL using a loopback callback. Manual `auth.json` export is unnecessary. API-key mode requires `credentials.mode: api-key` in trusted configuration. Do not keep both mode secrets in the Environment. -5. Review and merge the setup PR. Configure whitelist/merge policies and an optional custom handle in `.codexbot.yml`. Protect both the default branch and credential/publishing Environments. Comment using the handle from your setup PR. New setup chooses `@CodexBotReview` to reduce collisions; existing/manual `@codexbot` configurations remain supported. +5. Review and merge the setup PR. Configure whitelist/merge policies and an optional custom handle in `.codexbot.yml`. Protect both the default branch and credential/publishing Environments. Comment using the handle from your setup PR. New setup chooses `@CBReview` to reduce collisions; existing/manual `@codexbot` configurations remain supported. + +Accepted requests receive one editable App-owned progress comment, separate from the native review. It shows verified commit/file counts, a request identifier, confirmed lifecycle/run status and the final native-review link. Temporary comment/API failures are reconciled without borrowing credentials or duplicating ambiguous POSTs. Image caching is opt-in pending measurement; [the rollout guide](docs/review-rollout.md) explains both controls. ### ChatGPT hosting restrictions diff --git a/docs/architecture.md b/docs/architecture.md index 906b97e..1cb6ac1 100644 --- a/docs/architecture.md +++ b/docs/architecture.md @@ -1,5 +1,7 @@ # Architecture +The [review reliability guide](review-rollout.md) documents migration 0003, named Environment grants, sanitized runner diagnostics, durable progress comments and optional Buildx caching. Progress writes are backend-only and validate App/request/PR ownership; the model receives no write capability. A failed upstream job permanently disables approval for its persisted result, including publication recovery. + The Cloudflare Worker ingests signed GitHub webhooks, verifies live authorization, maintains D1 metadata and publishes native reviews. Official Codex runs exclusively in the repository owner's ephemeral GitHub-hosted Linux Actions jobs. Independent installations choose their own App, Worker, D1 database and source release; no vendor backend or shared OpenAI credential exists. ```mermaid diff --git a/docs/authentication.md b/docs/authentication.md index c245be5..42c2703 100644 --- a/docs/authentication.md +++ b/docs/authentication.md @@ -1,5 +1,7 @@ # Authentication and credential ownership +Reusable callers must explicitly grant the two optional secret names with empty values; the called installation/actor Environment supplies the actual credential. No repository-key fallback or blanket inheritance is supported. [Credential diagnostics and rollout](review-rollout.md) include a no-inference availability check. Migration 0003 does not change encryption/session formats or require reconnecting. + ## Official ChatGPT OAuth CodexBot uses the documented OSS public-client flow, not a borrowed Codex client ID or an undocumented ChatGPT endpoint. [Registration/sign-in](https://developers.openai.com/siwc/token-sharing-open-source/sign-in), [sessions](https://developers.openai.com/siwc/token-sharing-open-source/profiles-and-sessions), [app-server](https://developers.openai.com/siwc/token-sharing-open-source/codex-app-server) and [deployment transfer](https://developers.openai.com/siwc/token-sharing-open-source/self-hosted-vms) define the contract. diff --git a/docs/configuration.md b/docs/configuration.md index 737bcf3..9f5dceb 100644 --- a/docs/configuration.md +++ b/docs/configuration.md @@ -47,7 +47,7 @@ Patterns are full-string, case-insensitive RE2. `*` matches a sequence; `?` one The first nonempty top-level line must begin with a configured trigger and `review`. Four-space/tab indents, quotations, inline examples and code fences do not invoke. Flags may occur in any order on that first line: `--merge`, `--effort LEVEL`, `--effort=LEVEL`, `--rerun`, and `--` for literal context. Duplicates, unknown flags and unclosed quotes are errors. Later lines are advisory context, never flags or authorization. -Aliases are textual commands, not guaranteed GitHub App mentions. A custom alias can notify an unrelated GitHub account with the same name. Existing @codexbot installations remain compatible. New automated setup uses @CodexBotReview followed by the numeric repository ID to reduce collisions; it remains editable. Prefer a distinctive repository-specific alias such as @CodexBotReview7319, confirm account collisions yourself, and document it for contributors. Changing a trigger never changes the App's publishing identity. +Aliases are textual commands, not guaranteed GitHub App mentions. A custom alias can notify an unrelated GitHub account with the same name. Existing @codexbot installations remain compatible. New automated setup uses @CBReview followed by the numeric repository ID to reduce collisions; it remains editable. Configure `bot: {trigger: "@CBReview", aliases: []}` for a short explicit alternative. Avoid adding the old name as an alias if the purpose is avoiding an independent connector. Changing a trigger never changes the App's publishing identity. See [trigger isolation](review-rollout.md). Effort syntax recognizes none/minimal/low/medium/high/xhigh/max, but the selected model must actually advertise the requested value. Omission preserves Codex defaults. Catalog discovery does not prove account entitlement; completed inference is required. Never substitute an unsupported value or change billing modes. diff --git a/docs/deployment.md b/docs/deployment.md index 2cfffdd..f8fcee5 100644 --- a/docs/deployment.md +++ b/docs/deployment.md @@ -1,5 +1,7 @@ # Deployment checklist +For existing deployments, follow [the coordinated immutable-pin upgrade](review-rollout.md) before deploying this revision. Apply additive migration 0003, update the caller's named Environment secret grants, and set its workflow pin and Worker TRUSTED_WORKFLOW to the same actual merged commit. Keep merge disabled. Report validation before the production change; a temporary mismatch fails closed. + This document is operator instructions, not evidence of an existing hosted deployment. Do not register, deploy, publish a release or use paid inference without your own authorization. ## GitHub App diff --git a/docs/development.md b/docs/development.md index a2ff169..35fc336 100644 --- a/docs/development.md +++ b/docs/development.md @@ -1,5 +1,7 @@ # Development and local validation +For the current reliability changes, `node --import tsx --test tests/broker.test.ts tests/progress.test.ts tests/worker.test.ts tests/workflows.test.ts` runs the focused contracts. Full check also exercises them. [The rollout guide](review-rollout.md) describes provider-free credential and cache diagnostics; neither is a substitute for real inference acceptance. + Use Node 24. Dependency, Codex, Wrangler, Action and container versions are pinned. npm ci --ignore-scripts installs the packaged official native Codex binary without executing lifecycle scripts. The lockfile records integrity; the minimatch override avoids the audited transitive advisory and is checked by lint/tests. ```sh diff --git a/docs/installation.md b/docs/installation.md index c234795..3a06101 100644 --- a/docs/installation.md +++ b/docs/installation.md @@ -30,6 +30,7 @@ on: credential_owner: { required: true, type: string } installation_id: { required: true, type: string } dispatch_attempt: { required: true, type: string } + credential_check_only: { required: false, type: boolean, default: false } permissions: {} concurrency: group: codexbot-credentials @@ -40,15 +41,21 @@ jobs: contents: read id-token: write uses: YOUR-ORG/codexbot/.github/workflows/review.yml@FULL_COMMIT_SHA + secrets: + CODEXBOT_VAULT_KEY: '' + CODEXBOT_API_KEY: '' with: request_id: ${{ inputs.request_id }} credential_owner: ${{ inputs.credential_owner }} installation_id: ${{ inputs.installation_id }} backend: https://YOUR-WORKER + credential_check_only: ${{ inputs.credential_check_only }} ``` Use the exact filename .github/workflows/codexbot.yml and the backend's trusted immutable source pin. Do not add secrets: inherit, fork-head checkout, repository hooks or PR-controlled commands. The reusable workflow resolves its own source from GitHub's TLS-delivered workload identity; the backend independently verifies the signed claims/run before releasing anything. +The two explicit empty secret grants are required: the called job resolves its own installation/actor Environment. They do not copy a repository/organization secret into the job. Do not replace them with a shared key or blanket inheritance. The reusable workflow declares the two optional secret names; exactly one owning Environment credential must be present. The credential-only diagnostic checks availability and a read-only administrator workload, without building an image, acquiring a session or running Codex. See [coordinated upgrades](review-rollout.md). + ## Credentials Protect the default branch **before** connecting. New codexbot-INSTALLATION_ID-NUMERIC_USER_ID and codexbot-publish-INSTALLATION_ID Environments use a default-branch-only custom deployment policy. The installation namespace isolates both API keys and OAuth vault keys across independent Apps/reinstalls. Existing reviewers/timers are preserved. Unrestricted Environments and custom policies allowing other branches/tags are rejected. Existing protected-branch policies require an actually protected default branch: GitHub otherwise permits every branch when no protected branches exist. Private Free repositories are unsupported. diff --git a/docs/operations.md b/docs/operations.md index 8b04dfc..9ef27ec 100644 --- a/docs/operations.md +++ b/docs/operations.md @@ -1,9 +1,11 @@ # Operations and migration runbook +Migration 0003 adds leased progress-comment recovery and sanitized failure/run metadata without changing existing sessions. See [the review upgrade/recovery guide](review-rollout.md). A separate two-minute trigger guarantees one due progress reconciliation without sharing publication subrequest limits; idle minute recovery can also handle progress. Administrative `--retry-progress` only restarts bounded reconciliation; it never resets an ambiguous creation or recreates a deleted comment. Workflow failures before broker admission are finalized from independently verified App-run metadata. + ## Upgrade from the initial schema/workflow 1. Pause webhook dispatch. Cancel/drain old Actions runs and inspect/cancel any existing App-owned native auto-merge in GitHub. Preserve a private D1 backup; it contains ciphertext and private review/prompt metadata. -2. Apply migrations in filename order with pinned Wrangler: `node node_modules/wrangler/bin/wrangler.js d1 migrations apply codexbot --remote`. D1 applies each migration transactionally and records it once. Never re-execute individual ALTER statements manually. Fresh local/runtime tests apply both production migrations. +2. Apply migrations in filename order with pinned Wrangler: `node node_modules/wrangler/bin/wrangler.js d1 migrations apply codexbot --remote`. D1 applies each migration transactionally and records it once. Never re-execute individual ALTER statements manually. Fresh local/runtime tests apply all production migrations. 3. Migration 0002 adds receipt/outbox and publication state, quotas and a session table keyed by installation/repository/actor. It invalidates legacy in-flight requests. The old encrypted session table is quarantined and purged at the first cleanup. **Every ChatGPT user must reconnect**; the Worker cannot safely re-encrypt old AAD without their key. Reuse the local issued client/host profile, not a shared token. Revoke superseded sessions in ChatGPT settings if needed. Existing API-key secrets remain in their old GitHub Environments but are never reused automatically. All users, including API-key users, must explicitly provision the new installation-scoped Environment; then remove/revoke obsolete secrets. 4. Deploy the reviewed new Worker and select a real immutable source commit. Update each caller workflow through a reviewed PR: add required string inputs `dispatch_attempt` and `installation_id`, pass installation_id to the reusable workflow, and run-name `CodexBot ${{ inputs.request_id }} ${{ inputs.dispatch_attempt }}`. Keep the new reusable workflow pinned and match backend TRUSTED_WORKFLOW. Old dispatch inputs/workflow names intentionally cannot acquire credentials under the new attempt-binding protocol. 5. Keep CHATGPT_DEPLOYMENT_AUTHORIZED and MERGE_DEPLOYMENT_AUTHORIZED false until their separate external gates pass. Reconnect using the CLI's capability/installation preflight. Private Free repositories cannot store credentials through this topology. Protect the default branch first; preserve Environment reviewers and set a default-branch-only custom deployment policy. diff --git a/docs/review-rollout.md b/docs/review-rollout.md new file mode 100644 index 0000000..b9005f4 --- /dev/null +++ b/docs/review-rollout.md @@ -0,0 +1,52 @@ +# Review reliability, credential forwarding and coordinated rollout + +## Credential diagnosis + +An Environment secret existing in GitHub is not proof that a called workflow receives it. [GitHub's reusable-workflow contract](https://docs.github.com/en/actions/how-tos/reuse-automations/reuse-workflows#using-inputs-and-secrets-in-a-reusable-workflow) requires a named grant in the caller as well as the Environment on the called job. Both workflows must be updated together. CodexBot uses two optional named declarations and explicit empty caller grants. The Environment supplies the value; repository/organization keys are not forwarded. Never add `secrets: inherit` or move a user's key into repository secrets. + +The review job checks for exactly one credential before checkout/install/build. A missing or ambiguous credential fails with a fixed JSON diagnostic. With `credential_check_only: true`, it then compiles the trusted client and verifies a read-only OIDC diagnostic; no image build, session lease, credential decryption, inference or publishing is performed. A passing check proves availability and the checked workload claims, not OAuth entitlement or privileged broker admission. Use a new UUID for the diagnostic request/attempt and the actual installation/actor IDs. Its caller must remain on the protected default branch allowed by the Environment. + +The separate `/v1/workload/check` diagnostic requires a signed workload, the same exact caller/reusable/subject/Environment constraints, live repository administrator identity and the actual administrator-authored run. It returns only a fixed verification status. Its installation token is read-only and never returned. It does not read sessions, write D1 or authorize the review broker: manual user-authored runs still cannot prepare, rotate, submit or publish a review. Normal privileged routes continue requiring this App's exact dispatch actor/attempt/request identity. + +For normal reviews the Worker still verifies signature, issuer, audience, subject, repository/owner IDs, caller SHA, reusable SHA, Environment, run and App/dispatch-attempt identity. JWT expiry, invalid tokens and unavailable JWKS now have separate safe codes. Broker errors retain only status and known machine codes from a bounded response. HTML, arbitrary provider messages, request headers, URLs and tokens are withheld. The runner logs fixed stage/code/status records; it does not report a session-release failure when prepare never acquired a session. + +`/failure` and `/stage` require the same verified review workload. Failure reporting cannot alter another admitted run. Session release is conditioned on the actual run-binding transition. A failed review job cannot turn its stored output into approval during publisher retry: its context is durably non-approving. Review and publishing remain separate jobs; neither gives the model a write token. + +## Progress and recovery + +Migration 0003 adds comment identity, leased update state, verified workflow observations and sanitized failure metadata to requests. It does not change encrypted session AAD or require reconnecting existing credentials. New requests snapshot commit/file counts from GitHub's verified PR response; legacy requests explicitly report when counts were not recorded. + +One top-level progress comment is created per accepted request. Repeated deliveries and reruns of recovery edit that same App-owned comment. Every update verifies App ID/login/type, exact request marker, repository and PR. Public text contains only fixed vocabulary and validated identifiers/counts/constructed GitHub links, never prompts, model output, source or internal error codes. The native inline review remains separate. + +Updates are rate limited to fifteen seconds and each scheduled invocation handles one due operation. A separate two-minute progress trigger prevents dispatch/publication traffic from starving terminal comments; idle minute recovery can also reconcile progress. Run links/status come from exact App/attempt/SHA-bound GitHub run metadata. A published request continues observing the workflow until its actual completion is confirmed. Completed/cancelled/failed workflows without publication are finalized, and expired requests release leases during the same cleanup transaction. No invented percentage or fake analysis stage is shown. + +Creation is intentionally at-most-one automatic POST after an ambiguous response. Recovery lists comments for the exact App marker instead of blindly resending. A crash before the POST can therefore leave an unresolved creation requiring operator attention. Known rejection responses may retry; deleted comments stay deleted. Five failed reconciliations exhaust automatic retries. GitHub permission revocation/removal can prevent remote finalization; restore access or handle the comment manually. + +The repository-admin operations endpoint lists only status/IDs and sanitized failure metadata. To restart exhausted comment reconciliation: + +```sh +npm run ops -- --repository OWNER/REPO --backend https://YOUR-WORKER --retry-progress REQUEST_ID +``` + +This shares the existing bounded operator retry budget, cannot reset an ambiguous creation to a fresh POST and cannot recreate deleted comments. A failed job that never entered the protected publishing stage cannot be operator-retried into publication; fix prerequisites and request a new authorized review instead. + +## Cache cost and trust + +`docker_cache` defaults false. A roughly 20-second cold build and the locally observed roughly 867 MB uncompressed image do not justify assuming that network cache import/export is faster. The opt-in path uses commit-pinned Actions, an exact Buildx version and a digest-pinned BuildKit image. Its GHA v2 scope includes repository ID, immutable trusted reusable revision and Dockerfile/lockfile/input hashes. The build context is the checked-out trusted source; no PR source, review output, credential, Docker build secret or registry push is used. Runtime execution uses the built image's content ID instead of a mutable tag. Cache misses still build; optional cache-export failures cannot conceal build failures. Build records are not uploaded. + +Run the manually dispatched `Trusted image cache benchmark` workflow, or CI's `cache_benchmark: true` option. It compares ordinary, cold-cached and restored builds using a fresh second builder, including setup, load and export. Its scope is separate from production caches, and it uses no credential Environment or provider. Same-host reuse of the builder image is optimistic: repeat on independent runners and include network/storage/initial cache cost. Enable `with: {docker_cache: true}` in a repository caller only when repeated measurements show a meaningful net saving; otherwise retain the ordinary build. No release-image registry is introduced. + +## Trigger isolation + +Set `.codexbot.yml` to `bot: {trigger: "@CBReview", aliases: []}` or use the generated `@CBReview`. Matching is exact and case-insensitive; extra suffixes, quoted examples, inline code and fences do not invoke CodexBot. Legacy triggers remain supported when explicitly configured or inherited from the legacy default. They do not rename the GitHub App. + +OpenAI's connector is independent. Its [documented manual command](https://learn.chatgpt.com/docs/third-party/github) is `@codex review`; CodexBot cannot guarantee how that service matches other text. To disable its automatic reviews, open [Codex review settings](https://chatgpt.com/codex/settings/code-review), select the repository and disable Automatic review, and disable the personal automatic-review preference when applicable. This does not necessarily disable manual mentions. For complete isolation, disconnect that repository from the native integration or remove/suspend its separate GitHub App installation under GitHub Settings → Applications → Installed GitHub Apps. Do not uninstall CodexBot by mistake. No settings of the independent connector are modified by this project. + +## Coordinated immutable-pin upgrade + +1. Validate and merge the feature branch. Obtain the **actual merged commit SHA** from GitHub and verify that it contains the reviewed reusable workflow. Never substitute an example, branch name, guessed SHA or an unrelated last commit. +2. Drain active old reviews. Apply migration 0003 using pinned Wrangler's remote migrations command. It is additive; do not reapply ALTER statements individually. Keep `MERGE_DEPLOYMENT_AUTHORIZED=false` and repository merge disabled. +3. Prepare a caller PR using that exact SHA plus the two empty named secret grants and optional credential-only input. Preserve its run-name, permissions, concurrency and default-branch protection. Update the textual trigger separately if desired; do not change authorization or merge policy. +4. Report local/CI validation before deployment. Deploy the reviewed Worker with `TRUSTED_WORKFLOW=OWNER/codexbot/.github/workflows/review.yml@THAT_SAME_SHA`, preserving existing bindings, OAuth gate, secrets and domain settings. Merge the caller PR using normal protection checks. There is no atomic transaction across Cloudflare and GitHub: during the short mismatch window requests fail closed. Do not relax either SHA check to hide it. +5. Read back both actual pins and compare them byte-for-byte. Run the credential-only workflow check under the owning actor/installation Environment. Then request a new advisory/no-merge review for end-to-end broker/provider acceptance. Old Actions review attempts are not rerun to borrow a new credential/pin; use a new authorized comment. +6. Inspect the same progress comment, workflow link, sanitized failures and native review. Provider inference remains a distinct live test with the owner's authorization/allowance, never an API-key fallback. Roll back both pins together on failure; keep additive DB columns. A backend/caller protected-branch or deployment-permission block is a release blocker, not permission to bypass safeguards. diff --git a/docs/security.md b/docs/security.md index 9a8e74b..81dbf2a 100644 --- a/docs/security.md +++ b/docs/security.md @@ -1,5 +1,7 @@ # Security and trust boundaries +Progress comments and image caching have separate bounded trust surfaces; see [the review rollout guide](review-rollout.md). Public progress contains no prompts/model text/internal error details. Comment ownership and run identity are independently verified. Ambiguous POSTs are reconciled instead of repeated, and deleted comments are not recreated. Optional caches contain only the trusted image build inputs, are scoped to source/content/repository, and never contain PR context, user credentials or model output. + Untrusted data includes PR/fork code, comments, prompts, model output and API metadata until validated. Trusted components are protected default-branch/release code, repository administrators, GitHub/Actions/OIDC, the Worker/D1 operator, pinned dependencies, Docker/kernel isolation and OpenAI's official endpoints. | Boundary | Control | Residual limit | diff --git a/docs/validation.md b/docs/validation.md index 29f4b10..56a3e56 100644 --- a/docs/validation.md +++ b/docs/validation.md @@ -1,14 +1,20 @@ # Validation record and release gates +The latest [review reliability rollout](review-rollout.md) adds focused tests for credential forwarding/availability, broker error redaction, no-release-before-admission, workload-bound stage/failure updates, permanent non-approval after failed jobs, comment ownership/leases/idempotency/ambiguous responses and bounded recovery. The credential-only workflow checks availability and read-only OIDC identity only. A separate manually dispatched cache benchmark measures real GHA import/export; cache remains opt-in until that evidence justifies it. These checks do not assert live provider inference. + Run the exact commands in [development](development.md). Local checks cover HMAC, policy/parser/aliases, safe regex, quotas, immutable commit/tree/blob context, output schema/DLP/limits/confidence, OAuth/OIDC contracts, native review and guarded-merge state machines, installation-scoped session leases/CAS, atomic setup, and D1/HTTP fault transitions. `test:clean` validates an independent source checkout without generated Env. `test:workers` executes the actual Worker under workerd with real D1. `test:sandbox` uses the real pinned Codex app-server and isolated Docker wiring with synthetic Responses SSE. The native protocol test initializes the actual platform binary with an empty private home. None of these invokes paid inference or proves model quality, entitlement or GitHub protection behavior. The per-finding implementation/evidence matrix is maintained in [remediation](remediation.md). Deployment/permission/session migrations are in [operations](operations.md); repeatable real-provider and disposable-GitHub acceptance steps are in [acceptance](acceptance.md). -## Local verification — 2026-11-14 +## Review reliability validation — 2026-11-14 + +`npm run check` passed strict typecheck, lint, **123 tests** and build. `npm run test:clean`, `npm run test:workers`, actionlint, `docker build --tag codexbot-review:local .`, `npm run test:sandbox`, `npm audit --audit-level=moderate`, Wrangler deploy dry-run and `git diff --check` passed. Actual workerd/D1 coverage includes migration 0003, expired-request cleanup and installation-isolated session release. The strict image scan below reported zero HIGH/CRITICAL findings; a source-only secret scan reported no findings. No real model inference was used. Remote acceptance and cache timings must be recorded separately after execution; local green checks do not establish them. + +## Initial source-only audit snapshot — 2026-11-14 -Verified on Windows with Node 24.22.1/npm 11.6.4, Docker Desktop Linux containers, Node 24.22.3 in the final image, pinned Wrangler 4.162.0, actionlint 1.7.12 and Trivy 0.76.2. This source tree has no initial Git commit or configured origin; test:clean creates an independent source-only directory, not a remote Git clone. No remote migration, deployment, GitHub mutation or real provider inference was performed. +Verified on Windows with Node 24.22.1/npm 11.6.4, Docker Desktop Linux containers, Node 24.22.3 in the final image, pinned Wrangler 4.162.0, actionlint 1.7.12 and Trivy 0.76.2. At this initial snapshot the source tree had no initial Git commit or configured origin; test:clean created an independent source-only directory, not a remote Git clone. No remote migration, deployment, GitHub mutation or real provider inference had been performed at that snapshot. | Executed command | Observed result | |---|---| @@ -32,7 +38,7 @@ The final local image scan used the following exact options (on Windows set DOCK Result: exit 0, zero reported High/Critical vulnerabilities using the database updated 2026-11-14 01:05 UTC, Alpine 3.24.4 and the final image built from the source above. Earlier Debian images failed this same strict gate; they were replaced, not exempted. No CVE ignore list or ignore-unfixed switch was used. This is a database/package scan, not proof that every dependency embedded in native binaries is known or safe. Repeat on the release image with a fresh database. -## Gates which remain external +## External acceptance gates (initial snapshot; verify per deployment) - Public App registration, source publication, actual installation and OIDC workload verification against GitHub. - OpenAI authorization for the exact hosted/ephemeral Actions ChatGPT topology. Defaults keep CHATGPT_DEPLOYMENT_AUTHORIZED false; no workaround exists. diff --git a/examples/codexbot.yml b/examples/codexbot.yml index 2c3b997..93236a0 100644 --- a/examples/codexbot.yml +++ b/examples/codexbot.yml @@ -1,6 +1,6 @@ version: 1 bot: - trigger: "@codexbot" + trigger: "@CBReview" aliases: [] # Text commands; an alias can notify an unrelated GitHub account. authorization: whitelist: "" # Add authorized GitHub logins. Each uses their own credential. diff --git a/migrations/0003_review_progress.sql b/migrations/0003_review_progress.sql new file mode 100644 index 0000000..182c740 --- /dev/null +++ b/migrations/0003_review_progress.sql @@ -0,0 +1,17 @@ +-- Additive: no credential/session format changes or invalidation of existing requests. +ALTER TABLE requests ADD COLUMN progress_comment_id INTEGER; +ALTER TABLE requests ADD COLUMN progress_phase TEXT NOT NULL DEFAULT 'pending'; +ALTER TABLE requests ADD COLUMN progress_lease TEXT; +ALTER TABLE requests ADD COLUMN progress_until INTEGER NOT NULL DEFAULT 0; +ALTER TABLE requests ADD COLUMN progress_attempts INTEGER NOT NULL DEFAULT 0; +ALTER TABLE requests ADD COLUMN progress_next_at INTEGER NOT NULL DEFAULT 0; +ALTER TABLE requests ADD COLUMN progress_hash TEXT; +ALTER TABLE requests ADD COLUMN progress_reported_stage TEXT; +ALTER TABLE requests ADD COLUMN progress_stage TEXT NOT NULL DEFAULT 'queued'; +ALTER TABLE requests ADD COLUMN failure_code TEXT; +ALTER TABLE requests ADD COLUMN failure_stage TEXT; +ALTER TABLE requests ADD COLUMN failure_status INTEGER; +ALTER TABLE requests ADD COLUMN workflow_status TEXT; +ALTER TABLE requests ADD COLUMN workflow_conclusion TEXT; +ALTER TABLE requests ADD COLUMN workflow_checked_at INTEGER NOT NULL DEFAULT 0; +CREATE INDEX requests_progress_recovery ON requests(progress_next_at,progress_until,created_at); diff --git a/scripts/operations.mjs b/scripts/operations.mjs index c538771..448c783 100644 --- a/scripts/operations.mjs +++ b/scripts/operations.mjs @@ -3,14 +3,15 @@ import { parseArgs } from 'node:util'; import { execFileSync } from 'node:child_process'; import { backendUrl } from '../dist/src/runner/broker.js'; import { splitRepo } from '../dist/src/worker/github.js'; -const { values } = parseArgs({ options: { backend: { type: 'string' }, repository: { type: 'string' }, retry: { type: 'string' }, cancel: { type: 'string' } } }); +const { values } = parseArgs({ options: { backend: { type: 'string' }, repository: { type: 'string' }, retry: { type: 'string' }, cancel: { type: 'string' }, 'retry-progress': { type: 'string' } } }); try { const backend = backendUrl(values.backend ?? ''); splitRepo(values.repository ?? ''); - if (values.retry && values.cancel) throw new Error('Choose retry or cancel.'); + if ([values.retry, values.cancel, values['retry-progress']].filter(Boolean).length > 1) throw new Error('Choose one recovery operation.'); + const id = values.retry ?? values.cancel ?? values['retry-progress']; const token = execFileSync('gh', ['auth', 'token'], { encoding: 'utf8', stdio: ['ignore', 'pipe', 'ignore'] }).trim(); const response = await globalThis.fetch(`${backend}/v1/operations?repository=${encodeURIComponent(values.repository)}`, { - method: values.retry || values.cancel ? 'POST' : 'GET', headers: { Authorization: `Bearer ${token}`, 'Content-Type': 'application/json' }, - ...(values.retry || values.cancel ? { body: JSON.stringify({ id: values.retry ?? values.cancel, action: values.cancel ? 'cancel' : 'retry' }) } : {}), redirect: 'error', signal: globalThis.AbortSignal.timeout(30_000) + method: id ? 'POST' : 'GET', headers: { Authorization: `Bearer ${token}`, 'Content-Type': 'application/json' }, + ...(id ? { body: JSON.stringify({ id, action: values.cancel ? 'cancel' : values['retry-progress'] ? 'retry-progress' : 'retry' }) } : {}), redirect: 'error', signal: globalThis.AbortSignal.timeout(30_000) }); if (!response.ok) throw new Error('Recovery denied; check live repository-admin access, request age and retry limits.'); process.stdout.write(JSON.stringify(await response.json(), null, 2) + '\n'); diff --git a/src/core/diagnostics.ts b/src/core/diagnostics.ts new file mode 100644 index 0000000..5e07ce8 --- /dev/null +++ b/src/core/diagnostics.ts @@ -0,0 +1,46 @@ +import { AppError } from './errors.js'; + +export const REVIEW_STAGES = ['environment','prepare','context','credentials','analyzing','result','publish','release'] as const; +export const FAILURE_CODES = new Set(['missing_environment_credentials','ambiguous_environment_credentials','credential_owner_mismatch','missing_read_token', + 'api_key_missing_or_ambiguous_credentials','vault_key_missing_or_ambiguous_credentials','invalid_envelope','invalid_vault_key','invalid_saved_session', + 'session_too_short_reconnect_required','review_image_missing','isolated_review_failed','proxy_start_failed','docker_unavailable','docker_cleanup_failed_check_runner', + 'oauth_grant_invalid_reconnect_required','chatgpt_plan_usage_permission_missing','oauth_token_endpoint_failed','oauth_token_exchange_uncertain_reconnect_required', + 'invalid_saved_registration','invalid_saved_expiry','oauth_invalid_credential','oauth_invalid_expiry','oauth_invalid_refresh_time', + 'oauth_refresh_account_mismatch','oauth_refresh_authorized_party_mismatch','codex_turn_failed','codex_rpc_rejected','codex_output_not_json', + 'effort_not_supported_by_selected_model','configured_model_not_in_codex_catalog','diff_truncated','pr_changed_during_context_collection', + 'broker_prepare_failed','broker_result_failed','broker_rotate_failed','broker_publish_failed','broker_stage_failed','broker_release_failed', + 'ENOTFOUND','ECONNREFUSED','ECONNRESET','ETIMEDOUT','ENOENT','EACCES','EPERM','ENOSPC','EPIPE', + 'session_rotation_conflict_reconnect_required','review_preflight_failed','external_or_execution_failure']); +const OPERATIONS = new Set(['prepare','result','rotate','publish','release','failure','stage','check']); +export const BROKER_CODES = new Set(` +bearer_required request_not_found request_expired invalid_webhook_signature installation_no_longer_valid request_bound_to_another_run repository_admin_required +oidc_repository_mismatch oidc_event_mismatch oidc_caller_workflow_mismatch oidc_reusable_workflow_mismatch oidc_environment_mismatch oidc_subject_mismatch +oidc_run_missing oidc_invalid_run_identity oidc_token_expired oidc_token_invalid oidc_jwks_unavailable +workflow_run_mismatch workflow_not_dispatched_by_this_app workflow_dispatch_attempt_mismatch +chatgpt_deployment_authorization_revoked use_authorized_rerun_command_instead_of_workflow_rerun request_already_started stale_pull_request +trusted_default_branch_changed_or_unprotected review_authorization_revoked session_missing_or_in_use chatgpt_session_not_connected +request_not_running review_result_already_recorded request_not_ready_for_publication publication_busy_or_terminal internal_error +github_actions_oidc_unavailable invalid_oidc_endpoint github_oidc_request_failed github_oidc_missing_token invalid_request_id invalid_broker_operation +`.trim().split(/\s+/)); +export class BrokerError extends AppError { + constructor(public readonly operation: string, status: number, public readonly remoteCode: string | null) { super(`broker_${operation}_failed`, status); } +} +export function diagnostic(error: unknown, stage: string): { event: string; stage: string; code: string; status?: number; broker_code?: string } { + const result: { event: string; stage: string; code: string; status?: number; broker_code?: string } = { + event: 'review_failed', stage: REVIEW_STAGES.includes(stage as typeof REVIEW_STAGES[number]) ? stage : 'environment', code: 'external_or_execution_failure' + }; + if (error instanceof BrokerError && OPERATIONS.has(error.operation)) { + result.code = error.code; + if (error.remoteCode && BROKER_CODES.has(error.remoteCode)) result.broker_code = error.remoteCode; + } else if (error instanceof AppError && (FAILURE_CODES.has(error.code) || BROKER_CODES.has(error.code))) result.code = error.code; + else { + const codes = new Set(['ENOTFOUND','ECONNREFUSED','ECONNRESET','ETIMEDOUT','ENOENT','EACCES','EPERM','ENOSPC','EPIPE']); + let cause: unknown = error; + for (let depth = 0; depth < 4 && cause && typeof cause === 'object'; depth++) { + if ('code' in cause && typeof cause.code === 'string' && codes.has(cause.code)) { result.code = cause.code; break; } + cause = 'cause' in cause ? cause.cause : null; + } + } + if (error instanceof AppError && Number.isInteger(error.status) && error.status >= 400 && error.status <= 599) result.status = error.status; + return result; +} diff --git a/src/runner/broker.ts b/src/runner/broker.ts index d001455..4d1d68d 100644 --- a/src/runner/broker.ts +++ b/src/runner/broker.ts @@ -1,4 +1,23 @@ import { record, requireCondition, AppError } from '../core/errors.js'; +import { BrokerError, BROKER_CODES } from '../core/diagnostics.js'; + +async function result(response: Response, operation: string): Promise { + if (response.ok) return response.json() as Promise; + let code: string | null = null; + const reader = response.body?.getReader(); + if (reader) { + const chunks: Uint8Array[] = []; let size = 0; + try { + for (;;) { const part = await reader.read(); if (part.done) break; size += part.value.length; if (size > 4096) { await reader.cancel(); break; } chunks.push(part.value); } + if (size <= 4096) { + const body: unknown = JSON.parse(Buffer.concat(chunks).toString('utf8')); + if (body && typeof body === 'object' && 'error' in body && typeof body.error === 'string' && BROKER_CODES.has(body.error)) code = body.error; + } + } catch { /* Raw provider/proxy diagnostics may echo credentials. */ } + finally { reader.releaseLock(); } + } + throw new BrokerError(operation, response.status, code); +} export function backendUrl(value: string): string { const url = new URL(value); @@ -11,20 +30,19 @@ export async function oidcToken(audience: string): Promise { const url = new URL(endpoint); requireCondition(url.protocol === 'https:', 'invalid_oidc_endpoint'); url.searchParams.set('audience', audience); const response = await fetch(url, { headers: { Authorization: `Bearer ${token}` }, redirect: 'error', signal: AbortSignal.timeout(15_000) }); - requireCondition(response.ok, 'github_oidc_request_failed'); + requireCondition(response.ok, 'github_oidc_request_failed', response.status); const result = record(await response.json()); requireCondition(typeof result.value === 'string', 'github_oidc_missing_token'); return result.value; } export async function broker(backend: string, request: string, operation: string, body: unknown = {}): Promise { requireCondition(/^[a-f0-9-]{36}$/.test(request), 'invalid_request_id'); - requireCondition(['prepare','result','rotate','publish','release'].includes(operation), 'invalid_broker_operation'); + requireCondition(['prepare','result','rotate','publish','release','failure','stage'].includes(operation), 'invalid_broker_operation'); const origin = backendUrl(backend); const send = async () => { const response = await fetch(`${origin}/v1/requests/${request}/${operation}`, { method: 'POST', headers: { Authorization: `Bearer ${await oidcToken(origin)}`, 'Content-Type': 'application/json' }, body: JSON.stringify(body), redirect: 'error', signal: AbortSignal.timeout(90_000) }); - requireCondition(response.ok, `broker_${operation}_failed`, response.status); - return response.json() as Promise; + return result(response, operation); }; try { return await send(); } catch (error) { @@ -34,3 +52,8 @@ export async function broker(backend: string, request: string, operation: string throw error; } } +export async function checkWorkload(backend: string): Promise { + const origin = backendUrl(backend); + return result(await fetch(`${origin}/v1/workload/check`, { method: 'POST', headers: { Authorization: `Bearer ${await oidcToken(origin)}` }, + redirect: 'error', signal: AbortSignal.timeout(90_000) }), 'check'); +} diff --git a/src/runner/main.ts b/src/runner/main.ts index d103a1e..93fd995 100644 --- a/src/runner/main.ts +++ b/src/runner/main.ts @@ -6,28 +6,37 @@ import type { RequestSnapshot } from '../worker/store.js'; import type { Envelope } from '../core/crypto.js'; import { unseal, seal, sessionBinding } from '../core/crypto.js'; import { record, requireCondition } from '../core/errors.js'; +import { diagnostic, FAILURE_CODES, BROKER_CODES } from '../core/diagnostics.js'; import { OAuth, validateSession } from '../auth/oauth.js'; -import { broker, backendUrl } from './broker.js'; +import { broker, backendUrl, checkWorkload } from './broker.js'; import { collectContext } from './context.js'; import { isolatedReview } from './docker.js'; import { failedReview } from '../core/review.js'; export async function reviewJob(): Promise { - const backend = backendUrl(process.env.CODEXBOT_BACKEND ?? ''), id = process.env.CODEXBOT_REQUEST_ID ?? ''; - const temporary = await mkdtemp(join(tmpdir(), 'codexbot-')); + const id = process.env.CODEXBOT_REQUEST_ID ?? ''; + let backend = '', temporary: string | undefined; let snapshot: RequestSnapshot | null = null; - let submission: { output: unknown; context_complete: boolean } | null = null; + let submission: { output: unknown; context_complete: boolean; failure?: ReturnType } | null = null; + let stage = 'environment'; try { + backend = backendUrl(process.env.CODEXBOT_BACKEND ?? ''); + temporary = await mkdtemp(join(tmpdir(), 'codexbot-')); + requireCondition(process.env.CODEXBOT_VAULT_KEY || process.env.CODEXBOT_API_KEY, 'missing_environment_credentials'); + requireCondition(!(process.env.CODEXBOT_VAULT_KEY && process.env.CODEXBOT_API_KEY), 'ambiguous_environment_credentials'); + stage = 'prepare'; const prepared = record(await broker(backend, id, 'prepare')); snapshot = prepared.snapshot as RequestSnapshot; requireCondition(snapshot.id === id && String(snapshot.actorId) === process.env.CODEXBOT_CREDENTIAL_OWNER && String(snapshot.installationId) === process.env.CODEXBOT_INSTALLATION_ID, 'credential_owner_mismatch'); requireCondition(typeof prepared.read_token === 'string', 'missing_read_token'); const github = client(prepared.read_token); const directory = join(temporary, 'context'); + stage = 'context'; let manifest; try { manifest = await collectContext(github, snapshot, directory); } finally { await github.rest.apps.revokeInstallationAccessToken(); } let credential: string; + stage = 'credentials'; if (snapshot.config.credentials.mode === 'api-key') { requireCondition(process.env.CODEXBOT_API_KEY && !process.env.CODEXBOT_VAULT_KEY, 'api_key_missing_or_ambiguous_credentials'); credential = process.env.CODEXBOT_API_KEY; @@ -51,27 +60,46 @@ export async function reviewJob(): Promise { // Context is private source, readable only by the dedicated container uid via a read-only bind. const image = process.env.CODEXBOT_IMAGE; requireCondition(image, 'review_image_missing'); const sockets = join(temporary, 'sockets'); await mkdir(sockets, { mode: 0o700 }); + stage = 'analyzing'; + await broker(backend, id, 'stage', { stage: 'analyzing' }); const output = await isolatedReview(image, directory, credentialPath, sockets); submission = { output, context_complete: manifest.complete }; + stage = 'result'; await broker(backend, id, 'result', submission); - } catch { + } catch (error) { + const report = diagnostic(error, stage); console.error(JSON.stringify(report)); // Always leave a deterministic non-approving result when preflight succeeded. - if (snapshot) await broker(backend, id, 'result', submission ?? { context_complete: false, output: failedReview(snapshot.head) }); - else throw new Error('review_preflight_failed'); + const failure = { ...report, code: report.broker_code ?? (FAILURE_CODES.has(report.code) || BROKER_CODES.has(report.code) ? report.code : 'external_or_execution_failure') }; + if (snapshot) await broker(backend, id, 'result', submission ?? { context_complete: false, output: failedReview(snapshot.head), failure }); + else { + try { await broker(backend, id, 'failure', failure); } + catch (secondary) { console.error(JSON.stringify({ ...diagnostic(secondary, stage), event: 'failure_reporting_deferred' })); } + throw error; + } } finally { try { - try { await broker(backend, id, 'release'); } - catch { + try { if (snapshot) await broker(backend, id, 'release'); } + catch (error) { // A durable result must still reach the separately protected publisher. // Backend cleanup releases terminal/expired leases; surface the cleanup failure explicitly. - process.stderr.write('CodexBot session release failed; backend lease cleanup is required.\n'); + console.error(JSON.stringify({ ...diagnostic(error, 'release'), event: 'session_release_deferred' })); } - } finally { await rm(temporary, { recursive: true, force: true }); } + } finally { if (temporary) await rm(temporary, { recursive: true, force: true }); } } } -if (process.argv[2] === 'review') { - try { await reviewJob(); } catch { process.stderr.write('CodexBot review failed; no approval or merge authorized.\n'); process.exitCode = 1; } +if (process.argv[2] === 'check') { + try { + requireCondition(Boolean(process.env.CODEXBOT_VAULT_KEY) !== Boolean(process.env.CODEXBOT_API_KEY), 'missing_environment_credentials'); + const checked = record(await checkWorkload(process.env.CODEXBOT_BACKEND ?? '')); + requireCondition(checked.status === 'verified' && checked.scope === 'diagnostic_only', 'review_preflight_failed'); + console.info(JSON.stringify({ event: 'workload_diagnostic_verified', scope: 'no_session_no_inference' })); + } catch (error) { console.error(JSON.stringify(diagnostic(error, 'prepare'))); process.exitCode = 1; } +} else if (process.argv[2] === 'review') { + try { await reviewJob(); } catch { process.exitCode = 1; } } else if (process.argv[2] === 'publish') { - try { const result = record(await broker(process.env.CODEXBOT_BACKEND ?? '', process.env.CODEXBOT_REQUEST_ID ?? '', 'publish')); process.stdout.write(`CodexBot publication: ${String(result.event ?? result.status)}; merge: ${String(result.merge ?? 'unchanged')}\n`); } - catch { process.stderr.write('CodexBot publication failed; inspect backend diagnostic codes.\n'); process.exitCode = 1; } + try { + const result = record(await broker(process.env.CODEXBOT_BACKEND ?? '', process.env.CODEXBOT_REQUEST_ID ?? '', 'publish', { review_job_failed: process.env.CODEXBOT_REVIEW_JOB_SUCCEEDED !== 'true' })); + requireCondition(result.status !== 'review_failed', 'review_preflight_failed'); + process.stdout.write('CodexBot publication request completed.\n'); + } catch (error) { console.error(JSON.stringify(diagnostic(error, 'publish'))); process.exitCode = 1; } } diff --git a/src/setup.ts b/src/setup.ts index 21e14c8..b9b9cd3 100644 --- a/src/setup.ts +++ b/src/setup.ts @@ -28,10 +28,13 @@ export function callerWorkflow(source: string, ref: string, backend: string): st request_id: { description: 'Backend-bound request id', required: true, type: 'string' }, credential_owner: { description: 'Authenticated requester numeric GitHub id', required: true, type: 'string' }, installation_id: { description: 'Verified installation credential namespace', required: true, type: 'string' }, - dispatch_attempt: { description: 'Durable backend dispatch identity', required: true, type: 'string' } + dispatch_attempt: { description: 'Durable backend dispatch identity', required: true, type: 'string' }, + credential_check_only: { description: 'Check Environment availability without broker admission or inference', required: false, type: 'boolean', default: false } } } }, permissions: {}, concurrency: { group: 'codexbot-credentials', 'cancel-in-progress': false }, jobs: { review: { permissions: { contents: 'read', 'id-token': 'write' }, uses: `${source}/.github/workflows/review.yml@${ref}`, - with: { request_id: '${{ inputs.request_id }}', credential_owner: '${{ inputs.credential_owner }}', installation_id: '${{ inputs.installation_id }}', backend } } + // Named empty grants enable Environment resolution without repository-secret fallback. + secrets: { CODEXBOT_VAULT_KEY: '', CODEXBOT_API_KEY: '' }, + with: { request_id: '${{ inputs.request_id }}', credential_owner: '${{ inputs.credential_owner }}', installation_id: '${{ inputs.installation_id }}', credential_check_only: '${{ inputs.credential_check_only }}', backend } } } }); } export async function setupPullRequest(github: Octokit, repository: string, source: string, ref: string, backend: string, login: string, principal: 'user' | 'installation' = 'user'): Promise { @@ -40,7 +43,7 @@ export async function setupPullRequest(github: Octokit, repository: string, sour requireCondition(Number.isSafeInteger(metadata.id) && metadata.id > 0, 'repository_identity_unavailable'); const config = structuredClone(DEFAULT_CONFIG); config.authorization.whitelist = login; // New installations get a distinctive textual alias; existing configs retain their trigger. - config.bot.trigger = `@CodexBotReview${metadata.id}`; + config.bot.trigger = `@CBReview${metadata.id}`; const desired = new Map([['.github/workflows/codexbot.yml', callerWorkflow(source, ref, backend)], ['.codexbot.yml', stringify(config)]]); await github.rest.repos.getContent({ ...splitRepo(source), path: '.github/workflows/review.yml', ref }); for (const path of desired.keys()) { diff --git a/src/worker/index.ts b/src/worker/index.ts index cd9aea4..3159ddc 100644 --- a/src/worker/index.ts +++ b/src/worker/index.ts @@ -3,8 +3,9 @@ import type { Envelope } from '../core/crypto.js'; import { AppError, requireCondition, record } from '../core/errors.js'; import { authorize } from '../core/policy.js'; import { appClient, client, installationClient, permission, splitRepo, trustedConfig, verifyActor } from './github.js'; -import { verifyWorkload } from './oidc.js'; +import { verifyWorkload, verifySignedClaims, checkClaims } from './oidc.js'; import { Store } from './store.js'; +import type { RequestSnapshot } from './store.js'; import { publish, discardPendingReview } from './publish.js'; import { invalidateMerge } from './merge.js'; import { onboarding } from './onboarding.js'; @@ -14,6 +15,8 @@ import { MAX_RESULT_BODY_BYTES } from '../core/limits.js'; import { repositoryCapabilities } from '../capabilities.js'; import { compactWebhook } from './webhook.js'; import { credentialEnvironment, publishingEnvironment } from '../core/identity.js'; +import { updateProgress, recoverProgress } from './progress.js'; +import { FAILURE_CODES, REVIEW_STAGES, BROKER_CODES } from '../core/diagnostics.js'; export async function boundedBody(request: Request, limit: number): Promise { requireCondition(Number(request.headers.get('content-length') ?? 0) <= limit, 'body_too_large', 413); @@ -39,6 +42,17 @@ function envelope(value: unknown): Envelope { requireCondition(input.version === 1 && typeof input.iv === 'string' && /^[A-Za-z0-9_-]{16}$/.test(input.iv) && typeof input.ciphertext === 'string' && /^[A-Za-z0-9_-]{20,64000}$/.test(input.ciphertext), 'invalid_envelope'); return input as unknown as Envelope; } +function failure(value: unknown): { code: string; stage: string; status: number | null } { + const body = record(value); + requireCondition(typeof body.code === 'string' && (FAILURE_CODES.has(body.code) || BROKER_CODES.has(body.code)) && typeof body.stage === 'string' && REVIEW_STAGES.includes(body.stage as typeof REVIEW_STAGES[number]), 'invalid_failure_diagnostic'); + return { code: body.code, stage: body.stage, status: typeof body.status === 'number' && Number.isInteger(body.status) && body.status >= 400 && body.status <= 599 ? body.status : null }; +} +async function notify(env: Env, store: Store, snapshot: RequestSnapshot): Promise { + try { + const { github } = await installationClient(env, snapshot.installationId, snapshot.repositoryId, { contents: 'read', pull_requests: 'write', actions: 'read' }); + await updateProgress(github, store, snapshot, env.GITHUB_APP_SLUG, Number(env.GITHUB_APP_ID)); + } catch { console.error(JSON.stringify({ event: 'progress_update_deferred', request_id: snapshot.id })); } +} async function route(request: Request, env: Env): Promise { const url = new URL(request.url), store = new Store(env.DB); @@ -46,6 +60,28 @@ async function route(request: Request, env: Env): Promise { requireCondition(!origin || origin === new URL(env.PUBLIC_URL).origin, 'cross_origin_request_denied', 403); if (url.pathname === '/health' && request.method === 'GET') return Response.json({ status: 'ok', version: '0.1.0' }); if (url.pathname === '/' && request.method === 'GET') return onboarding(env); + if (url.pathname === '/v1/workload/check' && request.method === 'POST') { + // A read-only administrator diagnostic, never a credential/token broker grant. + // Privileged review routes below still require this App's dispatch identity. + const claims = await verifySignedClaims(bearer(request), env.PUBLIC_URL); + requireCondition(typeof claims.repository === 'string' && typeof claims.actor === 'string' && typeof claims.actor_id === 'string' && /^\d+$/.test(claims.actor_id) && + typeof claims.repository_id === 'string' && /^\d+$/.test(claims.repository_id), 'oidc_invalid_run_identity', 403); + const repo = splitRepo(claims.repository), actorId = Number(claims.actor_id), repositoryId = Number(claims.repository_id); + requireCondition(Number.isSafeInteger(actorId) && actorId > 0 && Number.isSafeInteger(repositoryId) && repositoryId > 0, 'oidc_repository_mismatch', 403); + const { data: installation } = await (await appClient(env)).rest.apps.getRepoInstallation(repo); + requireCondition(!installation.suspended_at, 'installation_no_longer_valid', 403); + const { github } = await installationClient(env, installation.id, repositoryId, { contents: 'read', pull_requests: 'read', actions: 'read' }); + const { data: metadata } = await github.rest.repos.get(repo); + const { data: branch } = await github.rest.repos.getBranch({ ...repo, branch: metadata.default_branch }); + requireCondition(branch.protected, 'trusted_default_branch_changed_or_unprotected', 403); + const identity = checkClaims(claims, { repository: metadata.full_name, repositoryId: metadata.id, ownerId: metadata.owner.id, defaultBranch: metadata.default_branch, + workflowSha: branch.commit.sha, trustedWorkflow: env.TRUSTED_WORKFLOW, environment: credentialEnvironment(installation.id, actorId) }); + requireCondition(await permission(github, metadata.full_name, claims.actor, actorId) === 'admin', 'repository_admin_required', 403); + const { data: run } = await github.rest.actions.getWorkflowRun({ ...repo, run_id: Number(identity.runId) }); + requireCondition(run.actor?.type === 'User' && run.actor.id === actorId && run.repository.id === metadata.id && run.repository.owner.id === metadata.owner.id && + run.head_sha === branch.commit.sha && run.event === 'workflow_dispatch' && run.path === '.github/workflows/codexbot.yml' && run.run_attempt === Number(identity.attempt), 'workflow_run_mismatch', 403); + return Response.json({ status: 'verified', scope: 'diagnostic_only' }); + } if (url.pathname === '/v1/capabilities' && request.method === 'GET') { const github = client(bearer(request)), repository = url.searchParams.get('repository') ?? ''; const capabilities = await repositoryCapabilities(github, repository); @@ -63,14 +99,15 @@ async function route(request: Request, env: Env): Promise { const { data: actor } = await github.rest.users.getAuthenticated(); const body = record(JSON.parse(new TextDecoder().decode(await boundedBody(request, 1024))) as unknown); requireCondition(typeof body.id === 'string' && /^[a-zA-Z0-9-]{1,100}$/.test(body.id), 'invalid_recovery_id'); - requireCondition(body.action === undefined || ['retry','cancel'].includes(String(body.action)), 'invalid_recovery_action'); + requireCondition(body.action === undefined || ['retry','cancel','retry-progress'].includes(String(body.action)), 'invalid_recovery_action'); if (body.action === 'cancel') { const snapshot = await store.cancellable(body.id, repo.id, installation.id); const { github: cleanup } = await installationClient(env, installation.id, repo.id, { pull_requests: 'write', ...(snapshot.command.merge ? { checks: 'write' } as const : {}) }); await discardPendingReview(cleanup, snapshot, env.GITHUB_APP_SLUG); const merged = snapshot.command.merge ? await invalidateMerge(cleanup, snapshot, env.GITHUB_APP_SLUG, Number(env.GITHUB_APP_ID)) : false; await store.terminateIntent(snapshot.id, merged); await store.release(snapshot); - } else await store.operatorRetry(body.id, repo.id, installation.id); + } else if (body.action === 'retry-progress') await store.operatorRetryProgress(body.id, repo.id, installation.id); + else await store.operatorRetry(body.id, repo.id, installation.id); console.info(JSON.stringify({ event: 'operator_recovery_requested', action: body.action ?? 'retry', actor_id: actor.id, repository_id: repo.id, installation_id: installation.id })); } return Response.json(await store.operations(repo.id, installation.id)); @@ -118,7 +155,7 @@ async function route(request: Request, env: Env): Promise { else return Response.json({ installation_id: installation.id, session: await store.session(repo.id, user.id, installation.id) }); return Response.json({ status: 'ok' }); } - const match = /^\/v1\/requests\/([a-f0-9-]{36})\/(prepare|rotate|result|publish|release)$/.exec(url.pathname); + const match = /^\/v1\/requests\/([a-f0-9-]{36})\/(prepare|rotate|result|publish|release|failure|stage)$/.exec(url.pathname); if (!match || request.method !== 'POST') throw new AppError('not_found', 404); const id = match[1]!, operation = match[2]!; const { row, snapshot } = await store.get(id); @@ -137,10 +174,24 @@ async function route(request: Request, env: Env): Promise { requireCondition(run.actor?.type === 'Bot' && run.actor.login === `${env.GITHUB_APP_SLUG}[bot]`, 'workflow_not_dispatched_by_this_app', 403); requireCondition(row.dispatch_attempt && run.display_title === `CodexBot ${snapshot.id} ${row.dispatch_attempt}`, 'workflow_dispatch_attempt_mismatch', 403); if (operation === 'release') { await store.release(snapshot); return Response.json({ status: 'released' }); } + if (operation === 'failure') { + requireCondition(identity.attempt === '1', 'use_authorized_rerun_command_instead_of_workflow_rerun', 403); + const input = failure(JSON.parse(new TextDecoder().decode(await boundedBody(request, 1024))) as unknown); + const failed = await store.failRun(id, identity.runId, input.code, input.stage, input.status); + const current = await store.progressRequest(id); + requireCondition(current?.row.run_id === identity.runId, 'request_bound_to_another_run', 403); + if (failed || current.row.state === 'failed') await store.release(snapshot); + await notify(env, store, snapshot); return Response.json({ status: 'failed' }); + } requireCondition(snapshot.config.credentials.mode !== 'chatgpt' || env.CHATGPT_DEPLOYMENT_AUTHORIZED === 'true', 'chatgpt_deployment_authorization_revoked', 403); requireCondition(operation === 'publish' || identity.attempt === '1', 'use_authorized_rerun_command_instead_of_workflow_rerun', 403); + if (operation === 'stage') { + const body = record(JSON.parse(new TextDecoder().decode(await boundedBody(request, 1024))) as unknown); + requireCondition(body.stage === 'analyzing', 'invalid_review_stage'); + await store.stage(id, identity.runId, 'analyzing'); await notify(env, store, snapshot); + return Response.json({ status: 'recorded' }); + } if (operation === 'prepare') { - requireCondition(await store.prepare(id, identity.runId), 'request_already_started', 409); const { data: pr } = await github.rest.pulls.get({ ...splitRepo(snapshot.repository), pull_number: snapshot.pr }); requireCondition(pr.head.sha === snapshot.head && pr.base.sha === snapshot.base && pr.state === 'open' && !pr.draft, 'stale_pull_request', 409); const { data: branch } = await github.rest.repos.getBranch({ ...splitRepo(snapshot.repository), branch: snapshot.defaultBranch }); @@ -149,11 +200,13 @@ async function route(request: Request, env: Env): Promise { await verifyActor(github, snapshot.actor, snapshot.actorId); const livePermission = await permission(github, snapshot.repository, snapshot.actor); requireCondition(config.review.enabled && authorize(snapshot.actor, livePermission, config.authorization), 'review_authorization_revoked', 403); + requireCondition(await store.prepare(id, identity.runId), 'request_already_started', 409); // Acquire before reading: reconnect must not replace the record between snapshot and lease. if (snapshot.config.credentials.mode === 'chatgpt') await store.acquire(snapshot); const credentialSession = snapshot.config.credentials.mode === 'chatgpt' ? await store.session(snapshot.repositoryId, snapshot.actorId, snapshot.installationId) : null; if (snapshot.config.credentials.mode === 'chatgpt') requireCondition(credentialSession, 'chatgpt_session_not_connected', 409); const { token } = await installationClient(env, snapshot.installationId, snapshot.repositoryId, { contents: 'read', pull_requests: 'read' }); + await notify(env, store, snapshot); return Response.json({ snapshot, read_token: token, session: credentialSession }); } if (operation === 'rotate') { @@ -166,24 +219,35 @@ async function route(request: Request, env: Env): Promise { if (operation === 'result') { const body = record(JSON.parse(new TextDecoder().decode(await boundedBody(request, MAX_RESULT_BODY_BYTES))) as unknown); requireCondition(typeof body.context_complete === 'boolean' && body.output !== undefined, 'invalid_result_submission'); + const input = body.failure !== undefined ? failure(body.failure) : null; + if (input) requireCondition(body.context_complete === false, 'failure_requires_incomplete_result'); await store.result(id, body.output, body.context_complete, identity.runId); + if (input) { + await store.noteFailure(id, identity.runId, input.code, input.stage, input.status); + } + await notify(env, store, snapshot); return Response.json({ status: 'recorded' }); } if (row.state === 'published') return Response.json({ status: 'already_published', review_id: row.review_id }); + if (row.state === 'failed') { await notify(env, store, snapshot); return Response.json({ status: 'review_failed' }); } if (snapshot.config.credentials.mode === 'chatgpt') requireCondition(await store.session(snapshot.repositoryId, snapshot.actorId, snapshot.installationId), 'chatgpt_connection_revoked', 403); requireCondition(['reviewed','publishing'].includes(row.state) && row.result !== null, 'request_not_ready_for_publication', 409); + const input = record(JSON.parse(new TextDecoder().decode(await boundedBody(request, 1024))) as unknown); + requireCondition(input.review_job_failed === undefined || typeof input.review_job_failed === 'boolean', 'invalid_publication_job_state'); const lease = await store.claimPublication(id, identity.runId); + if (input.review_job_failed === true) await store.noteFailure(id, identity.runId, 'review_job_failed', 'workflow'); + await notify(env, store, snapshot); try { const { github: publisher } = await installationClient(env, snapshot.installationId, snapshot.repositoryId, { contents: snapshot.command.merge ? 'write' : 'read', pull_requests: 'write', ...(snapshot.command.merge && env.MERGE_DEPLOYMENT_AUTHORIZED === 'true' ? { checks: 'write', administration: 'read' } as const : {}) }); - const result = await publish(publisher, snapshot, JSON.parse(row.result) as unknown, row.context_complete === 1, env.GITHUB_APP_SLUG, { + const result = await publish(publisher, snapshot, JSON.parse(row.result) as unknown, row.context_complete === 1 && input.review_job_failed !== true, env.GITHUB_APP_SLUG, { checkpoint: (reviewId, phase, merge) => store.checkpoint(id, lease, reviewId, phase, merge), merge: { enabled: env.MERGE_DEPLOYMENT_AUTHORIZED === 'true', appId: Number(env.GITHUB_APP_ID) }, beforeWrite: () => store.heartbeat(id, lease) }); - await store.published(id, result.reviewId, lease); return Response.json(result); + await store.published(id, result.reviewId, lease); await notify(env, store, snapshot); return Response.json(result); } catch (error) { await store.publicationFailed(id, lease, error instanceof AppError && error.status < 500 && error.status !== 429); throw error; } finally { await store.release(snapshot); } } @@ -204,5 +268,11 @@ export default { return Response.json({ error: code }, { status: error instanceof AppError ? error.status : 500, headers: { 'Cache-Control': 'no-store' } }); } }, - async scheduled(_event: ScheduledController, env: Env): Promise { await recover(env); } + async scheduled(event: ScheduledController, env: Env): Promise { + if (event.cron === '*/2 * * * *') { + // A separate bounded invocation prevents busy dispatch/publication queues + // from starving terminal comment updates or sharing their subrequest budget. + const store = new Store(env.DB); await store.cleanup(); await recoverProgress(env, store); + } else await recover(env); + } } satisfies ExportedHandler; diff --git a/src/worker/oidc.ts b/src/worker/oidc.ts index 90d8fcd..a59719b 100644 --- a/src/worker/oidc.ts +++ b/src/worker/oidc.ts @@ -1,6 +1,6 @@ -import { createRemoteJWKSet, jwtVerify } from 'jose'; +import { createRemoteJWKSet, jwtVerify, errors } from 'jose'; import type { JWTVerifyGetKey, JWTPayload } from 'jose'; -import { requireCondition } from '../core/errors.js'; +import { requireCondition, AppError } from '../core/errors.js'; export interface WorkflowIdentity { repository: string; repositoryId: number; ownerId: number; defaultBranch: string; @@ -9,12 +9,22 @@ export interface WorkflowIdentity { export interface Workload { runId: string; attempt: string; jti: string } const githubKeys = createRemoteJWKSet(new URL('https://token.actions.githubusercontent.com/.well-known/jwks')); export async function verifyWorkload(token: string, audience: string, expected: WorkflowIdentity, keys?: JWTVerifyGetKey): Promise { + return checkClaims(await verifySignedClaims(token, audience, keys), expected); +} +export async function verifySignedClaims(token: string, audience: string, keys?: JWTVerifyGetKey): Promise { const resolver = keys ?? githubKeys; - const { payload } = await jwtVerify(token, resolver, { + let payload: JWTPayload; + try { ({ payload } = await jwtVerify(token, resolver, { issuer: 'https://token.actions.githubusercontent.com', audience, algorithms: ['RS256'], maxTokenAge: '10m', requiredClaims: ['exp', 'iat', 'nbf', 'jti', 'sub'] - }); - return checkClaims(payload, expected); + })); } + catch (error) { + if (error instanceof errors.JWTExpired) throw new AppError('oidc_token_expired', 401); + if (error instanceof errors.JWKSTimeout) throw new AppError('oidc_jwks_unavailable', 503); + if (error instanceof errors.JOSEError) throw new AppError('oidc_token_invalid', 401); + throw new AppError('oidc_jwks_unavailable', 503); + } + return payload; } export function checkClaims(claims: JWTPayload, expected: WorkflowIdentity): Workload { const workflowRef = `${expected.repository}/.github/workflows/codexbot.yml@refs/heads/${expected.defaultBranch}`; diff --git a/src/worker/progress.ts b/src/worker/progress.ts new file mode 100644 index 0000000..0aa7a09 --- /dev/null +++ b/src/worker/progress.ts @@ -0,0 +1,121 @@ +import type { Octokit } from '@octokit/rest'; +import { splitRepo, installationClient, appClient } from './github.js'; +import type { Store } from './store.js'; +import type { RequestRow, RequestSnapshot } from './store.js'; +import { sha256 } from '../core/crypto.js'; +import { requireCondition } from '../core/errors.js'; + +const STATUSES = new Set(['queued','in_progress','completed','requested','waiting','pending']); +const CONCLUSIONS = new Set(['success','failure','cancelled','timed_out','action_required','stale','skipped','neutral','startup_failure']); +function marker(snapshot: RequestSnapshot): string { return ``; } +function phase(row: RequestRow): string { + if (row.state === 'failed' || (row.state === 'published' && row.failure_code)) return 'failed'; + if (row.state === 'published') return 'completed'; + if (row.state === 'publishing') return 'publishing'; + if (row.state === 'reviewed') return 'awaiting publication'; + if (row.state === 'running') return row.progress_stage === 'analyzing' ? 'analyzing' : 'running'; + if (row.workflow_status === 'in_progress') return 'running'; + return 'queued'; +} +export function progressBody(snapshot: RequestSnapshot, row: RequestRow, statusUnavailable = false): string { + splitRepo(snapshot.repository); + requireCondition(/^[a-f0-9-]{36}$/.test(snapshot.id) && Number.isSafeInteger(snapshot.pr) && snapshot.pr > 0, 'invalid_progress_identity'); + const counts = Number.isSafeInteger(snapshot.commits) && Number.isSafeInteger(snapshot.changedFiles) + ? `${snapshot.commits} PR commits; ${snapshot.changedFiles} changed files at acceptance.` : 'Counts were not recorded for this legacy request.'; + const run = row.run_id && /^\d+$/.test(row.run_id) ? `[GitHub Actions run](https://github.com/${snapshot.repository}/actions/runs/${row.run_id})` : 'Awaiting verified GitHub Actions run discovery.'; + const status = row.workflow_status && STATUSES.has(row.workflow_status) ? row.workflow_status : 'not yet confirmed'; + const conclusion = row.workflow_conclusion && CONCLUSIONS.has(row.workflow_conclusion) ? ` / ${row.workflow_conclusion}` : ''; + const review = row.review_id && Number.isSafeInteger(row.review_id) ? `\n\n[Native review](https://github.com/${snapshot.repository}/pull/${snapshot.pr}#pullrequestreview-${row.review_id}).` : ''; + // Every variable is a validated identifier/count or a fixed vocabulary. No prompt, + // model text, source, remote URL or internal error is part of this public channel. + return `${marker(snapshot)}\nCodexBot accepted review request \`${snapshot.id.slice(0, 8)}\`.\n\n${counts}\n\n**Stage: ${phase(row)}.**\n\n${statusUnavailable ? 'Workflow details are temporarily unavailable; no current run status is asserted.' : `Workflow: ${status}${conclusion}. ${run}`}${review}${phase(row) === 'failed' ? '\n\nReview processing did not complete. Check the workflow diagnostics, resolve the reported prerequisite and request a new review.' : ''}`; +} +async function observe(github: Octokit, store: Store, snapshot: RequestSnapshot, row: RequestRow, slug: string): Promise { + if (row.workflow_status === 'completed' || Date.now() - row.workflow_checked_at < 15_000 || !row.dispatch_attempt) return; + const repo = splitRepo(snapshot.repository); + const matches = (run: { event: string; head_sha: string; display_title: string; path: string; repository: { id: number; owner: { id: number } }; actor?: { login: string; type: string } | null }) => + run.event === 'workflow_dispatch' && run.head_sha === snapshot.workflowSha && run.path === '.github/workflows/codexbot.yml' && + run.repository.id === snapshot.repositoryId && run.repository.owner.id === snapshot.ownerId && run.actor?.type === 'Bot' && run.actor.login === `${slug}[bot]` && + run.display_title === `CodexBot ${snapshot.id} ${row.dispatch_attempt}`; + let run; + if (row.run_id) run = (await github.rest.actions.getWorkflowRun({ ...repo, run_id: Number(row.run_id) })).data; + else { + const { data } = await github.rest.actions.listWorkflowRuns({ ...repo, workflow_id: 'codexbot.yml', event: 'workflow_dispatch', branch: snapshot.defaultBranch, + created: `>=${new Date(snapshot.createdAt - 60_000).toISOString()}`, per_page: 100 }); + run = data.workflow_runs.find(matches); + } + if (!run) return; + requireCondition(matches(run), 'progress_workflow_identity_mismatch', 403); + requireCondition(run.status && STATUSES.has(run.status) && (!run.conclusion || CONCLUSIONS.has(run.conclusion)), 'progress_unknown_workflow_status'); + if (!await store.observeRun(snapshot.id, String(run.id), run.status, run.conclusion)) return; + if (run.status === 'completed' && ['created','dispatched','running','reviewed'].includes(row.state)) { + if (await store.failRun(snapshot.id, String(run.id), 'workflow_ended_before_publication', 'workflow')) await store.release(snapshot); + } +} +export async function updateProgress(github: Octokit, store: Store, snapshot: RequestSnapshot, slug: string, appId: number): Promise { + const lease = await store.claimProgress(snapshot.id); if (!lease) return; + let creating = false; + try { + let current = await store.progressRequest(snapshot.id); requireCondition(current, 'request_not_found', 404); + const repo = splitRepo(snapshot.repository), { data: repository } = await github.rest.repos.get(repo); + requireCondition(repository.id === snapshot.repositoryId && repository.owner.id === snapshot.ownerId, 'repository_identity_changed', 403); + let statusUnavailable = false; + try { await observe(github, store, snapshot, current.row, slug); } + catch { + statusUnavailable = true; + console.error(JSON.stringify({ event: 'workflow_observation_deferred', request_id: snapshot.id })); + } + current = await store.progressRequest(snapshot.id); requireCondition(current, 'request_not_found', 404); + const { row } = current, body = progressBody(snapshot, row, statusUnavailable), hash = await sha256(body); + const owned = (comment: { user: { type: string; login: string } | null; performed_via_github_app?: { id: number } | null; issue_url: string; body?: string }) => + comment.user?.type === 'Bot' && comment.user.login === `${slug}[bot]` && comment.performed_via_github_app?.id === appId && + comment.issue_url === `https://api.github.com/repos/${snapshot.repository}/issues/${snapshot.pr}` && comment.body?.startsWith(marker(snapshot)); + let comment; + if (row.progress_comment_id) { + try { comment = (await github.rest.issues.getComment({ ...repo, comment_id: row.progress_comment_id })).data; } + catch (error) { + if (error && typeof error === 'object' && 'status' in error && error.status === 404) { await store.progressDeferred(snapshot.id, lease, { deleted: true }); return; } + throw error; + } + requireCondition(owned(comment), 'progress_comment_ownership_mismatch', 403); + } else if (row.progress_phase === 'creating') { + // A lost POST response is reconciled, never blindly posted again. A crash + // before POST can therefore require operator attention, rather than spam. + for (let page = 1; page <= 3 && !comment; page++) { + const { data } = await github.rest.issues.listComments({ ...repo, issue_number: snapshot.pr, per_page: 100, page }); + comment = data.find(owned); if (data.length < 100) break; + } + if (!comment) { await store.progressDeferred(snapshot.id, lease, { failed: true }); return; } + } else { + await store.progressCreating(snapshot.id, lease); creating = true; + comment = (await github.rest.issues.createComment({ ...repo, issue_number: snapshot.pr, body })).data; + creating = false; // A returned but unverifiable comment is also an ambiguous side effect. + requireCondition(owned(comment), 'progress_comment_ownership_mismatch', 403); + } + if (comment.body !== body) { + await store.progressWriteAllowed(snapshot.id, lease); + await github.rest.issues.updateComment({ ...repo, comment_id: comment.id, body }); + } + await store.progressSaved(snapshot.id, lease, comment.id, hash, phase(row), statusUnavailable); + if (statusUnavailable) await store.progressDeferred(snapshot.id, lease, { failed: true }); + } catch (error) { + const status = error && typeof error === 'object' && 'status' in error ? Number(error.status) : 0; + await store.progressDeferred(snapshot.id, lease, { failed: true, definitive: creating && [400,401,403,404,410,422,429].includes(status) }); + console.error(JSON.stringify({ event: 'progress_update_deferred', request_id: snapshot.id, ...(status >= 400 && status <= 599 ? { status } : {}) })); + } +} +export async function recoverProgress(env: Env, store: Store): Promise { + const id = await store.dueProgress(); if (!id) return; + const current = await store.progressRequest(id); if (!current) return; + const { snapshot } = current; + try { + const { data } = await (await appClient(env)).rest.apps.getRepoInstallation(splitRepo(snapshot.repository)); + requireCondition(data.id === snapshot.installationId && !data.suspended_at, 'installation_no_longer_valid', 403); + const { github } = await installationClient(env, snapshot.installationId, snapshot.repositoryId, { contents: 'read', pull_requests: 'write', actions: 'read' }); + await updateProgress(github, store, snapshot, env.GITHUB_APP_SLUG, Number(env.GITHUB_APP_ID)); + } catch { + const lease = await store.claimProgress(id); + if (lease) await store.progressDeferred(id, lease, { failed: true }); + console.error(JSON.stringify({ event: 'progress_recovery_deferred', request_id: id })); + } +} diff --git a/src/worker/recovery.ts b/src/worker/recovery.ts index 9fe1e5c..1fb9149 100644 --- a/src/worker/recovery.ts +++ b/src/worker/recovery.ts @@ -6,6 +6,7 @@ import { publish } from './publish.js'; import { invalidateMerge } from './merge.js'; import type { RequestSnapshot } from './store.js'; import { MAX_RECOVERY_ATTEMPTS } from '../core/limits.js'; +import { recoverProgress } from './progress.js'; export async function processDelivery(id: string, env: Env): Promise { const store = new Store(env.DB), delivery = await store.claimDelivery(id); @@ -34,7 +35,8 @@ export async function recover(env: Env): Promise { await store.cleanup(); return; } const publications = await store.duePublications(); - for (const id of publications.length ? [] : (await store.dueDeliveries()).slice(0, 1)) { + const deliveries = publications.length ? [] : (await store.dueDeliveries()).slice(0, 1); + for (const id of deliveries) { try { await processDelivery(id, env); } catch { console.error(JSON.stringify({ event: 'delivery_recovery_deferred' })); } } for (const id of publications) { @@ -68,4 +70,5 @@ export async function recover(env: Env): Promise { } } await store.cleanup(); + if (!publications.length && !deliveries.length) await recoverProgress(env, store); } diff --git a/src/worker/store.ts b/src/worker/store.ts index 8c9ef37..96387ce 100644 --- a/src/worker/store.ts +++ b/src/worker/store.ts @@ -9,11 +9,15 @@ import { MAX_REVIEW_BYTES, MAX_RECOVERY_ATTEMPTS, RECOVERY_LEASE_MS, REQUEST_LIF export interface RequestSnapshot { id: string; installationId: number; repositoryId: number; ownerId: number; repository: string; defaultBranch: string; workflowSha: string; head: string; base: string; pr: number; actor: string; actorId: number; command: ReviewCommand; config: Config; createdAt: number; + commits?: number; changedFiles?: number; } export interface RequestRow { id: string; snapshot: string; state: string; run_id: string | null; review_id: number | null; result: string | null; context_complete: number; dispatch_attempt: string | null; dispatch_phase: string; dispatch_started: number | null; publish_phase: string; publish_attempts: number; publish_lease: string | null; publish_until: number; next_publish: number; review_phase: string; merge_phase: string; + progress_comment_id: number | null; progress_phase: string; progress_lease: string | null; progress_until: number; progress_attempts: number; progress_next_at: number; + progress_hash: string | null; progress_reported_stage: string | null; progress_stage: string; failure_code: string | null; failure_stage: string | null; failure_status: number | null; + workflow_status: string | null; workflow_conclusion: string | null; workflow_checked_at: number; } export interface DeliveryRow { id: string; event: string; payload: string; request_id: string; attempts: number; lease: string } export function retryDelay(attempt: number): number { @@ -140,7 +144,7 @@ export class Store { .bind(merged ? 'merged' : 'cancelled', merged ? 'published' : 'failed', merged ? 'published' : 'failed', id).run(); } async operations(repository: number, installation: number): Promise { - const requests = await this.db.prepare('SELECT id,state,dispatch_phase,publish_phase,merge_phase,publish_attempts,run_id,review_id FROM requests WHERE repository_id=? AND installation_id=? ORDER BY created_at DESC LIMIT 25').bind(repository, installation).all(); + const requests = await this.db.prepare('SELECT id,state,dispatch_phase,publish_phase,merge_phase,publish_attempts,run_id,review_id,progress_comment_id,progress_phase,progress_attempts,failure_code,failure_stage,failure_status FROM requests WHERE repository_id=? AND installation_id=? ORDER BY created_at DESC LIMIT 25').bind(repository, installation).all(); const deliveries = await this.db.prepare(`SELECT id,phase,attempts,error_code,request_id FROM deliveries WHERE json_extract(payload,'$.repository.id')=? AND json_extract(payload,'$.installation.id')=? ORDER BY created_at DESC LIMIT 25`).bind(repository, installation).all(); return { requests: requests.results, deliveries: deliveries.results }; } @@ -150,10 +154,17 @@ export class Store { .bind(id, repository, installation).run(); if (result.meta.changes) return; const publication = await this.db.prepare(`UPDATE requests SET state='publishing',publish_phase='retryable',publish_until=0,next_publish=0,publish_attempts=0,operator_retries=operator_retries+1 - WHERE id=? AND repository_id=? AND installation_id=? AND publish_phase IN ('failed','retryable') AND merge_phase NOT IN ('cancelled','cancelling') AND operator_retries<2 AND created_at>? AND result IS NOT NULL`) + WHERE id=? AND repository_id=? AND installation_id=? AND publish_phase IN ('failed','retryable') AND publish_attempts>0 AND merge_phase NOT IN ('cancelled','cancelling') AND operator_retries<2 AND created_at>? AND result IS NOT NULL`) .bind(id, repository, installation, Date.now() - REQUEST_LIFETIME_MS).run(); requireCondition(publication.meta.changes === 1, 'retry_not_allowed_or_limit_reached', 409); } + async operatorRetryProgress(id: string, repository: number, installation: number): Promise { + // Never reset an ambiguous POST to pending or recreate a deleted comment. + const result = await this.db.prepare(`UPDATE requests SET progress_attempts=0,progress_next_at=0,operator_retries=operator_retries+1 WHERE id=? AND repository_id=? AND installation_id=? + AND progress_until=? AND operator_retries<2 AND created_at>?`) + .bind(id, repository, installation, Date.now(), MAX_RECOVERY_ATTEMPTS, Date.now() - 7 * 86_400_000).run(); + requireCondition(result.meta.changes === 1, 'progress_retry_not_allowed_or_limit_reached', 409); + } async cancellable(id: string, repository: number, installation: number): Promise { // Claim cancellation atomically before remote cleanup; a publisher cannot acquire afterwards. const row = await this.db.prepare(`UPDATE requests SET state='failed',publish_phase='failed',merge_phase='cancelling' WHERE id=? AND repository_id=? AND installation_id=? AND publish_until { + const row = await this.db.prepare('SELECT * FROM requests WHERE id=?').bind(id).first(); + return row ? { row, snapshot: JSON.parse(row.snapshot) as RequestSnapshot } : null; + } + async stage(id: string, runId: string, stage: 'analyzing'): Promise { + const result = await this.db.prepare(`UPDATE requests SET progress_stage=? WHERE id=? AND run_id=? AND state='running'`).bind(stage, id, runId).run(); + requireCondition(result.meta.changes === 1, 'request_not_running', 409); + } + async failRun(id: string, runId: string, code: string, stage: string, status: number | null = null): Promise { + const result = await this.db.prepare(`UPDATE requests SET state='failed',publish_phase='failed',progress_stage='failed',failure_code=?,failure_stage=?,failure_status=?,run_id=COALESCE(run_id,?) + WHERE id=? AND state IN ('created','dispatched','running','reviewed') AND (run_id IS NULL OR run_id=?) AND publish_attempts=0`) + .bind(code, stage, status, runId, id, runId).run(); return result.meta.changes === 1; + } + async noteFailure(id: string, runId: string, code: string, stage: string, status: number | null = null): Promise { + await this.db.prepare(`UPDATE requests SET failure_code=?,failure_stage=?,failure_status=?,context_complete=CASE WHEN ?='review_job_failed' THEN 0 ELSE context_complete END WHERE id=? AND run_id=?`) + .bind(code, stage, status, code, id, runId).run(); + } + async observeRun(id: string, runId: string, status: string, conclusion: string | null): Promise { + const result = await this.db.prepare(`UPDATE requests SET run_id=COALESCE(run_id,?),workflow_status=?,workflow_conclusion=?,workflow_checked_at=? WHERE id=? AND (run_id IS NULL OR run_id=?)`) + .bind(runId, status, conclusion, Date.now(), id, runId).run(); return result.meta.changes === 1; + } + async claimProgress(id: string): Promise { + const lease = crypto.randomUUID(), now = Date.now(); + const result = await this.db.prepare(`UPDATE requests SET progress_lease=?,progress_until=? WHERE id=? AND progress_until { + const result = await this.db.prepare(`UPDATE requests SET progress_phase='creating' WHERE id=? AND progress_lease=? AND progress_until>? AND progress_comment_id IS NULL AND progress_phase='pending'`) + .bind(id, lease, Date.now()).run(); requireCondition(result.meta.changes === 1, 'progress_creation_lease_lost', 409); + } + async progressWriteAllowed(id: string, lease: string): Promise { + const row = await this.db.prepare('SELECT 1 FROM requests WHERE id=? AND progress_lease=? AND progress_until>?').bind(id, lease, Date.now()).first(); + requireCondition(row, 'progress_update_lease_lost', 409); + } + async progressSaved(id: string, lease: string, commentId: number, hash: string, stage: string, keepFailures = false): Promise { + const result = await this.db.prepare(`UPDATE requests SET progress_comment_id=?,progress_phase='ready',progress_hash=?,progress_reported_stage=?,progress_attempts=CASE WHEN ? THEN progress_attempts ELSE 0 END,progress_until=0,progress_next_at=? WHERE id=? AND progress_lease=? AND progress_until>?`) + .bind(commentId, hash, stage, keepFailures ? 1 : 0, Date.now() + 15_000, id, lease, Date.now()).run(); requireCondition(result.meta.changes === 1, 'progress_update_lease_lost', 409); + } + async progressDeferred(id: string, lease: string, options: { deleted?: boolean; definitive?: boolean; failed?: boolean } = {}): Promise { + await this.db.prepare(`UPDATE requests SET progress_phase=CASE WHEN ? THEN 'deleted' WHEN ? AND progress_comment_id IS NULL THEN 'pending' ELSE progress_phase END, + progress_until=0,progress_attempts=progress_attempts+?,progress_next_at=? WHERE id=? AND progress_lease=?`) + .bind(options.deleted ? 1 : 0, options.definitive ? 1 : 0, options.failed ? 1 : 0, Date.now() + (options.failed ? 60_000 : 15_000), id, lease).run(); + } + async dueProgress(): Promise { + const row = await this.db.prepare(`SELECT id FROM requests WHERE progress_until? AND NOT (state IN ('published','failed') AND progress_phase='ready' AND progress_reported_stage=CASE WHEN state='failed' OR failure_code IS NOT NULL THEN 'failed' ELSE 'completed' END + AND (COALESCE(workflow_status,'')='completed' OR (run_id IS NULL AND created_at(); + return row?.id ?? null; + } async cleanup(): Promise { await this.db.batch([ - this.db.prepare(`UPDATE sessions SET lease_request=NULL,lease_until=0 WHERE lease_request IS NOT NULL AND (lease_until=?`).bind(Date.now(), MAX_RECOVERY_ATTEMPTS), this.db.prepare(`UPDATE requests SET state='failed',publish_phase='failed' WHERE state='publishing' AND publish_until=?`).bind(Date.now(), MAX_RECOVERY_ATTEMPTS), - this.db.prepare(`UPDATE requests SET state='failed',publish_phase='failed' WHERE state IN ('created','dispatched','running','reviewed','publishing') AND created_at 0 && Number.isSafeInteger(pr.changed_files) && pr.changed_files >= 0, 'invalid_pr_counts'); let snapshot: RequestSnapshot = { id: requestId, installationId: Number(installation.id), repositoryId: liveRepo.id, ownerId: liveRepo.owner.id, repository: liveRepo.full_name, defaultBranch: liveRepo.default_branch, workflowSha: branch.commit.sha, - head: pr.head.sha, base: pr.base.sha, pr: prNumber, actor: sender.login, actorId: Number(sender.id), command, config, createdAt: Date.now() + head: pr.head.sha, base: pr.base.sha, pr: prNumber, actor: sender.login, actorId: Number(sender.id), command, config, createdAt: Date.now(), commits: pr.commits, changedFiles: pr.changed_files }; snapshot = await store.create(snapshot, env.TRUSTED_WORKFLOW); requireCondition(snapshot.head === pr.head.sha && snapshot.base === pr.base.sha && snapshot.workflowSha === branch.commit.sha, 'request_context_changed', 409); + try { await updateProgress(github, store, snapshot, env.GITHUB_APP_SLUG, Number(env.GITHUB_APP_ID)); } + catch { console.error(JSON.stringify({ event: 'progress_update_deferred', request_id: snapshot.id })); } await dispatchRequest(github, store, snapshot, env.GITHUB_APP_SLUG); return 'dispatched'; } diff --git a/tests/broker.test.ts b/tests/broker.test.ts new file mode 100644 index 0000000..0ef592e --- /dev/null +++ b/tests/broker.test.ts @@ -0,0 +1,48 @@ +import { test, mock } from 'node:test'; +import assert from 'node:assert/strict'; +import { broker } from '../src/runner/broker.js'; +import { diagnostic, BrokerError } from '../src/core/diagnostics.js'; +import { reviewJob } from '../src/runner/main.js'; +import { snapshot } from './fixtures.js'; + +const id = snapshot().id, secret = 'opaque-oauth-code fixture-access-token Authorization: Bearer fixture-secret'; +async function fixture(action: (calls: string[], logs: unknown[][]) => Promise, response: () => Response = () => Response.json({ error: 'oidc_environment_mismatch', message: secret }, { status: 403 })) { + const env = { ...process.env }, calls: string[] = [], logs: unknown[][] = []; + process.env.ACTIONS_ID_TOKEN_REQUEST_URL = 'https://oidc.invalid/token'; process.env.ACTIONS_ID_TOKEN_REQUEST_TOKEN = secret; + process.env.CODEXBOT_BACKEND = 'https://broker.invalid'; process.env.CODEXBOT_REQUEST_ID = id; + process.env.CODEXBOT_CREDENTIAL_OWNER = '5'; process.env.CODEXBOT_INSTALLATION_ID = '1'; + const fetcher = mock.method(globalThis, 'fetch', async (input: Parameters[0]) => { + const url = new URL(String(input)); if (url.hostname === 'oidc.invalid') return Response.json({ value: secret }); + calls.push(url.pathname); return response(); + }); + const logger = mock.method(console, 'error', (...args: unknown[]) => { logs.push(args); }); + try { await action(calls, logs); } + finally { fetcher.mock.restore(); logger.mock.restore(); for (const key of Object.keys(process.env)) if (!(key in env)) delete process.env[key]; Object.assign(process.env, env); } +} +await test('broker keeps only HTTP status and known remote codes, never credentials, headers or response bodies', async () => fixture(async () => { + await assert.rejects(broker('https://broker.invalid', id, 'prepare'), error => { + assert.ok(error instanceof BrokerError); assert.equal(error.status, 403); assert.equal(error.remoteCode, 'oidc_environment_mismatch'); + const text = JSON.stringify(diagnostic(error, 'prepare')); assert.ok(!text.includes(secret)); assert.ok(!text.includes('fixture-access-token')); return true; + }); +})); +await test('oversized or nonstandard broker errors cannot become diagnostic codes', async () => fixture(async () => { + await assert.rejects(broker('https://broker.invalid', id, 'prepare'), error => { + assert.ok(error instanceof BrokerError); assert.equal(error.remoteCode, null); assert.equal(error.status, 502); return true; + }); +}, () => new Response(JSON.stringify({ error: secret, extra: secret.repeat(1000) }), { status: 502 }))); +await test('missing or ambiguous Environment credentials fail closed, report failure, and never release a lease that was not acquired', async () => fixture(async (calls, logs) => { + delete process.env.CODEXBOT_VAULT_KEY; delete process.env.CODEXBOT_API_KEY; + await assert.rejects(reviewJob(), /missing_environment_credentials/); + assert.ok(!calls.some(path => path.endsWith('/prepare') || path.endsWith('/release'))); + assert.ok(logs.some(args => String(args[0]).includes('missing_environment_credentials'))); + process.env.CODEXBOT_VAULT_KEY = 'fixture-vault'; process.env.CODEXBOT_API_KEY = 'fixture-api'; + await assert.rejects(reviewJob(), /ambiguous_environment_credentials/); + assert.ok(!JSON.stringify(logs).includes('fixture-access-token')); +})); +await test('broker/OIDC rejection preserves its primary failure and does not pretend preparation acquired a session', async () => fixture(async (calls, logs) => { + process.env.CODEXBOT_VAULT_KEY = 'fixture-vault'; delete process.env.CODEXBOT_API_KEY; + await assert.rejects(reviewJob(), /broker_prepare_failed/); + assert.ok(calls.some(path => path.endsWith('/prepare'))); assert.ok(!calls.some(path => path.endsWith('/release'))); + assert.ok(logs.some(args => String(args[0]).includes('oidc_environment_mismatch'))); + assert.ok(!JSON.stringify(logs).includes('fixture-access-token')); +})); diff --git a/tests/core.test.ts b/tests/core.test.ts index b8fd8bc..2e7e07e 100644 --- a/tests/core.test.ts +++ b/tests/core.test.ts @@ -12,6 +12,11 @@ await test('every configured handle triggers independently and case-insensitivel assert.equal(parseCommand('@anotherbot review', handle), null); } }); +await test('the non-Codex alias is exact, preserves legacy opt-in and never aliases native connector commands', () => { + assert.ok(parseCommand('@cbreview review --effort high', '@CBReview')); + assert.ok(parseCommand('@codexbot review', '@codexbot')); + for (const text of ['@codex review', '@codexbot review', '@CBReviewExtra review', '> @CBReview review', '```text\n@CBReview review\n```']) assert.equal(parseCommand(text, '@CBReview'), null); +}); await test('handle accepts only the requested ASCII alphanumeric format', () => { for (const invalid of ['', '@', '@with space', '@bot-name', '@bot_name', '@ä', '@x!', 'CodexBotReviewTest', '@' + 'a'.repeat(40), '@a\n']) assert.throws(() => validateHandle(invalid)); assert.equal(validateHandle('@123'), '@123'); diff --git a/tests/github-fixture.ts b/tests/github-fixture.ts index c2d2a7b..ce57da1 100644 --- a/tests/github-fixture.ts +++ b/tests/github-fixture.ts @@ -7,7 +7,8 @@ export function githubFixture() { reviewDecision: 'APPROVED', autoMergeAllowed: true, reviews: [] as Record[], installationId: 1, suspended: false, merged: false, ci: 'SUCCESS', guarded: false, requiredHumans: 1, rules: [] as Record[], autoMergeOwner: null as string | null, humans: [{ id: 80, state: 'APPROVED', commit_id: HEAD, user: { id: 6, login: 'bob', type: 'User' } }] as Record[], - checks: [] as Record[], contextTruncated: false }; + checks: [] as Record[], contextTruncated: false, comments: [] as Record[], runStatus: 'in_progress', runConclusion: null as string | null, + runActor: { id: 42, type: 'Bot', login: 'codexbot[bot]' } }; const calls: { method: string; path: string; body: Record }[] = []; const fetcher: typeof fetch = async (input, init) => { const url = new URL(typeof input === 'string' ? input : input instanceof URL ? input.href : input.url); @@ -25,7 +26,18 @@ export function githubFixture() { if (path === '/repos/owner/repo/contents/.codexbot.yml') return ok({ type: 'file', size: 1000, content: Buffer.from(stringify(config)).toString('base64') }); if (path.includes('/collaborators/') && path.endsWith('/permission')) { const login = decodeURIComponent(path.split('/').at(-2)!); return ok({ permission: state.permission, user: { id: login === 'alice' ? state.userId : 6, login, type: 'User' } }); } if (path === '/users/alice' || path === '/user') return ok({ id: state.userId, login: 'alice', type: 'User' }); - if (path === '/repos/owner/repo/pulls/4') return ok({ number: 4, head: { sha: state.head }, base: { sha: state.base, ref: 'main', repo }, user: { id: state.author === 'alice' ? 5 : 7, login: state.author }, state: state.merged ? 'closed' : 'open', merged: state.merged, draft: false, changed_files: 1 }); + if (path === '/repos/owner/repo/pulls/4') return ok({ number: 4, head: { sha: state.head }, base: { sha: state.base, ref: 'main', repo }, user: { id: state.author === 'alice' ? 5 : 7, login: state.author }, state: state.merged ? 'closed' : 'open', merged: state.merged, draft: false, changed_files: 1, commits: 1 }); + if (path === '/repos/owner/repo/issues/4/comments') { + if (method === 'GET') return ok(state.comments); + const comment = { ...body, id: 1000 + state.comments.length, user: { type: 'Bot', login: 'codexbot[bot]' }, performed_via_github_app: { id: 42 }, issue_url: 'https://api.github.com/repos/owner/repo/issues/4' }; + state.comments.push(comment); return ok(comment, 201); + } + if (/\/repos\/owner\/repo\/issues\/comments\/\d+$/.test(path)) { + const comment = state.comments.find(x => String(x.id) === path.split('/').at(-1)); + if (!comment) return ok({ message: 'Not Found' }, 404); + if (method === 'PATCH') Object.assign(comment, body); + return ok(comment); + } if (path === '/repos/owner/repo/pulls/4/files') return ok(files); if (path === '/repos/owner/repo/pulls/4/reviews') { if (method === 'GET') return ok([...state.humans, ...state.reviews]); @@ -47,7 +59,7 @@ export function githubFixture() { } if (path === '/repos/owner/repo/actions/workflows/codexbot.yml/dispatches') return new Response(null, { status: 204 }); const lastDispatch = calls.filter(x => x.path.endsWith('/dispatches')).at(-1)?.body.inputs as Record | undefined; - const run = { id: 123, event: 'workflow_dispatch', head_sha: WORKFLOW, path: '.github/workflows/codexbot.yml', repository: repo, run_attempt: 1, actor: { type: 'Bot', login: 'codexbot[bot]' }, display_title: `CodexBot ${lastDispatch?.request_id} ${lastDispatch?.dispatch_attempt}` }; + const run = { id: 123, status: state.runStatus, conclusion: state.runConclusion, event: 'workflow_dispatch', head_sha: WORKFLOW, path: '.github/workflows/codexbot.yml', repository: repo, run_attempt: 1, actor: state.runActor, display_title: `CodexBot ${lastDispatch?.request_id} ${lastDispatch?.dispatch_attempt}` }; if (path === '/repos/owner/repo/actions/runs/123') return ok(run); if (path === '/repos/owner/repo/actions/workflows/codexbot.yml/runs') return ok({ workflow_runs: lastDispatch ? [run] : [] }); if (path.endsWith('/check-runs') && method === 'GET') return ok({ total_count: state.checks.length, check_runs: state.checks }); diff --git a/tests/oidc.test.ts b/tests/oidc.test.ts index 148c671..fe146ff 100644 --- a/tests/oidc.test.ts +++ b/tests/oidc.test.ts @@ -14,10 +14,10 @@ await test('OIDC cryptographically rejects forged signatures, expiry, issuer and const keys = await generateKeyPair('RS256'), wrong = await generateKeyPair('RS256'); const jwt = await new SignJWT(claims).setProtectedHeader({ alg: 'RS256' }).setIssuer('https://token.actions.githubusercontent.com').setAudience('https://broker.invalid').setIssuedAt().setNotBefore('0s').setExpirationTime('5m').sign(keys.privateKey); assert.equal((await verifyWorkload(jwt, 'https://broker.invalid', expected, () => keys.publicKey)).runId, '123'); - await assert.rejects(verifyWorkload(jwt, 'https://other.invalid', expected, () => keys.publicKey)); + await assert.rejects(verifyWorkload(jwt, 'https://other.invalid', expected, () => keys.publicKey), { code: 'oidc_token_invalid', status: 401 }); await assert.rejects(verifyWorkload(jwt, 'https://broker.invalid', expected, () => wrong.publicKey)); const expired = await new SignJWT(claims).setProtectedHeader({ alg: 'RS256' }).setIssuer('https://token.actions.githubusercontent.com').setAudience('https://broker.invalid').setIssuedAt(1).setNotBefore(1).setExpirationTime(2).sign(keys.privateKey); - await assert.rejects(verifyWorkload(expired, 'https://broker.invalid', expected, () => keys.publicKey)); + await assert.rejects(verifyWorkload(expired, 'https://broker.invalid', expected, () => keys.publicKey), { code: 'oidc_token_expired', status: 401 }); const wrongIssuer = await new SignJWT(claims).setProtectedHeader({ alg: 'RS256' }).setIssuer('https://attacker.invalid').setAudience('https://broker.invalid').setIssuedAt().setNotBefore('0s').setExpirationTime('5m').sign(keys.privateKey); await assert.rejects(verifyWorkload(wrongIssuer, 'https://broker.invalid', expected, () => keys.publicKey)); }); diff --git a/tests/progress.test.ts b/tests/progress.test.ts new file mode 100644 index 0000000..15ce318 --- /dev/null +++ b/tests/progress.test.ts @@ -0,0 +1,187 @@ +import { test, mock } from 'node:test'; +import assert from 'node:assert/strict'; +import { Octokit } from '@octokit/rest'; +import { database } from './sqlite.js'; +import { snapshot, clean } from './fixtures.js'; +import { githubFixture } from './github-fixture.js'; +import { Store } from '../src/worker/store.js'; +import { updateProgress, progressBody } from '../src/worker/progress.js'; +import { dispatchRequest } from '../src/worker/dispatch.js'; +import { randomSecret, seal } from '../src/core/crypto.js'; + +function fixture() { + const { db, sqlite } = database(), store = new Store(db), f = githubFixture(), request = { ...snapshot(), commits: 2, changedFiles: 1 }; + const ready = () => sqlite.prepare('UPDATE requests SET progress_next_at=0,workflow_checked_at=0 WHERE id=?').run(request.id); + const update = (github = f.github) => updateProgress(github, store, request, 'codexbot', 42); + return { ...f, db, sqlite, store, request, ready, update }; +} +await test('one App comment follows confirmed queued/running/analyzing/publication states and links its native review', async () => { + const f = fixture(); + try { + await f.store.create(f.request); await f.update(); + assert.equal(f.state.comments.length, 1); assert.match(String(f.state.comments[0]!.body), /2 PR commits; 1 changed files.*Stage: queued/s); + const initialCalls = f.calls.length; await f.update(); assert.equal(f.calls.length, initialCalls); // fifteen-second update bound + await dispatchRequest(f.github, f.store, f.request, 'codexbot'); f.ready(); await f.update(); + assert.match(String(f.state.comments[0]!.body), /Workflow: in_progress.*actions\/runs\/123/s); + assert.equal(await f.store.prepare(f.request.id, '123'), true); + await f.store.stage(f.request.id, '123', 'analyzing'); f.ready(); await f.update(); + assert.match(String(f.state.comments[0]!.body), /Stage: analyzing/); + await f.store.result(f.request.id, clean(), true, '123'); + const lease = await f.store.claimPublication(f.request.id, '123'); f.ready(); await f.update(); + assert.match(String(f.state.comments[0]!.body), /Stage: publishing/); + await f.store.published(f.request.id, 99, lease); f.state.runStatus = 'completed'; f.state.runConclusion = 'success'; f.ready(); await f.update(); + assert.match(String(f.state.comments[0]!.body), /Stage: completed.*Workflow: completed \/ success.*pullrequestreview-99/s); + assert.equal(f.state.comments.length, 1); assert.equal(await f.store.dueProgress(), null); + } finally { f.sqlite.close(); } +}); +await test('published progress still observes the real workflow until completion is confirmed', async () => { + const f = fixture(); + try { + await f.store.create(f.request); await dispatchRequest(f.github, f.store, f.request, 'codexbot'); await f.store.prepare(f.request.id, '123'); + await f.store.result(f.request.id, clean(), true, '123'); const lease = await f.store.claimPublication(f.request.id, '123'); await f.store.published(f.request.id, 99, lease); + await f.update(); f.ready(); assert.equal(await f.store.dueProgress(), f.request.id); + f.state.runStatus = 'completed'; f.state.runConclusion = 'success'; await f.update(); f.ready(); + assert.equal(await f.store.dueProgress(), null); assert.match(String(f.state.comments[0]!.body), /Workflow: completed \/ success/); + } finally { f.sqlite.close(); } +}); +await test('terminal progress with an unknown workflow status remains recoverable and observation failures never claim a stale status', async () => { + const f = fixture(); + const github = new Octokit({ request: { fetch: async (input: Parameters[0], init?: Parameters[1]) => + String(input).includes('/actions/runs/') ? new Response('{"message":"opaque-secret"}', { status: 503 }) : f.fetcher(input, init) + }, log: { debug() {}, info() {}, warn() {}, error() {} } }); + try { + await f.store.create(f.request); await dispatchRequest(f.github, f.store, f.request, 'codexbot'); + await f.store.prepare(f.request.id, '123'); await f.update(); + await f.store.failRun(f.request.id, '123', 'missing_environment_credentials', 'environment'); + f.sqlite.prepare('UPDATE requests SET workflow_status=NULL').run(); + for (let attempt = 0; attempt < 5; attempt++) { + f.ready(); assert.equal(await f.store.dueProgress(), f.request.id); await f.update(github); + assert.equal((await f.store.get(f.request.id)).row.progress_attempts, attempt + 1); + } + const body = String(f.state.comments[0]!.body); + assert.match(body, /Stage: failed/); assert.match(body, /no current run status is asserted/); + assert.ok(!body.includes('in_progress')); assert.ok(!body.includes('opaque-secret')); + assert.equal(f.state.comments.length, 1); f.ready(); assert.equal(await f.store.dueProgress(), null); + } finally { f.sqlite.close(); } +}); +await test('accepted comment POST with lost response reconciles its App-owned marker without another POST', async () => { + const f = fixture(); let lose = true; + const github = new Octokit({ request: { fetch: async (input: Parameters[0], init?: Parameters[1]) => { + const response = await f.fetcher(input, init); + if (String(input).endsWith('/issues/4/comments') && init?.method === 'POST' && lose) { lose = false; throw new Error('Lost response with fixture secret'); } + return response; + } }, log: { debug() {}, info() {}, warn() {}, error() {} } }); + try { + await f.store.create(f.request); await f.update(github); assert.equal(f.state.comments.length, 1); + assert.equal((await f.store.get(f.request.id)).row.progress_phase, 'creating'); + f.ready(); await f.update(github); assert.equal((await f.store.get(f.request.id)).row.progress_comment_id, 1000); + assert.equal(f.calls.filter(x => x.method === 'POST' && x.path.endsWith('/comments')).length, 1); + } finally { f.sqlite.close(); } +}); +await test('a crash before POST never guesses that an ambiguous creation is safe to resend', async () => { + const f = fixture(); + try { + await f.store.create(f.request); + const original = f.store.progressCreating.bind(f.store); + const intercepted = mock.method(f.store, 'progressCreating', async (...args: Parameters) => { await original(...args); throw new Error('D1 committed but response lost'); }); + await f.update(); intercepted.mock.restore(); + for (let attempt = 0; attempt < 7; attempt++) { f.ready(); await f.update(); } + assert.equal(f.state.comments.length, 0); assert.equal((await f.store.get(f.request.id)).row.progress_attempts, 5); + assert.equal(await f.store.prepare(f.request.id, '123'), true); // progress failure does not consume the review + } finally { f.sqlite.close(); } +}); +await test('unverifiable successful POST responses and HTTP timeouts are reconciled without another creation', async () => { + for (const status of [201, 408]) { + const f = fixture(); let first = true; + const github = new Octokit({ request: { fetch: async (input: Parameters[0], init?: Parameters[1]) => { + const response = await f.fetcher(input, init); + if (String(input).endsWith('/issues/4/comments') && init?.method === 'POST' && first) { + first = false; + if (status === 408) return new Response('{}', { status }); + const body = await response.json() as Record; delete body.performed_via_github_app; + return Response.json(body, { status }); + } + return response; + } }, log: { debug() {}, info() {}, warn() {}, error() {} } }); + try { + await f.store.create(f.request); await f.update(github); + assert.equal((await f.store.get(f.request.id)).row.progress_phase, 'creating'); + f.ready(); await f.update(github); assert.equal((await f.store.get(f.request.id)).row.progress_comment_id, 1000); + assert.equal(f.calls.filter(x => x.method === 'POST' && x.path.endsWith('/comments')).length, 1); + } finally { f.sqlite.close(); } + } +}); +await test('deleted comments stay deleted and foreign App/user/request/PR identities cannot be edited', async () => { + for (const change of ['deleted','user','app','pr','request','repository']) { + const f = fixture(); + try { + await f.store.create(f.request); await f.update(); + if (change === 'deleted') f.state.comments.length = 0; + if (change === 'user') f.state.comments[0]!.user = { login: 'alice', type: 'User' }; + if (change === 'app') f.state.comments[0]!.performed_via_github_app = { id: 999 }; + if (change === 'pr') f.state.comments[0]!.issue_url = 'https://api.github.com/repos/owner/repo/issues/99'; + if (change === 'request') f.state.comments[0]!.body = ''; + await f.store.prepare(f.request.id, '123'); f.ready(); + if (change === 'repository') await updateProgress(f.github, f.store, { ...f.request, repositoryId: 999 }, 'codexbot', 42); else await f.update(); + assert.equal(f.calls.filter(x => x.method === 'PATCH' && x.path.includes('/issues/comments/')).length, 0); + assert.equal(f.calls.filter(x => x.method === 'POST' && x.path.endsWith('/comments')).length, 1); + if (change === 'deleted') assert.equal((await f.store.get(f.request.id)).row.progress_phase, 'deleted'); + } finally { f.sqlite.close(); } + } +}); +await test('verified completed failed workflows finalize a never-prepared request and never display hidden context or errors', async () => { + const f = fixture(); + try { + f.request.command.instructions = 'opaque-secret hidden prompt'; + await f.store.create(f.request); await dispatchRequest(f.github, f.store, f.request, 'codexbot'); + f.state.runStatus = 'completed'; f.state.runConclusion = 'failure'; await f.update(); + const { row } = await f.store.get(f.request.id); assert.equal(row.state, 'failed'); assert.equal(row.run_id, '123'); + assert.equal(row.failure_code, 'workflow_ended_before_publication'); + const body = progressBody(f.request, { ...row, workflow_conclusion: '[secret](https://attacker.invalid)', failure_code: 'opaque-secret' }); + assert.match(body, /Stage: failed.*Workflow: completed/s); assert.ok(!body.includes('opaque-secret')); assert.ok(!body.includes('attacker')); + } finally { f.sqlite.close(); } +}); +await test('expired request cleanup releases its lease immediately and failed pre-publication jobs cannot bypass publisher authorization', async () => { + const f = fixture(); + try { + await f.store.create(f.request); await f.store.saveSession(2, 5, await seal({}, randomSecret(), 'fixture'), 1); await f.store.acquire(f.request); + f.sqlite.prepare('UPDATE requests SET created_at=0').run(); await f.store.cleanup(); + const session = f.sqlite.prepare('SELECT lease_until,lease_request FROM sessions').get()!; assert.equal(session.lease_until, 0); assert.equal(session.lease_request, null); + assert.equal(f.sqlite.prepare('SELECT count(*) AS n FROM requests').get()!.n, 0); // retention cleanup of the fixture epoch + const fresh = { ...f.request, id: crypto.randomUUID() }; await f.store.create(fresh); await f.store.prepare(fresh.id, '123'); await f.store.result(fresh.id, clean(), true, '123'); + await f.store.failRun(fresh.id, '123', 'workflow_ended_before_publication', 'workflow'); + await assert.rejects(f.store.operatorRetry(fresh.id, 2, 1), /retry_not_allowed/); + assert.equal((await f.store.get(fresh.id)).row.state, 'failed'); + } finally { f.sqlite.close(); } +}); +await test('a lost run-binding race cannot release the admitted runner session or mark it failed', async () => { + const f = fixture(); + try { + await f.store.create(f.request); await dispatchRequest(f.github, f.store, f.request, 'codexbot'); + await f.store.saveSession(2, 5, await seal({}, randomSecret(), 'fixture'), 1); + f.state.runStatus = 'completed'; f.state.runConclusion = 'failure'; + const original = f.store.observeRun.bind(f.store); + const intercepted = mock.method(f.store, 'observeRun', async (...args: Parameters) => { + await f.store.prepare(f.request.id, '456'); await f.store.acquire(f.request); return original(...args); + }); + await f.update(); intercepted.mock.restore(); + assert.equal((await f.store.get(f.request.id)).row.run_id, '456'); assert.equal((await f.store.get(f.request.id)).row.state, 'running'); + assert.equal(await f.store.failRun(f.request.id, '123', 'missing_environment_credentials', 'environment'), false); + await assert.rejects(f.store.acquire({ ...f.request, id: crypto.randomUUID() })); + } finally { f.sqlite.close(); } +}); +await test('operator progress retry is tenant-bound, bounded and never resets an ambiguous POST to a new creation', async () => { + const f = fixture(); + try { + await f.store.create(f.request); + f.sqlite.prepare("UPDATE requests SET progress_phase='creating',progress_attempts=5").run(); + await assert.rejects(f.store.operatorRetryProgress(f.request.id, 2, 999)); + await f.store.operatorRetryProgress(f.request.id, 2, 1); await f.update(); + assert.equal(f.state.comments.length, 0); assert.equal((await f.store.get(f.request.id)).row.progress_phase, 'creating'); + f.sqlite.prepare('UPDATE requests SET progress_attempts=5,progress_next_at=0').run(); + await f.store.operatorRetryProgress(f.request.id, 2, 1); + f.sqlite.prepare('UPDATE requests SET progress_attempts=5').run(); + await assert.rejects(f.store.operatorRetryProgress(f.request.id, 2, 1)); + assert.equal(f.calls.filter(x => x.method === 'POST' && x.path.endsWith('/comments')).length, 0); + } finally { f.sqlite.close(); } +}); diff --git a/tests/setup.test.ts b/tests/setup.test.ts index 93d87cc..74a0331 100644 --- a/tests/setup.test.ts +++ b/tests/setup.test.ts @@ -7,7 +7,8 @@ import { RELEASE } from './fixtures.js'; await test('generated workflow pins reusable code, has minimal permissions and never shares secrets', () => { const yaml = callerWorkflow('operator/codexbot', RELEASE, 'https://self.invalid'); const workflow = parse(yaml) as { permissions: unknown; jobs: { review: { uses: string; secrets?: unknown; permissions: unknown; with: { backend: string } } } }; - assert.equal(workflow.jobs.review.uses, `operator/codexbot/.github/workflows/review.yml@${RELEASE}`); assert.equal(workflow.jobs.review.secrets, undefined); + assert.equal(workflow.jobs.review.uses, `operator/codexbot/.github/workflows/review.yml@${RELEASE}`); + assert.deepEqual(workflow.jobs.review.secrets, { CODEXBOT_VAULT_KEY: '', CODEXBOT_API_KEY: '' }); assert.deepEqual(workflow.permissions, {}); assert.equal(workflow.jobs.review.with.backend, 'https://self.invalid'); assert.match(yaml, /dispatch_attempt/); assert.throws(() => callerWorkflow('operator/codexbot', 'main', 'https://self.invalid')); }); @@ -47,7 +48,7 @@ await test('automatic setup preserves existing workflows and creates only a dedi await assert.rejects(setup()); assert.equal(f.writes.length, 0); f.setExisting(false); assert.equal(await setup(), 'https://github.com/owner/repo/pull/1'); const tree = f.writes.find(x => x.path.endsWith('/git/trees'))!.body.tree as unknown[]; assert.equal(tree.length, 2); - assert.ok(JSON.stringify(tree).includes('@CodexBotReview2')); + assert.ok(JSON.stringify(tree).includes('@CBReview2')); assert.equal(f.writes.filter(x => x.path.endsWith('/git/refs')).length, 1); assert.equal(f.writes.some(x => x.body.force === true), false); assert.equal(await setup(), 'https://github.com/owner/repo/pull/1'); assert.equal(f.writes.filter(x => x.path.endsWith('/pulls')).length, 1); }); diff --git a/tests/worker.test.ts b/tests/worker.test.ts index ee0709a..0a5505e 100644 --- a/tests/worker.test.ts +++ b/tests/worker.test.ts @@ -12,7 +12,7 @@ import { seal, randomSecret } from '../src/core/crypto.js'; const keys = await generateKeyPair('RS256', { extractable: true }); const pem = await exportPKCS8(keys.privateKey), jwk = { ...await exportJWK(keys.publicKey), kid: 'fixture', use: 'sig', alg: 'RS256' }; async function oidc(environment: string, overrides: Record = {}): Promise { - return new SignJWT({ repository: 'owner/repo', repository_id: '2', repository_owner_id: '3', event_name: 'workflow_dispatch', ref: 'refs/heads/main', ref_type: 'branch', workflow_ref: 'owner/repo/.github/workflows/codexbot.yml@refs/heads/main', workflow_sha: WORKFLOW, + return new SignJWT({ repository: 'owner/repo', repository_id: '2', repository_owner_id: '3', actor: 'alice', actor_id: '5', event_name: 'workflow_dispatch', ref: 'refs/heads/main', ref_type: 'branch', workflow_ref: 'owner/repo/.github/workflows/codexbot.yml@refs/heads/main', workflow_sha: WORKFLOW, job_workflow_ref: `operator/codexbot/.github/workflows/review.yml@${RELEASE}`, job_workflow_sha: RELEASE, runner_environment: 'github-hosted', environment, sub: `repo:owner/repo:environment:${environment}`, run_id: '123', run_attempt: '1', jti: crypto.randomUUID(), ...overrides }) .setProtectedHeader({ alg: 'RS256', kid: 'fixture' }).setIssuer('https://token.actions.githubusercontent.com').setAudience('https://broker.invalid').setIssuedAt().setNotBefore('0s').setExpirationTime('5m').sign(keys.privateKey); } @@ -46,6 +46,45 @@ await test('signed webhook, OIDC preflight, committed result and separate public assert.equal((await post('publish', 'codexbot-publish-1')).status, 200); assert.equal(f.state.reviews.length, 1); for (const issued of f.calls.filter(x => x.path.endsWith('/access_tokens'))) assert.deepEqual(issued.body.repository_ids, [2]); })); +await test('manual administrator OIDC diagnostic is read-only and never authorizes the privileged review broker', async () => fixture(async (env, f) => { + f.state.permission = 'admin'; f.state.runActor = { id: 5, type: 'User', login: 'alice' }; + const check = async (overrides: Record = {}) => worker.fetch(new Request('https://broker.invalid/v1/workload/check', { method: 'POST', headers: { Authorization: `Bearer ${await oidc('codexbot-1-5', overrides)}` } }), env); + const response = await check(); assert.equal(response.status, 200); assert.deepEqual(await response.json(), { status: 'verified', scope: 'diagnostic_only' }); + for (const call of f.calls.filter(x => x.path.endsWith('/access_tokens'))) assert.deepEqual(call.body.permissions, { contents: 'read', pull_requests: 'read', actions: 'read' }); + assert.equal((await env.DB.prepare('SELECT count(*) AS n FROM requests').first<{ n: number }>())!.n, 0); + assert.equal((await check({ job_workflow_sha: HEAD })).status, 403); + assert.equal((await check({ actor_id: '6' })).status, 403); + f.state.permission = 'write'; assert.equal((await check()).status, 403); f.state.permission = 'admin'; + await worker.fetch(signed(payload(), env.GITHUB_WEBHOOK_SECRET), env); + const id = (f.calls.find(x => x.path.endsWith('/dispatches'))!.body.inputs as Record).request_id; + const admitted = await worker.fetch(new Request(`https://broker.invalid/v1/requests/${id}/prepare`, { method: 'POST', headers: { Authorization: `Bearer ${await oidc('codexbot-1-5')}` }, body: '{}' }), env); + assert.equal(admitted.status, 403); assert.equal((await admitted.json() as Record).error, 'workflow_not_dispatched_by_this_app'); +})); +await test('verified runner failure finalizes an unprepared request; foreign Environment or arbitrary failure text is denied', async () => fixture(async (env, f) => { + await worker.fetch(signed(payload(), env.GITHUB_WEBHOOK_SECRET), env); + const id = (f.calls.find(x => x.path.endsWith('/dispatches'))!.body.inputs as Record).request_id!; + const post = async (environment: string, body: unknown) => worker.fetch(new Request(`https://broker.invalid/v1/requests/${id}/failure`, { method: 'POST', headers: { Authorization: `Bearer ${await oidc(environment)}` }, body: JSON.stringify(body) }), env); + assert.equal((await post('codexbot-1-6', { stage: 'environment', code: 'missing_environment_credentials' })).status, 403); + assert.equal((await post('codexbot-1-5', { stage: 'environment', code: 'opaque-sensitive-provider-error' })).status, 400); + assert.equal((await post('codexbot-1-5', { stage: 'environment', code: 'missing_environment_credentials' })).status, 200); + const { row } = await new Store(env.DB).get(id); assert.equal(row.state, 'failed'); assert.equal(row.failure_code, 'missing_environment_credentials'); assert.equal(row.run_id, '123'); + assert.equal(f.state.reviews.length, 0); +})); +await test('analysis progress is stage/run bound and failed review jobs can only publish a non-approving report', async () => fixture(async (env, f) => { + await worker.fetch(signed(payload(), env.GITHUB_WEBHOOK_SECRET), env); + const id = (f.calls.find(x => x.path.endsWith('/dispatches'))!.body.inputs as Record).request_id!; + const post = async (operation: string, stage: string, body: unknown = {}) => worker.fetch(new Request(`https://broker.invalid/v1/requests/${id}/${operation}`, { method: 'POST', headers: { Authorization: `Bearer ${await oidc(stage)}` }, body: JSON.stringify(body) }), env); + assert.equal((await post('stage', 'codexbot-1-5', { stage: 'analyzing' })).status, 409); + assert.equal((await post('prepare', 'codexbot-1-5')).status, 200); + assert.equal((await post('stage', 'codexbot-1-5', { stage: 'completed' })).status, 400); + assert.equal((await post('stage', 'codexbot-1-5', { stage: 'analyzing' })).status, 200); + assert.equal((await post('result', 'codexbot-1-5', { output: clean(), context_complete: true, failure: { stage: 'analyzing', code: 'isolated_review_failed' } })).status, 400); + assert.equal((await new Store(env.DB).get(id)).row.result, null); + assert.equal((await post('result', 'codexbot-1-5', { output: clean(), context_complete: true })).status, 200); + const response = await post('publish', 'codexbot-publish-1', { review_job_failed: true }); assert.equal(response.status, 200); + assert.equal((await response.json() as Record).event, 'COMMENT'); + assert.equal((await new Store(env.DB).get(id)).row.context_complete, 0); +})); await test('invalid signature, replay, spoofed user, forged installation and unauthorized merge cannot dispatch', async () => fixture(async (env, f) => { assert.equal((await worker.fetch(signed(payload(), 'wrong-secret'), env)).status, 401); const valid = payload(); assert.equal((await worker.fetch(signed(valid, env.GITHUB_WEBHOOK_SECRET), env)).status, 202); diff --git a/tests/workers-runtime.mjs b/tests/workers-runtime.mjs index f9e9efd..3167bab 100644 --- a/tests/workers-runtime.mjs +++ b/tests/workers-runtime.mjs @@ -38,8 +38,16 @@ try { assert.equal((await post(signature)).status, 200); assert.equal((await post(signature)).status, 200); const stored = cli(['d1', 'execute', 'DB', '--local', '--persist-to', state, '--json', '--command', 'SELECT phase,payload FROM deliveries']); const rows = JSON.parse(stored); assert.deepEqual(rows[0].results, [{ phase: 'done', payload: null }]); + const request = randomUUID(), now = Date.now(); + cli(['d1', 'execute', 'DB', '--local', '--persist-to', state, '--command', + `INSERT INTO requests(id,installation_id,repository_id,actor_id,snapshot,state,created_at,progress_phase) VALUES('${request}',1,2,5,'{}','running',${now - 7_200_000},'disabled'); + INSERT INTO sessions(installation_id,repository_id,actor_id,envelope,lease_request,lease_until) VALUES(1,2,5,'{}','${request}',${now + 3_600_000}),(9,2,5,'{}','other-installation',${now + 3_600_000});`]); assert.equal((await globalThis.fetch(`${origin}/cdn-cgi/handler/scheduled`)).status, 200); - process.stdout.write('Real workerd + D1: migrations, HMAC rejection, durable receipt, deduplication and cron passed.\n'); + const terminal = JSON.parse(cli(['d1', 'execute', 'DB', '--local', '--persist-to', state, '--json', '--command', `SELECT state,failure_code FROM requests WHERE id='${request}'`]))[0].results; + assert.deepEqual(terminal, [{ state: 'failed', failure_code: 'request_expired' }]); + const leases = JSON.parse(cli(['d1', 'execute', 'DB', '--local', '--persist-to', state, '--json', '--command', 'SELECT installation_id,lease_request FROM sessions ORDER BY installation_id']))[0].results; + assert.deepEqual(leases, [{ installation_id: 1, lease_request: null }, { installation_id: 9, lease_request: 'other-installation' }]); + process.stdout.write('Real workerd + D1: migrations, HMAC, receipts, cron, expired requests and cross-installation lease cleanup passed.\n'); } finally { if (child?.pid) { if (process.platform === 'win32') execFileSync('taskkill', ['/PID', String(child.pid), '/T', '/F'], { stdio: 'ignore' }); diff --git a/tests/workflows.test.ts b/tests/workflows.test.ts index b1f8b31..c2803da 100644 --- a/tests/workflows.test.ts +++ b/tests/workflows.test.ts @@ -3,6 +3,9 @@ import assert from 'node:assert/strict'; import { readFile } from 'node:fs/promises'; import { parse } from 'yaml'; import { record } from '../src/core/errors.js'; +import { spawnSync } from 'node:child_process'; +import { callerWorkflow } from '../src/setup.js'; +import { RELEASE } from './fixtures.js'; await test('reusable workflow has no GitHub write permissions, shared secrets or PR-head checkout', async () => { const text = await readFile('.github/workflows/review.yml', 'utf8'), workflow = record(parse(text)); assert.deepEqual(workflow.permissions, {}); @@ -21,8 +24,40 @@ await test('reusable workflow has no GitHub write permissions, shared secrets or assert.ok(!text.includes('pull_request.head')); assert.ok(!text.includes('secrets: inherit')); assert.ok(!text.includes('upload-artifact')); assert.equal(record(record(jobs.publish).permissions).contents, 'read'); }); +await test('Environment secret grants are explicit and empty, with no repository-secret fallback or blanket inheritance', async () => { + const workflow = record(parse(await readFile('.github/workflows/review.yml', 'utf8'))); + const contract = record(record(workflow.on).workflow_call); + assert.deepEqual(Object.keys(record(contract.secrets)).sort(), ['CODEXBOT_API_KEY','CODEXBOT_VAULT_KEY']); + const caller = record(parse(callerWorkflow('operator/codexbot', RELEASE, 'https://self.invalid'))); + assert.deepEqual(record(record(caller.jobs).review).secrets, { CODEXBOT_VAULT_KEY: '', CODEXBOT_API_KEY: '' }); + const steps = record(record(workflow.jobs).review).steps as Record[]; + const check = String(steps[0]!.run).split("<<'JS'\n")[1]!.replace(/\nJS\s*$/, ''); + for (const [vault, api, status] of [['','',1],['opaque-vault','',0],['','opaque-api',0],['opaque-vault','opaque-api',1]] as const) { + const env = Object.assign(Object.create(null) as NodeJS.ProcessEnv, { CODEXBOT_VAULT_KEY: vault, CODEXBOT_API_KEY: api }); + const result = spawnSync(process.execPath, ['--input-type=module','-e',check], { env, encoding: 'utf8' }); + assert.equal(result.status, status, result.stderr); assert.ok(!result.stdout.includes('opaque-')); assert.ok(!result.stderr.includes('opaque-')); + } +}); +await test('cached builds are opt-in, source/input scoped, pinned and loaded only from the trusted checkout; execution uses a content ID', async () => { + const workflow = record(parse(await readFile('.github/workflows/review.yml', 'utf8'))), jobs = record(workflow.jobs); + assert.equal(record(record(record(record(workflow.on).workflow_call).inputs).docker_cache).default, false); + const steps = record(jobs.review).steps as Record[]; + const build = steps.find(x => String(x.uses).startsWith('docker/build-push-action@'))!, options = record(build.with); + assert.match(String(build.if), /inputs.docker_cache/); assert.equal(options.context, '.'); assert.equal(options.load, true); assert.equal(options.push, false); + for (const key of ['cache-from','cache-to']) { + assert.match(String(options[key]), /needs.source.outputs.ref/); assert.match(String(options[key]), /hashFiles\('Dockerfile', 'package.json', 'package-lock.json', '.dockerignore'\)/); + assert.ok(!String(options[key]).includes('pull_request')); assert.ok(!String(options[key]).includes('inputs.request_id')); + } + assert.equal(options.secrets, undefined); assert.equal(options['build-args'], undefined); assert.equal(build['continue-on-error'], undefined); + const builder = steps.find(x => String(x.uses).startsWith('docker/setup-buildx-action@'))!; + assert.match(String(record(builder.with)['driver-opts']), /^image=moby\/buildkit@sha256:[a-f0-9]{64}$/); + const execute = steps.find(x => String(x.run).includes('main.js review'))!; + assert.equal(record(execute.env).CODEXBOT_IMAGE, '${{ steps.image.outputs.id }}'); + const publish = record(jobs.publish); assert.match(String(publish.if), /needs.review.result == 'failure'/); + assert.match(String(publish.if), /!inputs.credential_check_only/); +}); await test('documentation local links resolve and Apache license is canonical', async () => { - const documents = ['README.md', 'CHANGELOG.md', 'docs/architecture.md', 'docs/authentication.md', 'docs/configuration.md', 'docs/deployment.md', 'docs/development.md', 'docs/installation.md', 'docs/security.md', 'docs/validation.md', 'docs/operations.md', 'docs/acceptance.md', 'docs/remediation.md', 'SECURITY.md', 'CONTRIBUTING.md']; + const documents = ['README.md', 'CHANGELOG.md', 'docs/architecture.md', 'docs/authentication.md', 'docs/configuration.md', 'docs/deployment.md', 'docs/development.md', 'docs/installation.md', 'docs/security.md', 'docs/validation.md', 'docs/review-rollout.md', 'docs/operations.md', 'docs/acceptance.md', 'docs/remediation.md', 'SECURITY.md', 'CONTRIBUTING.md']; for (const path of documents) { const text = await readFile(path, 'utf8'); for (const match of text.matchAll(/\]\(([^)]+)\)/g)) { diff --git a/wrangler.jsonc b/wrangler.jsonc index 33b9d0f..d349a79 100644 --- a/wrangler.jsonc +++ b/wrangler.jsonc @@ -50,7 +50,8 @@ ], "triggers": { "crons": [ - "* * * * *" + "* * * * *", + "*/2 * * * *" ] } } From ff57f832a9ef0307554cfc952f7273baf536962b Mon Sep 17 00:00:00 2001 From: AppSolves Date: Sat, 14 Nov 2026 08:31:48 +0100 Subject: [PATCH 2/2] test: retain safe external HTTP status and record cache measurements --- docs/review-rollout.md | 2 ++ docs/validation.md | 2 +- src/core/diagnostics.ts | 2 +- tests/broker.test.ts | 7 +++++++ 4 files changed, 11 insertions(+), 2 deletions(-) diff --git a/docs/review-rollout.md b/docs/review-rollout.md index b9005f4..584cc6c 100644 --- a/docs/review-rollout.md +++ b/docs/review-rollout.md @@ -36,6 +36,8 @@ This shares the existing bounded operator retry budget, cannot reset an ambiguou Run the manually dispatched `Trusted image cache benchmark` workflow, or CI's `cache_benchmark: true` option. It compares ordinary, cold-cached and restored builds using a fresh second builder, including setup, load and export. Its scope is separate from production caches, and it uses no credential Environment or provider. Same-host reuse of the builder image is optimistic: repeat on independent runners and include network/storage/initial cache cost. Enable `with: {docker_cache: true}` in a repository caller only when repeated measurements show a meaningful net saving; otherwise retain the ordinary build. No release-image registry is introduced. +The first [GitHub-hosted benchmark](https://github.com/AppSolves/codexbot/actions/runs/43571256477) measured **14 seconds ordinary / 49 seconds cold cache / 14 seconds restored cache** on 2026-11-14. This showed no net saving even with optimistic same-host builder-image reuse. The default therefore remains off. This is one measurement, not a general performance guarantee. + ## Trigger isolation Set `.codexbot.yml` to `bot: {trigger: "@CBReview", aliases: []}` or use the generated `@CBReview`. Matching is exact and case-insensitive; extra suffixes, quoted examples, inline code and fences do not invoke CodexBot. Legacy triggers remain supported when explicitly configured or inherited from the legacy default. They do not rename the GitHub App. diff --git a/docs/validation.md b/docs/validation.md index 56a3e56..b132034 100644 --- a/docs/validation.md +++ b/docs/validation.md @@ -10,7 +10,7 @@ The per-finding implementation/evidence matrix is maintained in [remediation](re ## Review reliability validation — 2026-11-14 -`npm run check` passed strict typecheck, lint, **123 tests** and build. `npm run test:clean`, `npm run test:workers`, actionlint, `docker build --tag codexbot-review:local .`, `npm run test:sandbox`, `npm audit --audit-level=moderate`, Wrangler deploy dry-run and `git diff --check` passed. Actual workerd/D1 coverage includes migration 0003, expired-request cleanup and installation-isolated session release. The strict image scan below reported zero HIGH/CRITICAL findings; a source-only secret scan reported no findings. No real model inference was used. Remote acceptance and cache timings must be recorded separately after execution; local green checks do not establish them. +`npm run check` passed strict typecheck, lint, **124 tests** and build. `npm run test:clean`, `npm run test:workers`, actionlint, `docker build --tag codexbot-review:local .`, `npm run test:sandbox`, `npm audit --audit-level=moderate`, Wrangler deploy dry-run and `git diff --check` passed. Actual workerd/D1 coverage includes migration 0003, expired-request cleanup and installation-isolated session release. The strict image scan below reported zero HIGH/CRITICAL findings; a source-only secret scan reported no findings. No real model inference was used. Remote acceptance and cache timings must be recorded separately after execution; local green checks do not establish them. ## Initial source-only audit snapshot — 2026-11-14 diff --git a/src/core/diagnostics.ts b/src/core/diagnostics.ts index 5e07ce8..4556b94 100644 --- a/src/core/diagnostics.ts +++ b/src/core/diagnostics.ts @@ -41,6 +41,6 @@ export function diagnostic(error: unknown, stage: string): { event: string; stag cause = 'cause' in cause ? cause.cause : null; } } - if (error instanceof AppError && Number.isInteger(error.status) && error.status >= 400 && error.status <= 599) result.status = error.status; + if (error && typeof error === 'object' && 'status' in error && typeof error.status === 'number' && Number.isInteger(error.status) && error.status >= 400 && error.status <= 599) result.status = error.status; return result; } diff --git a/tests/broker.test.ts b/tests/broker.test.ts index 0ef592e..abc9e51 100644 --- a/tests/broker.test.ts +++ b/tests/broker.test.ts @@ -6,6 +6,13 @@ import { reviewJob } from '../src/runner/main.js'; import { snapshot } from './fixtures.js'; const id = snapshot().id, secret = 'opaque-oauth-code fixture-access-token Authorization: Bearer fixture-secret'; +await test('external HTTP and Node failures retain safe status/errno without request or exception text', () => { + const http = Object.assign(new Error(secret), { status: 403, request: { headers: { authorization: secret } }, response: { data: secret } }); + assert.deepEqual(diagnostic(http, 'context'), { event: 'review_failed', stage: 'context', code: 'external_or_execution_failure', status: 403 }); + const network = new Error(secret, { cause: Object.assign(new Error(secret), { code: 'ECONNRESET' }) }); + assert.equal(diagnostic(network, 'prepare').code, 'ECONNRESET'); + assert.ok(!JSON.stringify(diagnostic({ status: secret, code: secret }, 'prepare')).includes(secret)); +}); async function fixture(action: (calls: string[], logs: unknown[][]) => Promise, response: () => Response = () => Response.json({ error: 'oidc_environment_mismatch', message: secret }, { status: 403 })) { const env = { ...process.env }, calls: string[] = [], logs: unknown[][] = []; process.env.ACTIONS_ID_TOKEN_REQUEST_URL = 'https://oidc.invalid/token'; process.env.ACTIONS_ID_TOKEN_REQUEST_TOKEN = secret;