Relations.Evidence (relations v0.1.0-dev)

Copy Markdown View Source

Records and recalls the evidence returned by an agent tool call.

Pass a running Relatex database owned by your application. The pair {session_id, call_id} identifies one immutable result within that database. Supply the tool's raw text output and choose which arguments and metadata to persist. For example, an application can redact credentials before recording or attach its own trace ID in metadata. Both maps accept JSON values and require string keys at every level. IDs and tool names are nonempty UTF-8 strings. Output can be empty. Set status: :error to record a failed call.

iex> alias Relations.Evidence
iex> {:ok, db} = Relatex.start_link()
iex> call = %Evidence{session_id: "run-17", call_id: "call-1", tool: "read", output: "42"}
iex> Evidence.record(db, call) == {:ok, call}
true
iex> Evidence.fetch(db, "run-17", "call-1") == {:ok, call}
true
iex> Evidence.recall(db, "run-17", limit: 1) == {:ok, [call]}
true
iex> Relatex.stop(db)
:ok

A RelationsSession links to each RelationsToolCall through RELATIONS_CALLED. Each call links to its RelationsObservation through RELATIONS_OBSERVED. The session has an id. The call stores session_id, call_id, tool and arguments_json. The observation stores status, output and metadata_json. Applications can attach their own nodes and edges to this path. This recipe owns the named properties and the two relationships. Use a directory-backed database to recall evidence across restarts. A stored row with an invalid payload returns {:error, :invalid_stored_evidence}.

Summary

Functions

Looks up one call by session and call ID. Returns :not_found for a missing call.

Returns a page of one session's evidence in ascending string order by call ID.

Atomically records one result and returns the stored evidence.

Types

json_object()

@type json_object() :: %{optional(String.t()) => json_value()}

json_value()

@type json_value() ::
  nil | boolean() | number() | String.t() | [json_value()] | json_object()

read_error()

@type read_error() ::
  {:invalid, atom()} | :invalid_stored_evidence | Relatex.Error.t()

recall_option()

@type recall_option() :: {:limit, 1..100} | {:after, String.t() | nil}

t()

@type t() :: %Relations.Evidence{
  arguments: json_object(),
  call_id: String.t(),
  metadata: json_object(),
  output: String.t(),
  session_id: String.t(),
  status: :ok | :error,
  tool: String.t()
}

Functions

fetch(db, session_id, call_id)

@spec fetch(db :: GenServer.server(), session_id :: String.t(), call_id :: String.t()) ::
  {:ok, t()} | :not_found | {:error, read_error()}

Looks up one call by session and call ID. Returns :not_found for a missing call.

recall(db, session_id, opts \\ [])

@spec recall(
  db :: GenServer.server(),
  session_id :: String.t(),
  opts :: [recall_option()]
) ::
  {:ok, [t()]} | {:error, read_error()}

Returns a page of one session's evidence in ascending string order by call ID.

The default :limit is 50 and the maximum is 100. To read the next page, pass its predecessor's last call ID as :after (exclusive). An unknown session or an exhausted page returns {:ok, []}. Each page reads the current graph. New calls that sort before a previously consumed cursor require a fresh scan.

record(db, evidence)

@spec record(db :: GenServer.server(), evidence :: t()) ::
  {:ok, t()} | {:error, :conflict | read_error()}

Atomically records one result and returns the stored evidence.

Concurrent callers sharing the database owner can safely retry the same record. Reusing its session and call IDs with different content returns {:error, :conflict} and preserves the original. Invalid input returns {:error, {:invalid, field}}. Engine errors are returned as Relatex.Error.