Skip to main content
Version: Next

Actions & Hooks

Entity types can declare callable actions via has_action and lifecycle hooks via has_hook. Both point to an action entity (or one of its subtypes). Actions provide a mechanism to execute logic in the context of an entity -- either as inline Lua scripts or as calls to external HTTP services.

Declaring Actions​

Actions are declared on an entity type via the has_action relationship:

"has_action": [
{
"properties": { "name": "bark", "label": { "en_us": "Bark" } },
"target": "action/bark"
}
]

The target can be either an executable_script (inline Lua) or a service_function (external HTTP).

Action Subtypes​

executable_script​

Runs an inline Lua script. The script is evaluated in the context of the entity and returns a string result.

{
"properties": { "name": "bark", "label": { "en_us": "Bark" } },
"target": {
"type_name": "entity_type/executable_script",
"properties": {
"name": "bark",
"script": "return \"wau wau!\""
}
}
}

An action's script may change entities -- the entity it is called on, any entity it loads (ents.load, get_entity) or reaches through a relationship, and new entities it creates with ents.create. The script only assigns property values; there is no commit() call. When the run ends, the engine writes the changes automatically as new commits, through the same path as an API update -- so the same lifecycle, branch and validation checks apply, and the changed entities' hooks fire.

A script run is atomic: everything it changed is written together, or nothing is. One call can create commits of several aggregates (at most 32), and the entity the action was called on may have a newer commit afterwards than the one the request addressed.

See Lua Scripting for the scripting reference and the rules for writing.

service_function​

Calls an external HTTP endpoint. This is the primary mechanism for integrating business logic implemented in external microservices (Python, Go, Rust, etc.).

{
"properties": { "name": "bark_net", "label": { "en_us": "Bark (Remote)" } },
"target": {
"type_name": "entity_type/service_function",
"properties": {
"name": "bark_net",
"endpoint": "http://localhost:12810/bark_service"
}
}
}

When invoked, the platform sends the entity context to the configured endpoint and returns the result to the caller.

Invoking an Action via the API​

Actions are invoked through the @action sub-route on any entity:

POST /api/v1/by-key/<type>/<key>/@action/<action_name>

Response 200:

{ "result": "<action output string>" }

Response 409: Action execution failed -- the script raised an error or one of its writes was refused. Because a script run is atomic, a 409 means nothing was written.

Example​

POST /api/v1/by-key/dog/rex/@action/bark
{ "result": "wau wau!" }

Lifecycle Hooks​

Where an action is invoked explicitly (POST …/@action/{name}), a hook fires automatically at a lifecycle event. A hook is declared via has_hook on an entity type and points at the same action targets (executable_script or service_function) — it reuses the entire action-execution machinery. Each has_hook edge carries two edge properties:

Edge propertyProperty typeValuesMeaning
eventenum_property_type/hook_eventon_create, on_update, on_status_changeWhich lifecycle moment the hook fires on
typeenum_property_type/hook_typepre, postTiming relative to the operation

Both are enum properties and both are required. The value sets are enums/hook_event and enums/hook_type, so the vocabulary is part of the model rather than a convention the engine enforces after the fact — and, like any enum property, they are written and read as their plain value string ("event": "on_create").

Events​

EventFires whenself is
on_createAn instance of the type is createdthe new (pre: not-yet-persisted) version
on_updateAn instance is re-versioned by a content editthe edited version
on_status_changeA lifecycle status transition (PUT /@status) occursthe entity being transitioned

Because both are enum-typed, an unrecognised event or type is rejected by the write that declares the hook (400, naming the offending value), not stored and reinterpreted later — so a typo can never silently turn a veto gate into a no-op. A hook edge that somehow carries no readable value for either is dropped at type resolution, loudly.

Pre vs. Post​

  • A pre hook runs before the operation and may veto it. A veto aborts the operation atomically — nothing is published (for create/update) and the branch status is left unchanged (for a transition).
  • A post hook runs after the operation has committed — for create/update that is after the enclosing batch commits, so self is the final, durable version with its sub-entities persisted and its virtual properties recomputed. It is advisory: a failure is logged but never rolls the operation back, and a post hook that returns a veto is ignored (with a warning) because there is nothing left to abort.

Veto Convention​

  • Lua (executable_script): return boolean false, or a table { ok = false, msg = "…" }, to veto. Any other return (nil, true, a string, a number) allows the operation.
  • Service (service_function): a 4xx HTTP response vetoes a pre-hook. A 5xx does not — see below.

A veto is a business decision and is reported as 400. A hook that could not be evaluated — a script that fails to compile, raises, or exceeds the 100 ms instruction budget, or an endpoint that cannot be reached or that answered 5xx — is a different outcome: the write is still rejected (a gate that cannot be evaluated is never treated as passed) but it is reported as 500, because the request itself was well-formed.

Only a 4xx is the endpoint deciding to reject. A 5xx means the hook service itself is broken, and reporting that as a 400 would blame the caller for a server-side fault — so a hook endpoint's own 5xx (like any other non-2xx that is not a 4xx) is classified as an infrastructure failure, not a veto.

The event Context​

Hook Lua scripts additionally receive a global event table (alongside self):

FieldPresent forDescription
event.nameallThe event name (e.g. "on_status_change")
event.timingall"pre" or "post"
event.kindallWhat actually changed, in the change feed's vocabulary: create, update, merge, status, …
event.from_status / event.to_statuson_status_changeSource / target status number
event.from_status_name / event.to_status_nameon_status_changeSource / target status name

event.kind is not a synonym for event.name: a merge commit and an ordinary edit both fire on_update, and only kind tells them apart — which is what makes a rule like "never auto-merge into a released aggregate" expressible:

if event.kind == "merge" and self.properties["released"] == true then
return { ok = false, msg = "released aggregates must be merged by hand" }
end
return true

A service_function hook receives the same information as {"entity": …, "event": {…}}, where the event object carries exactly the fields above.

"has_hook": [
{
"properties": { "event": "on_status_change", "type": "pre" },
"target": {
"type_name": "entity_type/executable_script",
"properties": {
"name": "block_release",
"script": "if event.to_status == 200 and self.properties[\"approved\"] ~= true then return { ok = false, msg = \"not approved\" } end return true"
}
}
}
]

Scope and Limitations​

  • Hooks are inherited from parent types — a rule declared on a base type applies to every subtype, which is why a subtype cannot silently drop an approval gate. (Actions, by contrast, stay direct-only: a caller invokes those by name on a concrete type.)
  • Hooks fire per entity version, including sub-entities (relationships, traits, list items, components) whose own type declares a hook.
  • A three-way merge commit is a content re-version, so it fires on_update — including the pre hook, which can therefore veto a merge. A fast-forward merge moves a branch pointer without creating a version and fires nothing.
  • Delete/tombstone, revive, version bump and purge do not fire hooks.
  • A pre hook script may change self; the change is folded into the commit being saved. See Writing From a Hook.
  • Multiple hooks sharing an (event, timing) run parents-first, then in authored (edge-creation) order within a type. A hook is identified by its target action: a subtype that re-declares a has_hook edge to the same action it already inherits fires it once, at the parent's position — while two distinct actions that merely share a name are two rules, and both fire.
  • A service_function hook is called synchronously and in-band (5 s timeout), so a slow endpoint slows the write that triggered it. Prefer the change feed (GET /api/v1/@events) for fan-out that does not need to veto.

Architecture Flow​

When an action is triggered from the frontend or API:

tip

Use executable_script for simple, self-contained logic (string formatting, basic calculations). Use service_function for complex business logic that benefits from a dedicated service with its own dependencies, testing, and deployment cycle.