# 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 this Git dependency to your application's `mix.exs` and run `mix deps.get`. Commit the resulting `mix.lock` to keep your application on the revision you tested.

```elixir
{:relatex, github: "houllette/relatex"}
```

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)

## 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.
