Skip to main content
A coding agent such as Cursor or Claude Code can fix a flaky test when it has the right context: which test, when it became flaky, and how it fails. Trunk’s API provides all of it. Have your agent follow these steps.

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 buildUrl instead.

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_changed event and act when new_status is FLAKY. Pass test_case.id to the calls below.
  • On a schedule. Call GET /v2/tests with status=flaky, newlyFlaky=true and a period, narrowed with testCollectionId or owner if you like.

2. Gather context

3. Fix and track

Keep monitoring the test afterwards to confirm the fix worked.