# `H.Document`
[🔗](https://github.com/parlant/harness/blob/v0.3.0/lib/h/document.ex#L1)

Versioned pure-functional conversation documents (`agent`, `live`, `inbox`, `usage`),
JSON delta operations, and fork policy resolution.

# `op`

```elixir
@type op() :: [String.t() | term()]
```

# `accumulate_usage`

```elixir
@spec accumulate_usage(map(), map()) :: {map(), [op()]}
```

Accumulates turn usage metrics into the `"usage"` document and returns
`{new_usage, ops}`.

# `append_text`

```elixir
@spec append_text(term(), [String.t() | non_neg_integer()], String.t()) ::
  {term(), [op()]}
```

Appends text at a path inside a document and returns `{new_doc, [op]}`.

# `apply_op`

```elixir
@spec apply_op(term(), op()) :: term()
```

Applies a single JSON delta operation (`replace`, `r`, `set`, `append_text`, `delete`).

# `apply_ops`

```elixir
@spec apply_ops(term(), [op()]) :: term()
```

Applies a list of JSON delta operations to a document immutably.

# `configure_agent`

```elixir
@spec configure_agent(map(), map()) :: {map(), [op()]}
```

Applies validated configuration updates to an `"agent"` document and emits
minimal `["set", [key], val]` delta ops.

# `definition`

```elixir
@spec definition(String.t()) :: {:ok, map()} | :error
```

Returns the definition map for a built-in document kind.

# `definitions`

```elixir
@spec definitions() :: %{required(String.t()) =&gt; map()}
```

Returns metadata for all built-in document kinds.

# `delete`

```elixir
@spec delete(term(), [String.t() | non_neg_integer()]) :: {term(), [op()]}
```

Deletes a path inside a document and returns `{new_doc, [op]}`.

# `fork_docs`

```elixir
@spec fork_docs(H.Storage.t(), pos_integer(), non_neg_integer(), keyword()) ::
  {:ok, %{required(String.t()) =&gt; term()}, H.Storage.t()} | {:error, term()}
```

Computes initial documents for a forked conversation at `at_entry_seq`.

`"agent"` uses `fork: :as_of` (read from storage at `as_of_seq: at_entry_seq`),
while `"live"`, `"inbox"`, and `"usage"` use `fork: :initial`.

# `initial`

```elixir
@spec initial(String.t(), keyword()) :: term()
```

Returns the initial value for a built-in conversation document.

# `replace`

```elixir
@spec replace(term(), term()) :: {term(), [op()]}
```

Replaces a document value and emits `["replace", value]`.

# `set`

```elixir
@spec set(term(), [String.t() | non_neg_integer()], term()) :: {term(), [op()]}
```

Sets a path inside a document and returns `{new_doc, [op]}`.

# `unwrap`

```elixir
@spec unwrap(nil | map(), String.t(), keyword()) :: term()
```

Unwraps a stored document record map, falling back to `initial/2` when `nil`.

# `wrap`

```elixir
@spec wrap(String.t(), term()) :: map()
```

Wraps a document value in a storage record map.

---

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