Skip to content
Source
Sign inSign up

Start a workflow on an event

Software factory / Agent workflows

Start a workflow on an event

You want an agent to act when something happens in the repository: a push to main, a merged pull request, a new label. Or you want it on a schedule.

5 min read

Put the trigger in on of the workflow. Start with a working workflow: see Write your first agent workflow.

1. Pick a trigger

TriggerStarts a run
event(kind, filters)When a repository event happens. See the list below
push({ branches, tags, paths })When a branch or a tag moves, as in CI
schedule(cron)On a cron schedule in UTC, at most every 5 minutes
manual({ inputs })When a person with write access starts it, with typed inputs
after(workflow, { conclusion })When a run of another CI workflow or agent workflow ends

A workflow can have several triggers. pullRequest() and button() are refused: use event('pull.opened') or event('pull.pushed').

2. Add it to the workflow

This workflow checks the docs after each push to main that changes them, and opens a pull request that fixes broken links:

import { agent, defineAgentWorkflow, event } from '@tanstack-source/ci'

const model = { provider: 'anthropic', model: 'claude-opus-5-5' } as const

export default defineAgentWorkflow('docs-links', {
  on: [event('push', { branches: ['main'], paths: ['docs/**'] })],
}).job('fix', {
  permissions: { contents: 'write' },
  steps: [
    agent({
      name: 'Fix links',
      model,
      prompt: 'Find the broken relative links in docs/. Fix them, then open a pull request into main from the branch fix-links.',
      tools: ['read_file', 'list_files', 'search', 'write_file', 'open_pr'],
    }),
  ],
})

Save it as .source/agent-workflows/docs-links.ts, and merge it.

3. See it start

  1. Push a change to a file in docs/ on main.
  2. Open Agents, then Workflows. The Last run of docs-links shows the new run.
  3. Click it to open the run page.

Done

Each push that changes docs/ on main starts one run. When the agent finds a broken link, it opens a pull request from agent/docs-links/fix-links.

The events

EventHappens when
issue.openedAn issue shows for the first time
issue.labeled, pull.labeledA label is added
issue.commented, pull.commentedA comment shows for the first time
issue.closed, pull.closedThe item is closed
pull.openedA pull request shows, or a draft becomes ready
pull.pushedA push moves the head branch. A move that the forge makes, such as Update branch, does not count
pull.reviewedA review shows for the first time
pull.mergedThe pull request is merged
pushA branch or a tag moves

The filters of event():

  • labels: only items with one of these labels. Not for push.
  • branches: the pushed branch, or the base branch of the pull request. Only for push and pull.*.
  • paths: only when a changed file matches. Only for push and pull.*.
event('issue.labeled', { labels: ['bug'] })
event('pull.opened', { branches: ['main'], paths: ['packages/**'] })

What starts a run, and what does not

How a run starts:

  • Each event starts one run of each workflow that takes it. When an event comes again, no second run starts.
  • The workflow code always comes from the tip of the default branch. A run on a pull request event checks out the head commit of the pull request, but runs the steps of the default branch.
  • The forge reads the triggers again after each push to the default branch.

What starts no run:

  • What the TanStack Source bot writes starts no event run. So a workflow that comments does not start itself again.
  • A push by the bot or a job to agent/<workflow>/... does not start that workflow.
  • An after() chain stops before a workflow starts a second time in the same chain.

Runs that wait for approval

The trust level of a run comes from the person who caused it, as in CI:

  • Trusted: a person with write access started it, or pushed the code.
  • Protected: a schedule, a start by hand or a push on a default branch whose rule requires a pull request.
  • Untrusted: a person without write access caused it, for example a comment by an outside contributor.

For a label event, the person who caused it is the person who added the label. On a pull request, the run takes the lower trust level of that person and the author of the pull request.

An untrusted run does not start by itself. It waits until a person with write access approves it:

  • On the run page, click Approve and run.
  • From a terminal: src factory agent-run approve <run id>.

The approved run starts with the workflow that is on the default branch at that time. If an admin disabled the workflow, or the workflow is not on the default branch anymore, the approval is refused.

To drop a run that waits, cancel it: src factory agent-run cancel <run id>.

One run of a workflow waits on an issue or pull request at a time. While it waits, more untrusted events on that item do not add runs.

An untrusted run only reads, and comments on the item of its run. It gets no secrets, and reaches only the package registries.

See Trust levels and secrets for the CI rules that these come from.

The item of an event

An agent step of an event run knows the number of the issue or pull request of the event. The forge adds it to the step's instructions, so the agent can comment on it or label it.

Next