Expression language
An agent definition holds expressions in some of its YAML keys. For the keys themselves, see Agent definition. The language is a typed subset of Scala with snake_case names. Hydra parses and type-checks every expression when it loads the definition, and refuses a definition with an expression that does not check.
agents:
swe:
runs_when:
build:
lives_on: issue
where: self.status == "open" && !self.archived
mode: headless
receives:
- event: issue/updated
match: (event.subject)-[child-of]->(self)
where: event.subject.status in ["complete", "cancelled", "failed"]
Where expressions appear
| Key | Must give | Names in scope |
|---|---|---|
runs_when entry where | Boolean | self, org, user! |
runs_when entry key items | Any value except a record | self, org, user! |
runs_when entry on_behalf_of | Principal or Option[Principal] | self, org, user! |
runs_when entry participants | Principal, Option[Principal] or List[Principal] | self, org, user! |
receives rule where with event | Boolean | self, event, the nodes the rule's match names, org, user! |
receives rule where with cron | Boolean | self, event, org, user! |
- A
lives_onentry must have awhere. Thewheredecides which nodes have an instance. - Only a
lives_onentry can have akey. - A
receivesrule'smatchis a graph pattern, not an expression. The rule'swherecan use each variable that thematchnames, such ascin(c:issue).
Variables
self
In a lives_on entry, self is the node the instance lives on. It has the fields of that node kind and an id field of type String. See Data model for the fields of each kind.
In an entry with instances, self is the singleton. It has a name field of type String. It also has each field that every instance sets to a string, boolean or number of one type. Hydra types a string as String, a boolean as Boolean, a whole number as Int and any other number as Double. Every instance must set the same fields with the same JSON types. A singleton has no node, so match! cannot start from it.
event
event is the event the rule receives. Only a receives rule's where can use it. Its fields are in Event fields in expressions.
Pattern variables
Each named node in a rule's match is a variable in the rule's where. The variable's type is the record of its node kind. A pattern that names self or event.subject does not create a new variable.
org and user!
org is the organization as a Principal.
user!("name") is the organization member with that username, as a Principal. The argument must be a string literal. Hydra reports an error when it loads a definition with a name that is not an organization member.
self.assignee.get_or_else(org)
self.assignee.contains(user!("alice"))
Literals
| Literal | Type | Example |
|---|---|---|
| boolean | Boolean | true, false |
| integer | Int | 3, -1 |
| decimal | Double | 0.5, -2.25 |
| string | String, or an enum that allows it | "open" |
| list | List[T] | ["review", "done"] |
| present option | Option[T] | Some("x") |
| empty option | Option[T] | None |
- Write a string in double quotes. A string supports the escapes
\",\\and\n. - A decimal has digits on both sides of the
.. - A
-is part of a number literal. The language has no minus operator. - All items of a list have the same type.
- The checker must be able to infer the item type of
[]andNonefrom the rest of the expression. - The language has no
null. UseNone, or callis_emptyon an optional value.
Types
| Type | Values |
|---|---|
Boolean | true or false |
Int | A 64-bit integer |
Double | A 64-bit floating point number |
String | Text |
enum, shown as "open" | "closed" | A string from a fixed set |
Option[T] | Some(x) or None |
List[T] | A list of T values |
| record | A node, an event or a nested object. This page shows a record by its kind, such as issue. |
Principal | org or an organization member |
Node fields take their types from the source's schema:
- An
integeris anInt, anumberis aDouble, and a stringenumis an enum. - A nullable field is an
Option. - An issue's
assigneenames a user and has the typeOption[Principal]. No other field has the typePrincipal. - An expression cannot read a field with no type in the schema or an object with no listed properties.
A string literal matches an enum type only when the enum allows that string. Hydra reports an error when an expression compares an enum field with a string the enum does not allow.
Operators
From lowest to highest precedence:
| Precedence | Operator | Operands | Result |
|---|---|---|---|
| 1 | a || b | Boolean | Boolean |
| 2 | a && b | Boolean | Boolean |
| 3 | a == b, a != b | Two values of one type | Boolean |
| 3 | a < b, a <= b, a > b, a >= b | Two Int, two Double or two String | Boolean |
| 3 | a in xs, a not in xs | A value and a List of its type | Boolean |
| 4 | !a | Boolean | Boolean |
| 5 | a.field, a.method, a.method(args) | See below | See below |
- Parentheses group subexpressions.
||and&&evaluate their right side only when the left side does not decide the result.- Comparisons do not chain. Write
a < b && b < c, nota < b < c. ==and!=compare whole values, including records and lists.- An expression cannot compare an
Intwith aDouble. - Strings order by their bytes.
- Hydra reports
x == Noneandx != Noneas errors. Usex.is_emptyandx.is_defined. notis valid only beforein. Negate with!.- The language has no arithmetic operators and no string concatenation.
self.status in ["review", "human-review"] && !self.archived
(self.status == "open" || self.status == "triage") && self.assignee.is_empty
Field access
x.name reads the field name of the record x. Write a field without ().
An Option of a record has no fields. Read the record's fields inside a method:
event.old.exists(o => o.status != event.subject.status)
event.old.map(o => o.status)
Lambdas
A method that takes a function takes a lambda. Write it as name => body, or as an expression with one _, which stands for the parameter.
match!(l => (self)-[has-label]->(l:label)).exists(l => l.name.starts_with("area:"))
match!(l => (self)-[has-label]->(l:label)).exists(_.name.starts_with("area:"))
- A lambda can appear only as a method's argument.
- A placeholder lambda can use
_only once. Name the parameter to use it more than once. - A parameter cannot be
self,event,orgor another reserved name.
Methods
Write a method with no arguments without (), as in x.is_defined.
Option[T]
| Method | Argument | Returns | Description |
|---|---|---|---|
is_defined | none | Boolean | Returns true for Some. |
is_empty | none | Boolean | Returns true for None. |
contains(x) | T | Boolean | Returns true when the value is Some(x). |
exists(f) | T => Boolean | Boolean | Returns f applied to the value, or false for None. |
forall(f) | T => Boolean | Boolean | Returns f applied to the value, or true for None. |
map(f) | T => U | Option[U] | Returns Some(f(v)) for Some(v), or None for None. |
flat_map(f) | T => Option[U] | Option[U] | Returns f(v) for Some(v), or None for None. |
get_or_else(x) | T | T | Returns the value, or x for None. |
or_else(x) | Option[T] | Option[T] | Returns the option, or x for None. |
List[T]
| Method | Argument | Returns | Description |
|---|---|---|---|
is_empty | none | Boolean | Returns true for an empty list. |
exists(f) | T => Boolean | Boolean | Returns true when f holds for at least one item, and false for an empty list. |
forall(f) | T => Boolean | Boolean | Returns true when f holds for every item, and true for an empty list. |
head_option | none | Option[T] | Returns the first item, or None for an empty list. |
map(f) | T => U | List[U] | Returns the result of f for each item. |
flat_map(f) | T => List[U] or T => Option[U] | List[U] | Applies f to each item and joins the items of the results into one list. |
flatten | none | List[U] | Joins the inner lists or options into one list. Each item must be a List[U] or an Option[U]. |
exists and forall stop at the first item that decides the result.
String and enums
| Method | Argument | Returns | Description |
|---|---|---|---|
starts_with(s) | String | Boolean | Returns true when the value begins with s. |
To test membership in a list, use the in operator.
match!
match!(x => pattern) gives the list of nodes one relation away from a node in scope. The pattern uses the grammar of a receives rule's match, with one edge:
match!(b => (self)-[blocked-on]->(b:issue))
match!(c => (self)<-[child-of]-(c:issue))
agents:
swe:
runs_when:
implement:
lives_on: issue
where: >-
self.status == "in-development"
&& match!(b => (self)-[blocked-on]->(b:issue)).forall(b => b.status == "done")
&& match!(l => (self)-[has-label]->(l:label)).exists(l => l.name == "ready")
mode: headless
- The pattern is one edge between two nodes. One node is the parameter, and the other node is the start.
- The parameter must name a kind, as in
(b:issue). - The edge names one relation type and points as
-[rel]->or<-[rel]-. The edge cannot use*,|or a hop range. - Neither node can have
{…}properties. Test fields in the lambda. - The relation must join the start's kind and the parameter's kind in that direction.
match!can appear only as the receiver of aListmethod, such asexists,forall,map,flat_map,is_emptyorhead_option.- A lambda on the matched nodes reads its parameter's fields, literals,
organduser!(…). It cannot readself,event, a pattern variable or the whole node. - The expression cannot give whole matched nodes. For example,
match!(…).head_optionalone is an error, andmatch!(…).map(b => b.status)is valid. match!cannot appear inside a lambda.
The start depends on the key:
| Key | Starts |
|---|---|
runs_when entry where, key, on_behalf_of, participants | The start is self, and only in a lives_on entry. |
receives rule where with event | The start is a node that the rule's match names, event.subject when every event type the rule matches has a subject, or self in a lives_on entry when the rule has no broadcast: true. |
receives rule where with cron | The start is self, and only in a lives_on entry. |
For relation types and the kinds they join, see Data model.
Errors
Hydra reports these errors when it loads the definition and refuses the definition:
- a syntax error
- an unknown name, field, method, macro, kind or relation type
- operands or arguments of the wrong type
- a string literal outside its enum
- a
user!name that is not an organization member - a
wherethat does not give aBoolean - an expression that nests deeper than 64 levels
At run time, a missing stored field of an Option type reads as None. A missing field of another type, or a stored value that does not fit its type, is a data mismatch:
| Key | Result of a data mismatch |
|---|---|
a where | The where gives false, and Hydra records a warning. |
a key item that reads self.<field> | Hydra records a warning and uses the field's raw value as the node stores it. When that value differs from the recorded key value, the instance restarts. |
any other key item | The instance keeps the stored key value, and Hydra records a warning. |
Results
A where decides by its Boolean value. In a runs_when entry, the node has an instance while the entry's where is true. In a receives rule, the rule delivers the event to the instance when the where is true. A rule with no where delivers every event its event and match select.