Nous.Eval.Codec (nous v0.21.1)

Copy Markdown View Source

The storage format of Nous.Eval.Run: a JSON-safe map, and its JSON text.

Every Nous.Eval.Store persists runs as encode_run_json/1 text and reads them back with decode_run_json/1, so a run written by one backend reads back identically from another. A store you implement outside Nous should do the same rather than invent its own serialization.

Versioning

Every record carries "schema_version" (Nous.Eval.Run.schema_version/0 when written). Decoding reads every version up to the current one and refuses a newer one with {:error, {:unsupported_schema_version, v}}: a record written by a later Nous may mean something this release cannot represent, and silently dropping what it does not understand would turn a baseline into a different run. The decoded struct keeps the version the record was written with.

Record shape

The run, its samples, scores, scorer errors, trace and events are JSON objects with string keys named after the struct fields (truncated? is "truncated", passed? is "passed"). Enumerated fields (status, outcome, verdict, kind) are plain strings decoded through literal tables; an unknown value is an error, not a guess. Score.value is "pass", "fail", a number, or {"label": text}. A skipped entry is a [scorer, reason] pair. Timestamps are ISO 8601 strings. Cases are referenced by case_id; the case itself is not stored.

Event names are lists of strings (["nous", "agent", "tool_call", "stop"]) and decode only through a closed table of the events Nous emits. An event whose name is not in the table is dropped and counted in the trace's dropped, never turned into a new atom.

Judge calibrations (Nous.Eval.JudgeCalibration.Result) are a separate record, encode_calibration_json/1, marked "schema": "nous.judge_calibration/1" and versioned by that marker.

Profiles (Nous.Profile) are stored in their own file form, encode_profile_json/1: the canonical JSON of Nous.Profile.to_map/1, versioned by its "nous_profile" marker and always carrying "id". decode_profile_json/1 recomputes the content hash, so a record whose text was changed after it was written is refused.

Open values

Open fields (fingerprint, budget, summary, metadata, output, details, event measurements/metadata) hold arbitrary terms, which are encoded as JSON values tagged where JSON has no native form. A tag is a single-key object whose key starts with $:

  • {"$atom": "executed"} - an atom other than nil/true/false
  • {"$amap": {...}} - a map whose keys are all atoms
  • {"$map": [[key, value], ...]} - any other map that is not a plain object (mixed or non-string keys, or a single key starting with $)
  • {"$tuple": [...]}
  • {"$dt": iso}, {"$ndt": iso}, {"$date": iso}, {"$time": iso}
  • {"$bin": base64} - a binary that is not valid UTF-8
  • {"$msg": {...}} - a Nous.Message (e.g. the messages of [:nous, :agent, :model, :start] and the response of its :stop)
  • {"$part": {...}} - a Nous.Message.ContentPart
  • {"$usage": {...}} - a Nous.Usage
  • {"$struct": [module, fields]} - any other struct; it decodes to a plain map of its fields, because rebuilding an arbitrary struct named by stored data is not something a reader should do
  • {"$opaque": text} - a term with no data representation (pid, reference, port, function, improper list), stored as its inspect/1 text and decoded as that string

Atoms are never created from stored data

Stored text is untrusted. An atom is decoded through a literal table of the atoms Nous's telemetry contract uses, then through String.to_existing_atom/1 (atoms in metadata are data, never dispatched on, and an atom some loaded module mentions, such as a guard strategy's module name, compares equal to what that module expects). A name that is neither stays a string, flagged as %{"$atom" => name} so it cannot be mistaken for a genuine string; it re-encodes to the same tag. A map key that cannot be decoded stays a string key.

Summary

Types

A JSON-safe map: only string keys, strings, numbers, booleans, nil, lists, maps.

Functions

Decodes a map produced by encode_calibration/1. A record with another "schema" is {:error, {:unsupported_schema, schema}}.

Decodes calibration JSON text. See decode_calibration/1.

Decodes a stored profile record. The record must declare its "id" ({:error, {:corrupt_profile, :missing_id}} otherwise), and the id must be the content hash (Nous.Errors.ProfileError with reason :id_mismatch otherwise).

Decodes profile JSON text. See decode_profile/1.

Decodes a map produced by encode_run/1 (or read back from JSON).

Decodes storage JSON text. See decode_run/1.

Decodes a summary map. Versioned like decode_run/1.

Decodes summary JSON text. See decode_summary/1.

Encodes a Nous.Eval.JudgeCalibration.Result as a JSON-safe map with "schema": "nous.judge_calibration/1". Intervals become {"estimate", "lower", "upper", "count", "n"} objects; enumerated fields (kappa_kind, error kind) plain strings.

Encodes a calibration as JSON text. See encode_calibration/1.

Validates profile and returns its file form (Nous.Profile.to_map/1) with "id" set. A profile that fails a structural check or whose declared id is not its content hash is {:error, %Nous.Errors.ProfileError{}}.

Encodes profile as canonical JSON text. See encode_profile/1.

Encodes run as a JSON-safe map.

Encodes run as its storage JSON text. See encode_run/1.

Encodes a Nous.Eval.Store.RunSummary as a JSON-safe map.

Types

json_map()

@type json_map() :: %{required(String.t()) => term()}

A JSON-safe map: only string keys, strings, numbers, booleans, nil, lists, maps.

Functions

decode_calibration(data)

@spec decode_calibration(map()) ::
  {:ok, Nous.Eval.JudgeCalibration.Result.t()} | {:error, term()}

Decodes a map produced by encode_calibration/1. A record with another "schema" is {:error, {:unsupported_schema, schema}}.

decode_calibration_json(text)

@spec decode_calibration_json(String.t()) ::
  {:ok, Nous.Eval.JudgeCalibration.Result.t()} | {:error, term()}

Decodes calibration JSON text. See decode_calibration/1.

decode_profile(data)

@spec decode_profile(map()) :: {:ok, Nous.Profile.t()} | {:error, term()}

Decodes a stored profile record. The record must declare its "id" ({:error, {:corrupt_profile, :missing_id}} otherwise), and the id must be the content hash (Nous.Errors.ProfileError with reason :id_mismatch otherwise).

decode_profile_json(text)

@spec decode_profile_json(String.t()) :: {:ok, Nous.Profile.t()} | {:error, term()}

Decodes profile JSON text. See decode_profile/1.

decode_run(data)

@spec decode_run(map()) :: {:ok, Nous.Eval.Run.t()} | {:error, term()}

Decodes a map produced by encode_run/1 (or read back from JSON).

Returns {:error, {:unsupported_schema_version, v}} for a record newer than this release, {:error, {:invalid_field, ...}} / {:error, {:invalid_value, ...}} for a malformed one.

decode_run_json(text)

@spec decode_run_json(String.t()) :: {:ok, Nous.Eval.Run.t()} | {:error, term()}

Decodes storage JSON text. See decode_run/1.

decode_summary(data)

@spec decode_summary(map()) ::
  {:ok, Nous.Eval.Store.RunSummary.t()} | {:error, term()}

Decodes a summary map. Versioned like decode_run/1.

decode_summary_json(text)

@spec decode_summary_json(String.t()) ::
  {:ok, Nous.Eval.Store.RunSummary.t()} | {:error, term()}

Decodes summary JSON text. See decode_summary/1.

encode_calibration(result)

@spec encode_calibration(Nous.Eval.JudgeCalibration.Result.t()) ::
  {:ok, json_map()} | {:error, term()}

Encodes a Nous.Eval.JudgeCalibration.Result as a JSON-safe map with "schema": "nous.judge_calibration/1". Intervals become {"estimate", "lower", "upper", "count", "n"} objects; enumerated fields (kappa_kind, error kind) plain strings.

encode_calibration_json(result)

@spec encode_calibration_json(Nous.Eval.JudgeCalibration.Result.t()) ::
  {:ok, String.t()} | {:error, term()}

Encodes a calibration as JSON text. See encode_calibration/1.

encode_profile(profile)

@spec encode_profile(Nous.Profile.t()) :: {:ok, json_map()} | {:error, term()}

Validates profile and returns its file form (Nous.Profile.to_map/1) with "id" set. A profile that fails a structural check or whose declared id is not its content hash is {:error, %Nous.Errors.ProfileError{}}.

encode_profile_json(profile)

@spec encode_profile_json(Nous.Profile.t()) :: {:ok, String.t()} | {:error, term()}

Encodes profile as canonical JSON text. See encode_profile/1.

encode_run(run)

@spec encode_run(Nous.Eval.Run.t()) :: {:ok, json_map()} | {:error, term()}

Encodes run as a JSON-safe map.

Returns {:error, {:invalid_field, {struct, field}, value}} when a struct field does not hold the type its struct documents (an unknown status, a non-string error): such a record could not be read back faithfully.

encode_run_json(run)

@spec encode_run_json(Nous.Eval.Run.t()) :: {:ok, String.t()} | {:error, term()}

Encodes run as its storage JSON text. See encode_run/1.

encode_summary(summary)

@spec encode_summary(Nous.Eval.Store.RunSummary.t()) ::
  {:ok, json_map()} | {:error, term()}

Encodes a Nous.Eval.Store.RunSummary as a JSON-safe map.

encode_summary_json(summary)

@spec encode_summary_json(Nous.Eval.Store.RunSummary.t()) ::
  {:ok, String.t()} | {:error, term()}

Encodes a Nous.Eval.Store.RunSummary as JSON text.