Writing a workflow¶
A workflow tells rig what states a task moves through, which moves are allowed, and what must be true before each move. rig has no lifecycle of its own: the rules live in a JSON file you choose. This page is the reference for that file. phase-2.md explains why the store and workflows exist; this page explains how to write one.
Choosing the workflow rig uses¶
rig loads one workflow per command, from the first of these that is set:
RIG_WORKFLOW, a preset name or an absolute path to a file.- The
workflowconfig key (a preset name or an absolute path). It is read only from the global file,~/.rig/config.json, andrig config setrefuses it: you edit the file by hand. - The built-in preset
basic.
A relative path is refused. rig never looks for a workflow inside a repository, so cloning a repository never makes rig run its programs.
rig workflow show prints the workflow in effect and where it came from.
--tree prints the states as an indented tree from the initial state, with
each transition's guards in brackets, its counters in parentheses and the
program that runs in the right-hand column. A ↩ marks a state already shown
higher up. --graph prints a box-drawing graph of the same states, with the
guards under each state and the edges that cross the picture listed at the
end. The two flags and --json are mutually exclusive: giving more than one is
exit 2. This is the start of --tree on the kit-loop workflow:
planned (start) fanout
├─ intake plan
│ └─ planned [owns set, spec set] ↩
└─ implementing [deps merged] review
├─ reviewed [no open findings] verify
│ ├─ changes (rounds += 1) fanout
│ │ └─ implementing [rounds < 4] ↩
│ └─ verified [sha is head, takes sha] record
…
and this is the start of --graph:
● intake plan
⇅ ↓ owns set, spec set
● planned (start) fanout
│ deps merged
┌─┬▶● implementing ───────────────────────┐ review
│ │ │ no open findings │
│ │ ● reviewed ───────────────────────────┤ verify
│ │ │ sha is head, takes sha │
…
rig workflow validate [<file>] checks a file, or the one in effect, and
reports any task that is in a state the workflow does not have. It exits 0 when
valid, 1 when invalid and 2 on an error. To see what a file does before you
use it, see Checking a workflow below.
Each problem names its JSON path and, where the file is known, the line and
column of the value, and a did you mean when a name is close to a valid one:
workflow demo is invalid:
transitions[0].to: state "dnoe" is not declared (line 6, column 44); did you mean "done"?
With --json each entry of problems has path and message, and gains
line, column (both 1-based) and suggestion; each is left out when it does
not apply. A file that does not parse at all is still exit 2, with the line and
column of the syntax error in the message.
A workflow file may start with a "$schema" key. rig accepts it and reads it
for nothing; an editor uses it for completion and checks. The schema is
published at
https://raw.githubusercontent.com/AindriuB/rig/main/schema/workflow.schema.json
(schema/workflow.schema.json in the repository):
{ "$schema": "https://raw.githubusercontent.com/AindriuB/rig/main/schema/workflow.schema.json", "name": "mine", "version": 1 }
The basic preset¶
open -> active -> done any -> stopped stopped -> (the state before)
any -> dropped
States are open (action start), active (action finish), done (final),
stopped (action decide, for a human) and dropped (final, the drop state).
Moving to stopped or dropped needs a reason. There are no gates.
Findings have two severities, blocking and info. Use
rig workflow show --json to see the exact file.
The file¶
A workflow is one JSON object. rig refuses a key it does not know, so a typo is an error and not a silent no-op.
| Key | Meaning |
|---|---|
name, version |
A label and a positive integer. A task records both when it is created. |
initial |
The state a new task starts in. |
branch |
Branch name template; {short}, {slug}, {id} and {key} are replaced. Optional. A task made from an issue (rig task new --from, rig tracker pull) has its branch stamped at creation, but only when the workflow has a branch template (with none, nothing is stamped), with {key} as the issue's key, lowercased, with every run of characters outside a-z, 0-9 and - turned into one -; a later change to the template or the issue does not rename it. Other tasks derive the branch from the template whenever it is read, and there {key} is the task's short id, even after rig task link, so linking never renames a branch. 0.3.0 cannot load a file whose template uses {key}. |
states |
An object: state name to {action, who, final, shaBound, drop}, all optional. |
onNewCommit |
The state a sha-bound task counts as in when its branch has moved on. Needed if any state is sha-bound. |
transitions |
The allowed moves, in order (see below). |
fields |
Typed fields on tasks. |
findings |
{severities: [...], fields: {...}}. |
counters |
Names of counters a task can carry. |
hooks |
Programs to run after a move. |
tracker |
How issues from a tracker become tasks. Optional. See "Linking tasks to a tracker": 0.3.0 cannot load a file that has this key. |
States¶
actionis a label thatrig nextprints for a task in this state, such asreviewormerge. rig attaches no meaning to it.whoisagent(the default) orhuman.rig next --who humanlists the tasks waiting on a person.final: truemeans no transition leaves the state, andrig task listhides tasks in it unless you pass--all. A final state cannot be sha-bound.drop: truemarks the workflow's drop state, whererig task drop <task> --reason <text>sends a task that will not be merged. The state must befinal, and at most one state may set it;rig workflow validatenamesstates.<name>.dropfor either breach. rig hardcodes no state name. A drop state is not counted as a way to finish:rig nextleaves it out when it works out how near a task is to done.rig task dropis exactlyrig task move <task> <drop-state> --reason <text>: the same transition lookup, gates, event, hooks,--actor,--once,--jsonand exit codes, so you need a transition into the drop state from the task's state, for example{ "from": "*", "to": "dropped", "takes": "reason" }. With no drop state it exits 1 and writes nothing. A dropped task is final: there is no revival, so create a new task.shaBound: trueties a task to the commit it was moved in at. When the task's branch head is no longer that commit, the task's effective state isonNewCommit. This is worked out when the task is read; nothing is written.onNewCommititself must be neither final nor sha-bound.
State names may not be empty, *, or start with $.
Transitions¶
{ "from": ["a", "b"], "to": "c", "takes": "sha", "count": "rounds", "gates": [ ... ] }
fromis a state, a list of states, or*for every non-final state other than the target.tois a state, or$previous.$previousmeans the state the task was in before its latest move. A task that has never moved has none, and the move is refused.rig nextshows such a move as blocked byprevious.takesnames an argument the move records:sha(--sha),pr(--pr) orreason(--reason). A move that takes one refuses to run without it.countadds one to the named counter when the move is written.- A move into a sha-bound state that takes no sha inherits the sha of the
sha-bound state it came from, so
verified -> pr_opencarriesverified's sha. Otherwise it records none and the state keeps the sha it was last entered at. gatesare checked in order before anything is written. Every gate is evaluated and the move happens only if all pass. A refusal is exit 1 and writes nothing. There is no--force: to get past a gate, change the file.
Routes must be unique. After from lists and * are expanded, no two
transitions may take the same state to the same target, and one transition may
not name a route twice. So {"from": "*", "to": "X"} beside
{"from": "a", "to": "X"} is refused, because * already covers a.
rig workflow validate reports each clash as
route a -> X is also taken by transitions[N]. To give one state different
rules from the rest, list the other states explicitly instead of using *.
Validation also requires that every transition names declared states, that no
transition leaves a final state, and that every state can be reached from
initial. Each problem is reported with its JSON path, for example
transitions[3].gates[0].shaIsHead.
Gates¶
A gate is an object with one key, the gate's name.
| Gate | Passes when |
|---|---|
{"noOpenFindings": {"severity": ["P1", "P2"]}} |
no finding with one of those severities is open. Each severity must be declared in findings.severities. |
{"shaIsHead": true} |
the sha the move was given is the branch head. The move must take sha. |
{"shaIs": "<state>"} |
the sha the move was given is the one recorded when the task entered <state>. The move must take sha. |
{"dependsIn": ["merged"]} |
every task named in the depends field is in one of those states. The workflow must declare a depends field of type tasks. A dependency in a sha-bound state is judged by its effective state. |
{"fieldSet": "<field>"} |
the field has a value. title and body count as fields. |
{"counterBelow": {"counter": "rounds", "max": 4}} |
the counter is below max. max is at least 1. |
{"command": ["program", "arg", "{short}"]} |
the program exits 0. |
A command gate gets the task as JSON on standard input and has 30 seconds.
rig next never runs one: it reports the move's ok as null and the gate
as "ok": null, meaning "not run". Only rig task move runs them.
To refuse a move to merged unless the commit landed on the default branch,
gate it with rig task landed:
{"command": ["rig", "task", "landed", "{slug}", "--sha", "{sha}"]}
The move must take sha. Offline, rig task landed proves a landing only by
git ancestry, so this gate refuses every squash merge: only the forge can prove
one. A repository that squash-merges needs --online in the argv:
{"command": ["rig", "task", "landed", "{slug}", "--sha", "{sha}", "--online"]}
The forge answer has to arrive within the gate's 30 seconds.
Fields, severities and counters¶
Task fields have a type: string, text, int, bool, list, enum
(with values), paths (file globs) or tasks (task references). title
and body are built in and cannot be declared. Two flags change how a field
is used:
context: trueprints the field in the fixed part ofrig context.owns: truemarks the onepathsfieldrig contextuses to find the task's files. At most one task field may carry it.
findings.fields are the extra fields a finding takes, such as a route
enum. They cannot carry owns. A finding's severity must be one of
findings.severities; NOTE or any severity no noOpenFindings gate names
never blocks anything.
Hooks¶
{ "on": "enter:stopped", "run": ["notify-user", "{short}", "{reason}"] }
A hook is a program rig runs after it has written an event. on is a selector
or a list of them: transition, enter:<state>, leave:<state>, final,
kind:<kind> or kind:*. Hooks run in file order, for every event a single-task
write produces, not only for the first. No rig tracker command runs
inline hooks. A hook gets
{"format":1,"event","task","effective"} as JSON on standard input, runs with
an allowlisted environment (add what it needs with "env": ["GH_TOKEN"]) and
has 30 seconds by default. A hook that fails is reported on standard error, and
in hookErrors with --json, but it never undoes the write. Something that
must be able to refuse a move is a command gate, not a hook.
A hook may also set "when": "linked", the only value, so that it fires only
for a task that is linked to a tracker issue. A hook that reports to a tracker
then skips a task with no link, instead of failing for it. An inline hook does
not match an event whose task has no link, a queued hook counts that event
delivered without running its program, and an event with no task never matches.
Any other value is a validate error at hooks[<i>].when. For one command, the
same check is rig tracker report <task> --if-linked, which exits 0 and sends
nothing when the task is not linked or no tracker is configured.
Names, selectors, the payload, the environment, the loop guard, rig hooks
start, rig events and recipes are in hooks.md.
What rig task move does, in order:
- It looks up the transition from the task's effective state to the target.
With no such transition the move is refused. A target of
$previouson a task that has never moved is refused too. - It evaluates every gate of that transition, not stopping at the first failure.
- If any gate failed, the move is refused: exit 1, and nothing is written.
- Otherwise it writes the move. If the task, its findings or its branch head changed since they were read, it starts again from step 1, a few times, and then gives up with a conflict.
- Only after the write do the hooks run, once for each event the move wrote
(the
transition, and acountwhen the transition counts something). A hook that fails is reported, and the move stands.
Arguments and placeholders¶
command gates and hooks are argument vectors. rig runs the program directly
and never through a shell. An argument that is exactly {short}, {id},
{slug}, {state}, {reason} or {sha} is replaced by that value, and the
value stays one argument whatever it contains, spaces and ; included. A
placeholder inside longer text, such as --to={state}, is refused by
validation. In a hook {state} is the task's state after the event, and
{reason} and {sha} are refused when the hook's on selector can fire for a
kind other than transition; in a gate {state} is the state the move leaves.
Linking tasks to a tracker¶
A tracker is an issue tracker: Jira, GitHub issues, GitLab issues, or any
other that you reach through a program of your own (exec). A task can be
linked to one issue. The tracker then owns the task's planning fields, such
as its title and description, and rig owns everything about the work itself:
state, findings, notes, counters, shas, branch and pull request. Nothing is
sent back to the tracker in 0.4.0. rig tracker pull, rig task new --from
and rig task link are the commands that use this; see the README for the
list.
The tracker key¶
The optional top-level tracker key says how an issue maps to a task. Like the
rest of the workflow file, it comes from the global file, so a cloned
repository cannot change it.
"tracker": {
"intake": { "states": ["design", "ready"] },
"map": {
"title": "title", "body": "description", "acceptance": "acceptance",
"priority": "priority", "assignee": "assignee",
"labels": "labels", "type": "type"
},
"report": { "status": { "review": "In Review", "done": "Done" } }
}
intake.stateslists the states a task may be created in from an issue. The default is the workflow'sinitialstate alone.--stateoutside the list is refused (exit 1). A listed state must be declared and must not be final.mapgoes from a rig field to one attribute of the issue. The attributes aretitle,description,acceptance,priority,assignee,labelsandtype. The mapped fields are the owned fields: the tracker supplies them, andrig task setandrig task editrefuse to change them on a linked task (exit 1). A key ofmapis a field the workflow declares, ortitle, orbody. An owned field is notowns: true.labelsandacceptanceneed alistfield; every other attribute needs astringortextfield. Each attribute is mapped from at most one field. With nomap, the owned fields aretitleandbody.-
report.statusgoes from a rig state to the name of a tracker status. rig checks that the states are declared and the names are not empty.rig tracker reportsends the entry for the task's current state as the issue's status; a state with no entry sends no status (exit 1). -
pushsays which issue typerig tracker pushgives a task. It has two lists of the tracker's own type names: levelsis ordered from the top. The pushed task takes the first name; each child, one deeper, takes the next; with--treea task's depth chooses its type. A task that has atypefield, or a linked issue with a type, uses that instead, and its children continue from it. A task deeper than the list is refused (exit 1).typeslists the names a task's owntypefield may choose; any other is refused.- rig hardcodes no type names: it neither knows
Epic,StorynorTask. The names are whatever your tracker calls them, and rig checks them against the tracker's list before it creates anything. A tracker that lists no types is sent none. Ifpushis present,levelsmust name at least one type; no name may be empty or repeated.
A four-level list, for Jira:
"push": {
"levels": ["Initiative", "Epic", "Story", "Sub-task"],
"types": ["Bug", "Story", "Sub-task"]
}
A two-level list, for a GitHub organisation with issue types:
"push": { "levels": ["Feature", "Task"] }
rig workflow validate reports each broken rule with its JSON path, for
example tracker.map.priority.
tracker.push is new in 0.6.0; older rigs reject it.
0.3.0 cannot load a workflow file that has a tracker key, or a branch
template that uses {key}. It rejects the unknown key or placeholder and
exits 2. If one global file is shared between machines, update rig on all of
them before you add either. Stores are not affected: 0.3.0
still reads a store that 0.4.0 wrote, and sees the current title and body of a
linked task; it skips the events it does not know and shows a task made from an
issue without its link or parent.
Where the tracker is¶
These config keys (set with rig config) say which tracker and where:
| Key | Meaning |
|---|---|
tracker.kind |
local (no tracker, the default), jira, github, gitlab or exec. |
tracker.url |
The tracker's address, https:// only. Jira needs it; GitHub and GitLab need it only for a self-hosted server. |
tracker.project |
The Jira project key, or owner/name (group/.../name on GitLab). For GitHub and GitLab the default is the origin remote's path. |
tracker.command |
For exec: the program and its arguments, a list. Global file only. |
tracker.jira.api |
auto (default), v2 (Server or Data Center) or v3 (Cloud). |
tracker.jira.epicField |
The Server or Data Center "Epic Link" field id. Found by name when unset. |
tracker.jira.acceptanceField |
The id of the Jira field that holds acceptance criteria. Without it, an issue has none. |
A repository opts in. A tracker is on only where tracker.kind is set at
local scope (.rig/config.json) or repo scope (the repository's section in the
global file). A tracker.kind at the top level of the global file is inert:
the tracker commands exit 2 as with no tracker, and rig prints
tracker.kind "<kind>" in the global config does not turn on a tracker; turn it
on for this repository with: rig config set tracker.kind <kind> --repo.
tracker.kind local at local or repo scope turns a tracker off. The global
file may still hold defaults (tracker.url, tracker.project,
tracker.jira.*) and credentials; they apply beneath a kind set in a
repository. rig tracker status shows which scope the kind came from. Moving
from 0.x: run rig config set tracker.kind <kind> --repo (or --local) in each
repository that used the global kind.
Trust. tracker.url decides where a login token is sent, so a repository
cannot supply it. If .rig/config.json is tracked by git (or is a symlink, or
rig cannot tell), rig ignores a tracker.url in it and the tracker commands
stop with an error; rig tracker status still exits 0 and reports
trusted: false. Set it in the global file or an untracked local file.
tracker.command is a program rig would run, so it is read only from the
global file.
GitHub and GitLab hosts. For github and gitlab rig takes the host from
tracker.url. With no tracker.url, it takes the host from the origin
remote, but only if that host is github.com (for GitHub), gitlab.com (for
GitLab), or the host you named in forge.host. Any other host is refused, and
the message says set tracker.url to the tracker's address. This is on
purpose: a guessed host could read or link an issue on the wrong server.
Linking, refreshing, unlinking¶
A task is unique by its link: one issue is linked from one task. The link is
identified by the tracker kind, the host, the key and, for GitHub and GitLab,
the project. Keys match whatever their case. Creating a task from an issue
that is already linked (rig task new --from, rig tracker pull) returns the
task that has it; rig task link on an issue another task has is refused
(exit 1).
A worked example, with a GitHub repository:
rig config set tracker.kind github --repo # use this repository's issues
rig tracker pull 42 # make a task from issue 42
rig tracker pull --label ready --children # make tasks from a label, and their sub-issues
rig tracker refresh --all # re-read every linked issue
rig tracker refresh --all --dry-run # show what a refresh would change
rig task link 07 43 # link the existing task 07 to issue 43
rig task unlink 07 # drop the link; the fields stay
pull and task new --from copy the owned fields from the issue into a new
task. refresh copies them again. The tracker wins: a refresh overwrites an
owned field you edited locally, and lists it under replaced in --json with
the value that was lost. A deleted issue is reported as gone and nothing is
written. task unlink leaves the fields as they are; from then on they are the
task's own.
In steps:
rig tracker pullandrig task new --fromlook for a task that has the issue. If one does, they return it; if not, they create a linked task with the owned fields copied from the issue.rig task linkis refused (exit 1) if another task has the issue; otherwise it links the task.rig tracker refreshreads the issue again. If the issue is gone it is reported asgoneand nothing is written. Otherwise the owned fields are overwritten, and any a local edit changed are listed asreplaced.rig task unlinkdrops the link and leaves the fields as they are.
Starting from a template¶
rig init --template <name> [--out <file>] [--force] [--json] writes a starter
workflow file. There are three templates:
minimal:open,doneanddropped.pr-gated:planned,implementing,pr_open,merged,stoppedanddropped, withtakes shaintoimplementingandmergedandtakes printopr_open.kit-loop: the kit's own review loop.
minimal and pr-gated mark dropped with "drop": true. The file goes to
.rig/workflow.json in the main checkout unless you give --out, and the
missing directories are made. .rig/ is added to .gitignore unless something
under it is already tracked. An existing file is refused (exit 1) unless you
pass --force. After writing, init checks the file as rig workflow validate
does; if that fails it exits 2 and removes the file. With no template, or an
unknown one, it exits 2 and lists the three.
Templates are not presets. workflow: pr-gated in config does not load one:
only basic is a preset, and a template is a file you own once it is written.
Init changes no config. It prints the next steps: point rig at the file by
adding "repos": {"<repoKey>": {"workflow": "<path>"}} to ~/.rig/config.json
by hand (or, with no origin, the top-level "workflow": "<path>", which applies
to every repository), then run rig workflow show and rig task new --slug <s>
--title <t>. With --json it prints {"template", "path", "next"}.
Checking a workflow¶
Three read-only commands take an optional file, or default to the workflow in
effect, and each takes --json and --repo <dir>:
| Command | Prints | Exit |
|---|---|---|
rig workflow explain [<file>] |
Each state (initial, final, drop, who, action, sha-bound), each move with its gates in plain words, and each hook. | 0; 2 invalid or error |
rig workflow try [<file>] |
Each move as ok, or unreachable with why, each state no move reaches, and each non-final state with no way out. |
0 every move can be taken, every state is reached and none is a dead end; 1 otherwise; 2 error |
rig workflow diff <file> |
Each non-final task the file would strand, with the reason. | 0 none stranded; 1 some; 2 error |
try starts at the initial state and takes every move in turn on a scratch
task held in memory. It never opens the real store for writing and never runs a
hook. It makes the gates that look only at the task true: fieldSet by
writing the field, counterBelow by leaving the counter low, and
noOpenFindings by adding no finding. A gate that needs the outside world
cannot be tried, so try assumes it passes and reports the move as ok
[assumed gates: ...] naming it. Those gates are shaIsHead, shaIs,
dependsIn and command. diff strands a task whose state is missing from
the file, or is not final in the file and has no move out, * included.
Worked example: a review loop¶
An example review workflow: plan, implement, review, verify, pull request,
merge. It is one workflow among others. The file below is the fixture rig
tests against (internal/workflow/testdata/review-loop.json); a project that
uses it keeps its own copy.
{
"name": "review-loop",
"version": 1,
"initial": "planned",
"branch": "task/{short}-{slug}",
"states": {
"planned": { "action": "implement" },
"implementing": { "action": "review" },
"reviewed": { "action": "verify" },
"verified": { "action": "open-pr", "shaBound": true },
"pr_open": { "action": "wait-ci", "shaBound": true },
"ci_green": { "action": "mark-ready", "shaBound": true },
"ready": { "action": "merge", "who": "human", "shaBound": true },
"merged": { "final": true },
"stopped": { "action": "decide", "who": "human" }
},
"onNewCommit": "implementing",
"transitions": [
{ "from": "planned", "to": "implementing",
"gates": [{ "dependsIn": ["merged"] }] },
{ "from": "implementing", "to": "reviewed",
"gates": [{ "noOpenFindings": { "severity": ["P1", "P2", "STYLE", "SPEC"] } }] },
{ "from": "reviewed", "to": "verified", "takes": "sha",
"gates": [{ "shaIsHead": true }] },
{ "from": "reviewed", "to": "implementing", "count": "rounds",
"gates": [{ "counterBelow": { "counter": "rounds", "max": 4 } }] },
{ "from": "verified", "to": "pr_open", "takes": "pr" },
{ "from": "pr_open", "to": "ci_green", "takes": "sha",
"gates": [{ "shaIs": "verified" }] },
{ "from": "ci_green", "to": "ready" },
{ "from": "ready", "to": "merged", "takes": "sha" },
{ "from": ["verified", "pr_open", "ci_green", "ready"], "to": "implementing" },
{ "from": "*", "to": "stopped", "takes": "reason" },
{ "from": "stopped", "to": "$previous" }
],
"fields": {
"acceptance": { "type": "list", "context": true },
"owns": { "type": "paths", "context": true, "owns": true },
"depends": { "type": "tasks" }
},
"findings": {
"severities": ["P1", "P2", "P3", "STYLE", "SPEC", "NOTE"],
"fields": { "route": { "type": "enum", "values": ["implementer", "orchestrator"] } }
},
"counters": ["rounds"],
"hooks": [
{ "on": "enter:stopped", "run": ["notify-user", "{short}", "{reason}"] }
]
}
Reading it through:
- Starting. A task begins in
planned. It cannot move toimplementinguntil every task in itsdependsfield ismerged. - Review.
implementing -> reviewedis refused while any P1, P2, STYLE or SPEC finding is open. P3 and NOTE findings never block. - Verifying.
reviewed -> verifiedtakes the sha that was reviewed and needs it to be the branch head.verified,pr_open,ci_greenandreadyare sha-bound, so a new commit on the branch drops the task back toimplementingwith no one having to notice.rig task is <t> verifiedanswers "was this verified at the current head?" with one exit code. - Rounds.
reviewed -> implementingis the only way back fromreviewed. It counts a round and is refused onceroundsreaches 4. The from-list of the plain-> implementingtransition therefore leavesreviewedout: if it were there, the same route would exist twice, and validation would refuse the file, and the round cap could be skipped. - Pull request.
verified -> pr_openrecords the pull request number and carriesverified's sha.pr_open -> ci_greenneeds the sha it is given to be the one recorded atverified. - Merge.
readyis for a human.ready -> mergedis final. - Stopping. From any state but
merged, a task can be stopped with a reason, which runs thenotify-userhook.stopped -> $previousreturns it to the state it left.
When the workflow changes under a task¶
A task records the workflow's name and version when it is created, but gates
use the workflow loaded now. A task in a state the current workflow lacks is
reported by rig workflow validate, and by rig next with the action
unknown-state. A * transition can still move it.