Documentation

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

KeyMust giveNames in scope
runs_when entry whereBooleanself, org, user!
runs_when entry key itemsAny value except a recordself, org, user!
runs_when entry on_behalf_ofPrincipal or Option[Principal]self, org, user!
runs_when entry participantsPrincipal, Option[Principal] or List[Principal]self, org, user!
receives rule where with eventBooleanself, event, the nodes the rule's match names, org, user!
receives rule where with cronBooleanself, event, org, user!
  • A lives_on entry must have a where. The where decides which nodes have an instance.
  • Only a lives_on entry can have a key.
  • A receives rule's match is a graph pattern, not an expression. The rule's where can use each variable that the match names, such as c in (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

LiteralTypeExample
booleanBooleantrue, false
integerInt3, -1
decimalDouble0.5, -2.25
stringString, or an enum that allows it"open"
listList[T]["review", "done"]
present optionOption[T]Some("x")
empty optionOption[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 [] and None from the rest of the expression.
  • The language has no null. Use None, or call is_empty on an optional value.

Types

TypeValues
Booleantrue or false
IntA 64-bit integer
DoubleA 64-bit floating point number
StringText
enum, shown as "open" | "closed"A string from a fixed set
Option[T]Some(x) or None
List[T]A list of T values
recordA node, an event or a nested object. This page shows a record by its kind, such as issue.
Principalorg or an organization member

Node fields take their types from the source's schema:

  • An integer is an Int, a number is a Double, and a string enum is an enum.
  • A nullable field is an Option.
  • An issue's assignee names a user and has the type Option[Principal]. No other field has the type Principal.
  • 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:

PrecedenceOperatorOperandsResult
1a || bBooleanBoolean
2a && bBooleanBoolean
3a == b, a != bTwo values of one typeBoolean
3a < b, a <= b, a > b, a >= bTwo Int, two Double or two StringBoolean
3a in xs, a not in xsA value and a List of its typeBoolean
4!aBooleanBoolean
5a.field, a.method, a.method(args)See belowSee 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, not a < b < c.
  • == and != compare whole values, including records and lists.
  • An expression cannot compare an Int with a Double.
  • Strings order by their bytes.
  • Hydra reports x == None and x != None as errors. Use x.is_empty and x.is_defined.
  • not is valid only before in. 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, org or another reserved name.

Methods

Write a method with no arguments without (), as in x.is_defined.

Option[T]

MethodArgumentReturnsDescription
is_definednoneBooleanReturns true for Some.
is_emptynoneBooleanReturns true for None.
contains(x)TBooleanReturns true when the value is Some(x).
exists(f)T => BooleanBooleanReturns f applied to the value, or false for None.
forall(f)T => BooleanBooleanReturns f applied to the value, or true for None.
map(f)T => UOption[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)TTReturns the value, or x for None.
or_else(x)Option[T]Option[T]Returns the option, or x for None.

List[T]

MethodArgumentReturnsDescription
is_emptynoneBooleanReturns true for an empty list.
exists(f)T => BooleanBooleanReturns true when f holds for at least one item, and false for an empty list.
forall(f)T => BooleanBooleanReturns true when f holds for every item, and true for an empty list.
head_optionnoneOption[T]Returns the first item, or None for an empty list.
map(f)T => UList[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.
flattennoneList[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

MethodArgumentReturnsDescription
starts_with(s)StringBooleanReturns 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 a List method, such as exists, forall, map, flat_map, is_empty or head_option.
  • A lambda on the matched nodes reads its parameter's fields, literals, org and user!(…). It cannot read self, event, a pattern variable or the whole node.
  • The expression cannot give whole matched nodes. For example, match!(…).head_option alone is an error, and match!(…).map(b => b.status) is valid.
  • match! cannot appear inside a lambda.

The start depends on the key:

KeyStarts
runs_when entry where, key, on_behalf_of, participantsThe start is self, and only in a lives_on entry.
receives rule where with eventThe 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 cronThe 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 where that does not give a Boolean
  • 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:

KeyResult of a data mismatch
a whereThe 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 itemThe 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.