Before you begin
- Create an API key for the agent. Reading tests needs no permissions. To have it link or create tickets, give it the Create tickets permission.
- Turn on CI Intelligence for the repository, at Settings → Repositories, with the Trunk CI GitHub App installed. It provides the failure output in step 2, and keeps logs for 30 days. Without it, have the agent open the run’s
buildUrlinstead.
1. Find the tests to fix
Start the agent in one of two ways:- From a webhook. Subscribe the agent’s endpoint to the
v2.test_case.status_changedevent and act whennew_statusisFLAKY. Passtest_case.idto the calls below. - On a schedule. Call
GET /v2/testswithstatus=flaky,newlyFlaky=trueand aperiod, narrowed withtestCollectionIdorownerif you like.
2. Gather context
3. Fix and track
- Have the agent open a pull request that fixes the root cause, not one that raises a timeout or loosens an assertion.
- To track the work, link the pull request’s ticket with
POST /v2/tests/{testId}/ticket, or create one withPOST /v2/tests/{testId}/create-ticket.
Related
- Route Flaky Tests to Their Owners. File the ticket and tell the owning team, before the agent picks the test up.
- Tighten Test Timeouts. Have the agent fix tests that wait out their timeouts.
- Autofix CI Failures. Fix a red CI run as soon as it fails, with Trunk’s MCP server.