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

Transport-agnostic daemon API for `H` sessions, conversations, transcripts,
SSE subscriptions, and idempotent commands.

Every function accepts `opts \ []` so isolated `--no-start` tests and the
full `H.HTTP` daemon share the exact same contract. Read
[Local HTTP and SSE API on hiness.dev](https://hiness.dev/docs/protocol/) for
wire formats, idempotent `request_id` retries, and SSE cursor rules.

# `archive_session`

```elixir
@spec archive_session(String.t(), map(), keyword()) ::
  {:ok, %{session: map()}}
  | {:error,
     :not_found
     | {:bad_request, String.t()}
     | {:conflict, String.t()}
     | {:quarantined, String.t()}
     | term()}
```

Archives (and unloads from RAM) or unarchives a session.

# `browse_workspace`

```elixir
@spec browse_workspace(map(), keyword()) ::
  {:ok, map()}
  | {:error,
     :not_found
     | :unknown_environment
     | :workspace_unavailable
     | {:bad_request, String.t()}}
```

Browses directories in an execution environment (`GET /api/v1/fs`), enriching
each child directory with `H.Catalog` session counts/recency and listing
sessions rooted directly at `path`.

# `create_session`

```elixir
@spec create_session(map(), keyword()) ::
  {:ok, %{session: map(), seq: non_neg_integer()}}
  | {:error, {:bad_request, String.t()} | {:conflict, String.t()} | term()}
```

Idempotently creates (or attaches to) a session, persists it in `H.Catalog`,
and starts its supervised `H.Session.Server`.

When `session_id` is omitted or empty, a unique `<folder-slug>-<hex>` ID is
minted automatically.

# `execute_command`

```elixir
@spec execute_command(String.t(), map(), keyword()) ::
  {:ok, map()}
  | {:error,
     :not_found | {:bad_request, String.t()} | {:conflict, String.t()} | term()}
```

Validates and dispatches a command envelope to `H.Session.Server`.

# `fork_session`

```elixir
@spec fork_session(String.t(), map(), keyword()) ::
  {:ok, %{session: map(), seq: non_neg_integer()}}
  | {:error,
     :not_found
     | {:bad_request, String.t()}
     | {:conflict, String.t()}
     | {:quarantined, String.t()}
     | term()}
```

Forks a session's visible conversation history into a brand-new session bound
to an existing target `cwd` (allowing recovery/continuation when the original
`cwd` was deleted or moved).

# `get_session_detail`

```elixir
@spec get_session_detail(String.t(), keyword()) ::
  {:ok, %{session: map(), conversations: [map()], tasks: [map()]}}
  | {:error, :not_found | {:quarantined, String.t()} | term()}
```

Returns session detail, conversation ancestry forest, and live tasks.

# `get_session_tree`

```elixir
@spec get_session_tree(String.t(), keyword()) ::
  {:ok,
   %{
     session: map(),
     conversations: [map()],
     nodes: [map()],
     truncated: boolean()
   }}
  | {:error, :not_found | {:quarantined, String.t()} | term()}
```

Returns a bounded, read-only session-wide entry tree across all conversations
and branches without waking unloaded archived sessions.

# `get_submission`

```elixir
@spec get_submission(String.t(), String.t(), keyword()) ::
  {:ok, map()} | {:error, :not_found | {:bad_request, String.t()} | term()}
```

Looks up the durable outcome/status of a submission by `request_id`.

# `instance_options`

```elixir
@spec instance_options() :: keyword() | nil
```

Returns the running instance's validated daemon options, or `nil` when not booted.

# `list_environments`

```elixir
@spec list_environments(keyword()) :: [map()]
```

Lists configured execution environments (`"host"` plus any custom/remote environments).

# `list_files`

```elixir
@spec list_files(map(), keyword()) ::
  {:ok, map()}
  | {:error,
     :not_found
     | :unknown_environment
     | :workspace_unavailable
     | {:forbidden, String.t()}
     | {:bad_request, String.t()}}
```

Environment-scoped bounded recursive file discovery (`GET /api/v1/fs/files`).

Requires explicit `env_id` and absolute `cwd` and never falls back to the
daemon's local process `cwd`.

# `list_models`

```elixir
@spec list_models(keyword()) :: [map()]
```

Lists models from the configured `H.Provider`.

# `list_session_files`

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

Session-scoped bounded recursive file discovery (`GET /api/v1/sessions/:sid/files`).

Resolves `{env_id, cwd}` strictly from the session's `H.Catalog` record.

# `list_sessions`

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

Lists sessions directly from `H.Catalog` ETS without waking unloaded sessions.

# `list_sessions`

```elixir
@spec list_sessions(map(), keyword()) ::
  {:ok, [map()]} | {:error, {:bad_request, String.t()}}
```

Lists sessions filtered by `env_id`, `cwd`, optional `subtree`, `archived`, and `include_subsessions`.

# `open_workspace_review`

Opens a dedicated review session, reusing the last one for this workspace while empty.

# `read_file`

```elixir
@spec read_file(map(), keyword()) ::
  {:ok, map()}
  | {:error,
     :not_found
     | :unknown_environment
     | :workspace_unavailable
     | {:forbidden, String.t()}
     | {:bad_request, String.t()}}
```

Environment-scoped bounded read-only UTF-8 file reader (`GET /api/v1/fs/file`).

Requires explicit `env_id`, absolute `cwd`, and `path`. Enforces workspace
boundary containment (lexical + canonical symlink checks) and `no_follow: true`.

# `read_review_file`

Reads a bounded review file from the worktree, HEAD, or a pinned commit.

# `read_session_file`

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

Session-scoped bounded read-only UTF-8 file reader (`GET /api/v1/sessions/:sid/file`).

Resolves `{env_id, cwd}` strictly from the session's `H.Catalog` record.

# `recover_active_sessions`

```elixir
@spec recover_active_sessions(keyword()) :: :ok
```

Boot recovery pass: starts supervised session runtimes for every unquarantined
session that was `active: true` in `H.Catalog` when the previous VM stopped,
reconciling unfinished work without waiting for a client reconnect.

# `review_workspace`

Browses workspace directories, Git history, or a commit in the session's execution environment.

# `save_review_file`

Saves an existing text file if its revision still matches, under the workspace lease.

# `scan_entries`

```elixir
@spec scan_entries(String.t(), term(), map(), keyword()) ::
  {:ok, %{items: [map()], next_cursor: nil | String.t()}}
  | {:error, :not_found | {:bad_request, String.t()} | term()}
```

Scans cursor-paged raw transcript entries for a conversation.

# `stop_session`

```elixir
@spec stop_session(String.t(), map(), keyword()) ::
  {:ok, %{session: map()}}
  | {:error,
     :not_found
     | {:bad_request, String.t()}
     | {:quarantined, String.t()}
     | term()}
```

Stops all active runs across all conversations in a session.

# `subscribe_conversation`

```elixir
@spec subscribe_conversation(String.t(), term(), pid(), term(), keyword()) ::
  {:ok,
   {:snapshot, non_neg_integer(), binary()}
   | {:replay, [{non_neg_integer(), binary()}]}}
  | {:error, :not_found | {:bad_request, String.t()} | term()}
```

Subscribes `subscriber_pid` (must equal `self()`) to `H.PubSub` for
`{:conversation, session_id, cid}` and atomically returns either a `snapshot`
JSON binary or missed `replay` frames from the 64-entry commit ring.

# `subscribe_sessions`

```elixir
@spec subscribe_sessions(pid(), keyword()) ::
  {:ok, {:snapshot, non_neg_integer(), binary()}} | {:error, term()}
```

Subscribes `subscriber_pid` (must equal `self()`) to `H.PubSub` for `:catalog`
events and returns an atomic `{:snapshot, seq, snapshot_json_bin}` directly
from `H.Catalog` without waking any unloaded sessions.

# `triage_session`

```elixir
@spec triage_session(String.t(), map(), keyword()) ::
  {:ok, %{session: map()}}
  | {:error, :not_found | {:bad_request, String.t()} | term()}
```

Updates a session's `needs_attention` flag or `tags` without waking it.

# `unwatch_submission`

```elixir
@spec unwatch_submission(String.t(), String.t(), keyword()) :: :ok
```

Unregisters `self()` from `H.PubSub` topic `{:submission, session_id, request_id}`.

# `watch_submission`

```elixir
@spec watch_submission(String.t(), String.t(), pid(), keyword()) ::
  {:ok, {:settled, map()} | {:pending, map()}}
  | {:error, :not_found | {:bad_request, String.t()} | term()}
```

Race-free submission settlement watcher.

Registers `subscriber_pid` (when `subscriber_pid == self()`) on `H.PubSub` topic
`{:submission, session_id, request_id}` *before* querying `H.Session.Server`.

  * Returns `{:ok, {:settled, outcome}}` immediately (and unregisters any new
    registration) if the submission is already terminal (`"done"`, `"unanswered"`,
    `"withdrawn"`).
  * Returns `{:ok, {:pending, outcome}}` while keeping the registration active so
    `subscriber_pid` deterministically receives `{:submission_settled, outcome}`
    after the durable commit succeeds.

# `wire_submission_outcome`

```elixir
@spec wire_submission_outcome(map()) :: map()
```

Formats a submission outcome map into its JSON wire representation.

---

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