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

Server-owned session metadata index (`<data_dir>/catalog/sessions.json`)
backed by a `:protected` ETS table for lock-free concurrent reads.

`H.Catalog` never opens `<data_dir>/sessions/<session_id>/` directly and
never wakes unloaded sessions on read.

# `annotate_directories`

```elixir
@spec annotate_directories(String.t() | nil, [map()], keyword()) :: [map()]
```

Annotates a list of directory entry maps in a single pass over ETS for `env_id`.

Each directory map is enriched with `direct_session_count`, `subtree_session_count`,
`active_session_count`, and `latest_session` (matching atom vs. string key style of
each input directory map). Subsessions (`parent != nil`) are excluded by default.

Options:
  * `:table` - ETS table name (default `:h_catalog`)
  * `:parent_path` - optional base path used when a directory map only provides `:name` / `"name"`
  * `:include_subsessions` - include subsessions in directory counts/recency (default `false`)

# `broadcast_live_state`

```elixir
@spec broadcast_live_state(GenServer.server(), String.t()) :: :ok
```

Broadcasts a fresh `:catalog` upsert for `session_id` reflecting live computed
fields (`loaded`, `cwd_exists`) without rewriting `sessions.json`.

# `child_spec`

Returns a specification to start this module under a supervisor.

See `Supervisor`.

# `delete_session`

```elixir
@spec delete_session(GenServer.server(), String.t()) :: :ok
```

Deletes a session entry from ETS and disk atomically.

# `get_session`

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

Looks up a single session entry directly from ETS.

# `insert_session`

Creates an identity without overwriting a concurrently created session.

# `list_sessions`

```elixir
@spec list_sessions(atom()) :: [map()]
```

Lists all catalog entries directly from ETS, sorted by `updated_at` descending.

# `list_sessions_for_cwd`

```elixir
@spec list_sessions_for_cwd(String.t() | nil, String.t(), keyword()) :: [map()]
```

Lists catalog entries directly from ETS scoped to `{env_id, cwd}`.

Options:
  * `:table` - ETS table name (default `:h_catalog`)
  * `:subtree` - include sessions in descendant subdirectories (default `false`)
  * `:include_subsessions` - include sessions with a non-nil `parent` (default `true`)

# `put_session`

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

Inserts or merges server-owned session metadata after validating `session_id`
and absolute `cwd`. Persists to ETS and disk atomically.

# `quarantine`

```elixir
@spec quarantine(GenServer.server(), String.t(), term()) ::
  {:ok, map()} | {:error, :not_found}
```

Marks a session as quarantined (`status: "quarantined"`, `active: false`).

# `report_runtime`

```elixir
@spec report_runtime(GenServer.server(), String.t(), map()) ::
  {:ok, map()} | {:error, :not_found}
```

Applies worker-owned runtime updates (`title`, `active`, `next_wake_at`,
`updated_at`, `applied_edit_rev`) without overwriting server-owned fields
like `cwd` or `status`.

# `root_session?`

```elixir
@spec root_session?(map()) :: boolean()
```

Returns `true` when the catalog entry represents a root session (`parent == nil`).

# `sanitize_activity_label`

```elixir
@spec sanitize_activity_label(term()) :: nil | String.t()
```

Sanitizes an optional activity label to at most 120
printable characters without control characters.

# `set_archived`

```elixir
@spec set_archived(GenServer.server(), String.t(), boolean(), keyword()) ::
  {:ok, map()} | {:error, :not_found}
```

Archives or unarchives a session in the catalog without waking it.

# `set_triage`

```elixir
@spec set_triage(GenServer.server(), String.t(), map()) ::
  {:ok, map()} | {:error, :not_found | String.t()}
```

Updates catalog triage metadata (`needs_attention` and/or `tags`) without
waking the session. Setting `needs_attention: true` clears the archive flag.

# `snapshot`

```elixir
@spec snapshot(GenServer.server()) :: %{seq: pos_integer(), sessions: [map()]}
```

Returns an atomic catalog snapshot (`%{seq: pos_integer(), sessions: [map()]}`)
from the catalog GenServer without waking any sessions.

# `start_link`

```elixir
@spec start_link(keyword()) :: GenServer.on_start()
```

Starts the catalog process.

Options:
  * `:data_dir` (required) - root storage directory
  * `:name` - GenServer registration name (default `H.Catalog`, pass `nil` for unnamed)
  * `:table` - ETS table name (default `:h_catalog`)
  * `:pubsub` - `Registry` name for `:catalog` topic broadcasts (default `H.PubSub`, pass `nil` to disable)
  * `:registry` - `Registry` name for live session lookup (default `H.Registry`, pass `nil` to disable)

# `wire_session`

```elixir
@spec wire_session(map(), keyword()) :: map()
```

Formats a catalog entry into the wire-level `SessionSummary` map shared by
`H.Daemon` and catalog SSE broadcasts.

---

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