Check Terraform pull requests for station regressions

Use this when your team changes its lines through pull requests to a Terraform repository, and you want each pull request that changes a work station to be tried on the station's past tasks before it merges. A CI job runs spotlight replay check on the saved plan. For each work station the plan changes, Spotlight replays the station's replay set of past tasks under the planned change, compares the results with the station as it is now, and the job fails if the change did worse on more tasks than the line allows. You need:

  • The line's Terraform planned in GitHub Actions. The workflow below builds on Run Terraform from GitHub Actions without a stored token.
  • For each station to check, at least one ended run. Spotlight keeps a replay set for each station from its runs.
  • A host serving the workspace with the station's harness. Checks run on the team's runners, after task runs.
  • A way for the job to call Spotlight with the plan scope: a OIDC trust the job's OIDC token is exchanged through, or a workspace token.

A check replays the change as the pull request plans it, including the line's repositories, their runner image and setup command. Anyone whose pull request runs the check can have the team's runners pull an image and run a command of their choosing. Accept only jobs you trust: scope the trust's subject to your own repository's pull requests or branches, never run the check on pull_request_target or on pull requests from forks, and limit the images your hosts run with SPOTLIGHT_RUNNER_IMAGE. See Host settings.

Check the tasks each check replays

A check of a station replays the replay set that Spotlight keeps for it. The set holds up to 30 of the station's recent runs, one for each task, and Spotlight refreshes it from new runs. A station with no set yet, or with no runs in its set, has nothing to replay, so its check is undecided.

Nobody chooses the runs by hand. A task that a search of the station found to be the cause of its own failure, such as a task that cannot be done as written, leaves the set at its next refresh. See The replay set Spotlight keeps.

While a check is running, it keeps replaying the set as it was when the check started. A later change to the set makes the next check start afresh.

Set the threshold on the line

Add a replay_check block to the line's spotlight_line resource, and apply it:

resource "spotlight_line" "core_delivery" {
  # ...

  replay_check {
    max_regressions = 0
    ceiling         = 10
    currency        = "USD"
    min_gain        = 0.05
  }
}
  • max_regressions: how many tasks a change may regress on and still pass. A task regresses when the change is clearly worse on it than the live config, after the noise of both sides, or when the station's judge clearly scored the change lower. A task that is flaky on the live config does not regress from one failed repeat or one lower score. See Regressions.
  • ceiling and currency: the most each replay a check starts may spend. A check replays the change, and also replays the live config three times when the set has no baseline for it yet. The baseline's ceiling is scaled to its repeats. A check that reaches its ceiling before it fails is undecided. See Ceiling.
  • min_gain: the smallest gain in success that counts as more than noise, as a share above 0 and at most 1, 0.05 unless set. The output says whether the change's gain over the live config reaches it. It does not decide whether the check passes. The line's replays and searches read their gains against it too.

A line without replay_check is not checked: the job fails for its stations and says so. The threshold always comes from the line as it is applied, never from the plan, so a pull request cannot loosen its own check. In the same way, a change to a station's check, judge, diffs or runners is replayed with the live station's, and the output says which it pinned. See replay_check.

Let the job call Spotlight

Use a trust that matches the job and grants plan, such as the github-pull-requests trust from Create the trusts. Give the job its id as SPOTLIGHT_OIDC_TRUST, and permissions: id-token: write. The command exchanges the job's OIDC token for a short-lived workspace token, and exchanges it again before it expires.

Give the job SPOTLIGHT_URL as well: https://app.usespotlight.ai, or your deployment's address on a self-hosted Spotlight.

Add the check to the workflow

spotlight replay check reads a saved plan as terraform show -json prints it. It has no install of its own apart from a host, so the job downloads the release binary.

Replace the plan job of .github/workflows/terraform.yml with:

  plan:
    if: github.event_name == 'pull_request'
    runs-on: ubuntu-latest
    env:
      SPOTLIGHT_OIDC_TRUST: ${{ vars.SPOTLIGHT_PLAN_TRUST }}
    steps:
      - uses: actions/checkout@v4
      - uses: hashicorp/setup-terraform@v3
        with:
          terraform_wrapper: false
      - run: terraform init -input=false
      - run: terraform plan -input=false -out=tfplan
      - run: terraform show -json tfplan > plan.json
      - name: Download spotlight
        run: |
          base=https://github.com/usespotlightai/spotlight/releases/latest/download
          curl -fsSL -o spotlight "$base/spotlight-linux-amd64"
          curl -fsSL -o spotlight.sha256 "$base/spotlight-linux-amd64.sha256"
          echo "$(cut -d' ' -f1 spotlight.sha256)  spotlight" | sha256sum -c -
          chmod +x spotlight
      - run: >-
          ./spotlight replay check
          --pull-request ${{ github.event.pull_request.number }}
          --commit ${{ github.event.pull_request.head.sha }}
          plan.json

terraform_wrapper: false keeps the JSON free of the wrapper's own output. Give the plan from terraform show -json: the log that terraform plan -json streams contains no station blocks, and the command rejects it. To pin a release, replace latest/download with download/<version>. See Install.

To replay each run of the set more than once, add --repeats and a number up to 5. Each repeat is a run, and the line's ceiling covers them all, so raise it to match. See Repeats of the same run.

The command waits for the checks, up to 60 minutes unless you pass --timeout. A check still running then keeps running in Spotlight, and the job ends with exit code 3.

Read the result

Exit codeMeaning
0Every changed station passed, or the plan changes nothing a check replays.
1A check could not start or failed to run, such as for a line with no replay_check.
2A change regressed on more tasks than its line allows.
3A check is undecided, such as for a station with no replay set yet or no runs in it, or still running at the timeout.

The job's summary gets a table with one row per station: its result, how many tasks regressed, how the change and the live config did, each with its range, the change's gain over the live config with better, worse or within noise, and what the check spent. Under it, each station has a table of its tasks, as Task, Live config, Candidate and Regressed, with each side's result and a one-line reason, such as Failed: Checks failed. Above that table, a station gets a line that compares the mean run of the change with the live config's, by cost, tool cost, harness tokens and wall time, and a line for each hook its replays did not run, as in hook ci-context did not run in replays: <reason>. The log prints the same.

Some changes are not checked, and the output notes why: a new line, a line that is removed, a station that is added or removed, or a station where nothing runs. A station is undecided, exit code 3, when its line is replaced, when the plan removes a work station and adds another (which may be a renamed station), when Terraform knows part of it only after apply, or when the plan also changes something the station depends on. A replay uses only what is live, so it cannot replay a changed skill, harness profile, integration, hook or secret before apply. A run.starting hook is the one exception. When the plan changes, adds or removes a hook that is on run.starting and whose steps all call read-only tools, as live and as planned, the check replays the change. The change's replays run the hook as planned, and the live config's replays run it as live. The check sends Spotlight the keys of the skills, harness profiles, integrations and hooks the plan changes for the station's team, the lines and stations each changed hook matches as planned, each changed hook's and integration's planned definition, and the names of the secrets the plan changes. Spotlight finds which of them the station depends on:

  • the harness profile the station names
  • a skill the station names
  • an integration whose tools the station's runs get, through the station, a skill it names or a mark
  • a hook that may fire at the station, as it is live or as planned, other than a run.starting hook the check can replay
  • an integration that such a hook calls
  • an integration that, as planned, enables tools on marks, since those reach every station of the team
  • a secret that one of these uses

When Terraform knows the key, name or team of a changed resource, the match of a changed hook or the definition of a changed integration only after apply, the check cannot send it, so every work station it may reach is undecided. The check also does not send a changed integration's planned definition when it holds sensitive values or is larger than 16 KiB as JSON, so every work station of the team is undecided. The check does not send a changed hook when Terraform knows part of its planned definition only after apply, when the definition holds sensitive values, when it is larger than 16 KiB as JSON, or when it sets attributes that the CLI does not read, steps included. Then every work station of the team is undecided.

Running the check again on the same change, against the same live config and the same live run.starting hooks at the station, the same runs of the set, the same threshold and the same scoring, answers with the earlier check while it is running, passed or failed. Results lists every result and why a check can be undecided.

On this page