# `Nous.Profile`
[🔗](https://github.com/nyo16/nous/blob/v0.21.1/lib/nous/profile.ex#L1)

A versioned, content-addressed replacement for an agent's prompt text.

A profile carries **text only**: the agent's instructions, tool
descriptions and tool parameter descriptions. Tool names, types,
`required` lists, enums and defaults always come from the live agent, so a
profile can change what the model reads but never the call contract.

    profile = Nous.Profile.load!("priv/profiles/support_agent.json")
    agent = Nous.new("anthropic:claude-sonnet-4-5", instructions: @base, tools: tools, profile: profile)

    # Per run: another profile, or none (the base text)
    Nous.run(agent, prompt, profile: candidate)
    Nous.run(agent, prompt, profile: nil)

## Components

`components` maps a key to the text that replaces the addressed base:

  * `"instructions"` - the agent's instructions (its `:instructions`, else
    its `:system_prompt`)
  * `"tool:<name>"` - the description of the agent tool named `<name>`
  * `"tool:<name>#<pointer>"` - the `"description"` of the parameter schema
    node at the JSON pointer (RFC 6901 escaping). The pointer walks
    `/properties/<name>`, `/items`, `/additionalProperties` and
    `/anyOf|oneOf|allOf/<index>`; `"tool:<name>#"` is the root object.

Keys under `skill:`, `guard:`, `router:`, `output:` and `agent:` are
reserved and rejected. Text built into Nous (plugin, behaviour, skill and
guard prompts, built-in tools) is not addressable: it changes through a
reviewed release, not a runtime artifact. Instructions are at most 64 KiB
and each description at most 8 KiB; text must be non-blank UTF-8 without
NUL bytes.

## Manifest: failing closed on a changed base

`manifest` holds the hash of each base a component replaces:
`"instructions"` the hash of the base instruction text, `"tool:<name>"` the
hash of the tool's whole contract (description and parameters). Applying a
profile recomputes them from the live agent and fails with `:stale` on any
difference, so a profile never silently overwrites text a developer has
since changed. `seal/2` fills the manifest from an agent.

## Identity

`id` is `"sha256:" <> hex` of the canonical JSON (`Nous.Eval.CanonicalJSON`)
of `%{"schema", "components", "manifest"}`. `name`, `version`, `parent_id`
and `provenance` are not hashed, so identical text over an identical base
has one id whoever produced it.

## File format

`encode/1` writes the canonical JSON form, with the tool components nested
so tool names need no escaping:

    {"components":{"instructions":"...","tools":{"search_orders":{
      "description":"...","params":{"/properties/status":"..."}}}},
     "id":"sha256:...","manifest":{"instructions":"sha256:...",
     "tool:search_orders":"sha256:..."},"name":"support_agent","nous_profile":1}

`load/1` also reads YAML (`.yaml`/`.yml`) for hand-written profiles, which
may omit `id` (it is computed) and `manifest` (see `seal/2`). Keys are
matched against a fixed whitelist; no atom is created from a file.

# `t`

```elixir
@type t() :: %Nous.Profile{
  components: %{optional(String.t()) =&gt; String.t()},
  id: String.t() | nil,
  manifest: %{optional(String.t()) =&gt; String.t()},
  name: String.t() | nil,
  parent_id: String.t() | nil,
  provenance: map(),
  schema: pos_integer(),
  version: pos_integer() | nil
}
```

  * `:id` - `"sha256:<hex>"` content hash (see the moduledoc)
  * `:name` - logical name, `~r/\A[a-z0-9][a-z0-9_.-]{0,63}\z/`, or nil
  * `:version` - positive integer a store assigns under `name`, or nil
  * `:parent_id` - id of the profile this one was derived from, or nil
  * `:schema` - serialization schema version (`1`)
  * `:components` - component key => replacement text
  * `:manifest` - `"instructions" | "tool:<name>"` => hash of the base
  * `:provenance` - free-form audit data with string keys (`"source"`,
    `"run_ids"`, `"note"`, ...); not hashed

# `applied_id`

```elixir
@spec applied_id(Nous.Agent.t()) :: String.t() | nil
```

The id of the profile `agent` runs with, or nil.

# `apply`

```elixir
@spec apply(t() | nil, Nous.Agent.t()) ::
  {:ok, Nous.Agent.t()} | {:error, Nous.Errors.ProfileError.t()}
```

Returns `agent` running `profile`, or with its base text restored for
`nil`.

All or nothing: the patched agent is returned only when every check of
`validate/2` passes. A profile replaces the base text, never another
profile's text: applying to an agent that already has one starts from its
base. Tools whose description is replaced keep the original in
`:base_description` (what `Nous.Hook.Decision` shows a guard model).

# `apply!`

```elixir
@spec apply!(t() | nil, Nous.Agent.t()) :: Nous.Agent.t()
```

As `apply/2`, raising `Nous.Errors.ProfileError`.

# `components`

```elixir
@spec components(t() | Nous.Agent.t()) :: %{optional(String.t()) =&gt; String.t()}
```

The addressable text as a flat component map (key => text).

For a profile, its `components`. For an agent, the text it runs with now
(its profile's text where it has one, else its base text): the static
instructions, and the description of every tool a profile may address
(not built-in, uniquely named, no `"#"` in the name) plus each parameter
schema node's description, keyed by pointer. Dynamic (function)
instructions and absent descriptions have no entry. Use
`components(Nous.Profile.apply!(nil, agent))` for the base text.

# `diff`

```elixir
@spec diff(t() | map() | nil, t() | map() | nil) :: %{
  added: [String.t()],
  removed: [String.t()],
  changed: [String.t()],
  unchanged: [String.t()],
  components: [map()]
}
```

Component-level difference from `a` to `b`.

Each side is a profile, a component map (as `components/1` returns) or
`nil` (no components). Returns the keys `:added` (only in `b`),
`:removed` (only in `a`), `:changed` and `:unchanged`, each sorted, and
`:components`, one entry per added, removed or changed key in key order:

  * `:key`, `:change` (`:added | :removed | :changed`), `:before` and
    `:after` (the texts, `nil` on the missing side)
  * `:lines` - the line diff (`List.myers_difference/2` over lines) as
    `{:eq | :del | :ins, line}` tuples
  * `:lines_added` / `:lines_removed` - counts of `:ins` / `:del` lines
  * `:chars_delta` - characters of `b` minus characters of `a`
  * `:tokens_delta` - `:chars_delta` / 4, rounded: a rough estimate of
    the prompt-size change, not a tokenizer count

# `encode`

```elixir
@spec encode(t()) :: String.t()
```

Canonical JSON of `to_map/1`: byte-stable, keys sorted at every level.

# `from_map`

```elixir
@spec from_map(term()) :: {:ok, t()} | {:error, Nous.Errors.ProfileError.t()}
```

Builds a profile from its decoded file form (string keys).

`id` is computed; a declared `id` must match it. `manifest` may be absent
(an unsealed profile, see `seal/2`).

# `load`

```elixir
@spec load(Path.t()) :: {:ok, t()} | {:error, Nous.Errors.ProfileError.t()}
```

Reads a profile file: JSON (`.json`) or YAML (`.yaml`, `.yml`).

# `load!`

```elixir
@spec load!(Path.t()) :: t()
```

As `load/1`, raising `Nous.Errors.ProfileError`.

# `seal`

```elixir
@spec seal(t(), Nous.Agent.t()) :: {:ok, t()} | {:error, Nous.Errors.ProfileError.t()}
```

Fills `profile`'s manifest with the hashes of `agent`'s base text and
recomputes its id.

Every check of `validate/2` but the manifest's runs first. Sealing pins a
hand-written profile to the agent it was written for; from then on any
change to that base text makes the profile `:stale`.

# `to_map`

```elixir
@spec to_map(t()) :: map()
```

The file form of `profile`: string keys, tool components nested under
`"tools"`. `name`, `version`, `parent_id` and `provenance` appear only when
set.

# `validate`

```elixir
@spec validate(t(), Nous.Agent.t()) :: :ok | {:error, Nous.Errors.ProfileError.t()}
```

Checks that `profile` applies to `agent`, without applying it.

The checks run in order and stop at the first failure: schema, the
structure (component and manifest keys, text values, `name`, `version`,
`parent_id`, `provenance`), id, `:dynamic_instructions`, the addressed tools
(`:unknown_tool`, `:ambiguous_tool`, `:builtin_tool`), the parameter
pointers (`:unknown_param`), and the manifest (`:missing_manifest`,
`:stale`). An agent that already has a profile is checked against its
base text, not the applied text.

---

*Consult [api-reference.md](api-reference.md) for complete listing*
