Packages

Embedded Relate graph databases for Elixir/OTP applications

Current section

Files

Jump to
relatex README.md
Raw

README.md

# Relatex

Relatex embeds the Relate property graph engine in an Elixir application. Put a database in your supervision tree, write parameterized Cypher, and use the same API for an in-memory graph or a local store that survives restarts. One use case is preserving an agent's tool observations and their provenance across sessions.

## Install

Add Relatex to your application's `mix.exs` and run `mix deps.get`. Commit the resulting `mix.lock` to keep the version you tested.

```elixir
{:relatex, "~> 0.2.0"}
```

Use Elixir 1.20 with OTP headers, a C compiler, make, curl, tar and a SHA-256 utility. The first compile downloads the matching Relate v1.5.0 archive, verifies its pinned digest and statically links the engine into the application’s NIF. The build selects macOS or glibc Linux on arm64 or x86_64. Subsequent starts work offline. For an offline build, point `RELATEX_FFI_DIR` at an unpacked release containing `include/relate.h` and `librelate_ffi.a`. The Relate release and its notices are linked below. [Release assets](https://github.com/relatedb/relate/releases/tag/v1.5.0) · [Engine license](https://github.com/relatedb/relate/blob/v1.5.0/LICENSE) · [Third-party notices](https://github.com/relatedb/relate/blob/v1.5.0/THIRDPARTY-NOTICES.md)

## Use

```elixir
{:ok, graph} = Relatex.start_link(database: ":memory:")

{:ok, _} = Relatex.query(graph,
  "CREATE (:ToolCall {name: $tool})-[:OBSERVED]->(:Observation {note: $note})",
  %{"tool" => "read", "note" => "first sample"}
)

{:ok, result} = Relatex.query(graph, """
MATCH (:ToolCall {name: $tool})-[:OBSERVED]->(o:Observation)
RETURN o.note AS note
""", %{"tool" => "read"})

[["first sample"]] = result.rows
:ok = Relatex.stop(graph)
```

For a durable supervised store, add `{Relatex, database: "./agent-graph", name: MyApp.Graph}` to your application's children. Each owner serializes calls on one native connection. Statement deadlines apply in the engine. The default is five seconds. Query results are materialized, so bound large answers in Cypher. [API contract](docs/api/elixir/spec.md) · [Embedding guide](docs/api/elixir/description.md)

### Sharing a busy store

Route readers and writers to the same supervised owner. Set `max_in_flight` to the number of outstanding queries and batches your application will accept on that store. A value of `1` admits one operation at a time. Larger values allow submitted work to queue on the same connection. The default is `:infinity`.

```elixir
{Relatex, database: "./agent-graph", name: MyApp.Graph, max_in_flight: 16}
```

At capacity, `query/3` and `transaction/2` return `{:error, %Relatex.Error{code: "Relatex.Overloaded", stage: "admission", retryable: true}}` before execution. The application can shed that work or retry with backoff and a deadline. Once submitted, a write can commit even if the caller exits. Separate durable stores can make progress independently. A competing live owner opening the same durable store receives Relate's storage lock error.

### Upgrading from 0.1.x

Update the Mix requirement to `~> 0.2.0` and run `mix deps.update relatex`.
Existing calls to `query/3` and `transaction/2` retain their result shapes.
Admission stays unbounded until an owner starts with a finite `:max_in_flight`.
Only the explicit `Relatex.Overloaded` response guarantees that admission
refused the operation. If a caller loses contact with the owner after submitting
a write, verify its outcome before replaying it.

Deploy the native client with a BEAM node restart. Drain application traffic
and stop durable owners before restarting the node, then reopen the same
store paths. Both `":memory:"` and an empty database path open an ephemeral
store. Check environment-supplied paths before opening a durable graph.
In-place NIF hot upgrades are unsupported.

## Run the examples

From this checkout, run `mix run docs/api/elixir/examples/observations.exs` to write a tool observation in a transaction, close the store and read its evidence after reopening it.

For a live agent test, start Ollama with a tool-capable model and run the example.

```sh
ollama pull qwen3.8:27b
elixir examples/lemieux_memory.exs
```

The script installs Lemieux for this example. The first agent reads a generated specimen. The script stores the actual tool call and output in Relate, releases the first transcript and removes the fixture. A fresh agent receives a lookup tool and the previous session ID. The script verifies both the recalled value and its provenance after reopening the graph. Set `RELATEX_OLLAMA_MODEL` to use another installed tool-capable model. [Lemieux example](https://github.com/houllette/relatex/blob/main/examples/lemieux_memory.exs)

## Develop

Install the versions in `.tool-versions`, then run `mix deps.get`, `mix test`, and `mix precommit`. Use `MIX_ENV=test mix dialyzer` for the CI type check. The native source lives in `c_src/` and binds the released C ABI v2. The adapter code uses the repository's MIT license.

For agent tooling, `mix tidewave` serves the development MCP endpoint on loopback at port 4027. In Claude Code, approve the project's `.mcp.json` interactively and check the connection with `/mcp` while that server runs.