Cut Your CI Bill: Let Your Agent Pick the Tests (Early Preview)

    Your agent changed one function, so why should CI run 160 blocks to check it? In an early preview, Semaphore lets the agent tell CI exactly which tests to run, and runs only those.

    Agents now push most code changes, and many teams run software factories with autonomous agents on top of that. Every one of those changes has to go through CI to catch regressions, so CI bills and run times keep climbing. This preview is how we’re tackling that.

    Early Preview: Selective Test Runs

    The agent picks the tests

    The agent that made a change already has the context: it knows the codebase, the ticket or plan behind the change, and what it touched. That makes it well placed to work out which tests the change needs. With selective test runs, the agent passes that list to Semaphore, and Semaphore runs CI the usual way, with all its setup and best practices, but with a right-sized test suite.

    Some of this was already possible with existing Semaphore features. The preview turns it into a product, so it’s more straightforward to set up.

    How it works

    There’s a new sem-ai test run command. The agent calls it with the list of tests to run. Together with a new configuration file in your repository, that’s all Semaphore needs. In the initialization job, Semaphore modifies the pipeline YAML so that only the blocks needed for those tests start, and only the selected tests run. Promotions and anything else a selective run doesn’t need are removed, and the run’s name changes to show that it’s a selective run.

    Coding agent pushing its change and running the three affected test files in CI with sem-ai test run

    The new file, a selective YAML in your .semaphore directory, maps tests to the jobs that need to run for them. It also holds other settings, such as the name of the environment variable the selected tests are passed in.

    Selective YAML file mapping test files to the CI blocks and jobs that run them

    What it looks like in practice

    In the demo, on a fork of Semaphore’s own monorepo, changing a single service normally starts a pipeline with many blocks and tests split across parallel jobs. That’s already narrowed down with change detection, so only the changed service is tested. A selective run goes further: it started 2 of 160 blocks, the block that builds the test image and the block with the tests, and finished in about two minutes instead of six.

    Full Semaphore pipeline for one service, with QA, integration and deployment blocks and five promotions
    Selective run in Semaphore that starts only the test image block and the QA block

    The demo then asked a coding agent to fix a real Semaphore bug: an unclear error message when an organization hasn’t configured a machine type for initialization jobs. The agent found the code, updated the message, edited the tests, pushed the change to a separate branch, and ran just the three affected tests in CI with the new command.

    Better than running tests locally

    Much production software isn’t easy to test on your own machine. Running a single integration test can mean building and starting several services first. Your CI pipeline already codifies all of that setup, so a selective run can narrow things down to just the integration test you need, as one job, without you remembering the prerequisites. It also doesn’t use your computer’s resources, so you can keep working with another agent while it runs.

    Selective run narrowed down to a single integration test job and its prerequisite blocks

    In one example from the demo, a selective run cut four jobs of around five minutes each, about 20 minutes in total, down to one job of around four minutes. Teams we’ve worked with on this kind of optimization typically save between 60 and 80 percent.

    Still get a full run before merging

    To make sure a selection never skips a test you need, you can still run the full suite once. Configure your project to run CI on the main branch and on pull requests, but not on draft pull requests. During the rapid back-and-forth phase, no full CI runs start automatically and the agent runs only the tests it needs. When you mark the pull request ready for review, the full suite runs and you get full confidence in the change.

    Semaphore project settings running CI on the main branch and on pull requests, with draft pull requests turned off

    What’s Coming

    Selective test runs are an early preview, and we’re still polishing them. A new sem-ai command to help generate the selective YAML file is on the way, along with support for the different ways languages and test runners select and start tests. We’ll share an update when the feature becomes generally available, and we’d love your feedback in the meantime.

    Want to discuss this article? Join our Discord.

    Pete Miloravac
    Writen by:
    Pete Miloravac is a software engineer and educator at Semaphore. He writes about CI/CD best practices, test automation, reproducible builds, and practical ways to help teams ship software faster and more reliably.
    Star us on GitHub