Current section
Files
Jump to
Current section
Files
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.3.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, transactions, checkpoints and configuration changes your application will accept on that connection. 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 an auto-commit write, a batch, or a final commit has been submitted, it can finish even if the caller exits. Separate durable stores can make progress independently. A second writer opening the same durable store receives Relate's storage lock error.
Use `Relatex.admission_stats(MyApp.Graph)` to inspect occupied slots and the number of requests the owner has refused. Query summaries include mutation counts, auto-commit write bookmarks, engine timings, and structured plans for EXPLAIN and PROFILE.
### Independent sessions and interactive transactions
`Relatex.connect/2` opens another live connection to the same database, including an in-memory graph. Each session has its own namespace, statement timeout and admission limit. Use the default connection for simple calls and open a session when a worker needs its own settings or transaction.
```elixir
{:ok, session} = Relatex.connect(MyApp.Graph, max_in_flight: 2)
result =
Relatex.transaction(session, fn tx ->
{:ok, %{rows: rows}} =
Relatex.query(tx, "MATCH (t:ToolCall {id: $id}) RETURN t.name", %{"id" => "read-1"})
case rows do
[[name]] -> name
[] -> Relatex.rollback(tx, :unknown_tool_call)
end
end)
:ok = Relatex.stop(session)
```
This returns `{:ok, name, nil}` on a successful read or `{:error, :unknown_tool_call}` on explicit rollback. A committed write returns a bookmark. The callback runs in the calling process and has exclusive use of its connection. Other sessions can continue working. Relate can reject one of two conflicting commits with a retryable error. The application decides whether it can safely repeat the whole callback.
### Reading a live directory
```elixir
:ok = Relatex.checkpoint(MyApp.Graph)
{:ok, reader} = Relatex.start_link(database: "./agent-graph", read_only: true)
{:ok, %{rows: rows}} = Relatex.query(reader, "MATCH (o:Observation) RETURN o.note LIMIT 20")
:ok = Relatex.stop(reader)
```
The reader includes committed WAL records as of open, even between checkpoints. The writer can continue committing while the reader works. Reopen the reader for a newer snapshot. A single-file `.rl` store requires its writer to close before readers can open.
### Upgrading from 0.1.x or 0.2.x
Update the Mix requirement to `~> 0.3.0` and run `mix deps.update relatex`.
Existing calls to `query/3` and `transaction/2` retain their success and error tuples. New fields on `Relatex.Result` are `nil` when an older remote owner serves the query. New controls against an older remote owner return a binding error.
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, review it through an independent session, roll back a duplicate review, and read the committed evidence from a snapshot.
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 using a read-only graph owner. 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.