H.Daemon (H v0.3.0)

Copy Markdown View Source

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 for wire formats, idempotent request_id retries, and SSE cursor rules.

Summary

Functions

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

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.

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

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

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).

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

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

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

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

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

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

Lists models from the configured H.Provider.

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

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

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

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

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

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

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

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.

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

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

Scans cursor-paged raw transcript entries for a conversation.

Stops all active runs across all conversations in a session.

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.

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.

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

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

Formats a submission outcome map into its JSON wire representation.

Functions

archive_session(session_id, params \\ %{}, opts \\ [])

@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(query_params \\ %{}, opts \\ [])

@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(params, opts \\ [])

@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(session_id, raw_envelope, opts \\ [])

@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(source_session_id, params, opts \\ [])

@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(session_id, opts \\ [])

@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(session_id, opts \\ [])

@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(session_id, request_id, opts \\ [])

@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()

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

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

list_environments(opts \\ [])

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

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

list_files(query_params \\ %{}, opts \\ [])

@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(opts \\ [])

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

Lists models from the configured H.Provider.

list_session_files(session_id, query_params \\ %{}, opts \\ [])

@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(opts \\ [])

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

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

list_sessions(query_params, opts)

@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(params, opts \\ [])

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

read_file(query_params \\ %{}, opts \\ [])

@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(session_id, path, source, opts \\ [])

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

read_session_file(session_id, query_params \\ %{}, opts \\ [])

@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(opts \\ [])

@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(session_id, params, opts \\ [])

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

save_review_file(session_id, path, revision, text, opts \\ [])

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

scan_entries(session_id, conversation_id, query_params \\ %{}, opts \\ [])

@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(session_id, params \\ %{}, opts \\ [])

@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(session_id, conversation_id, subscriber_pid, last_event_id_or_nil, opts \\ [])

@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(subscriber_pid, opts \\ [])

@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(session_id, params, opts \\ [])

@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(session_id, request_id, opts \\ [])

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

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

watch_submission(session_id, request_id, subscriber_pid \\ self(), opts \\ [])

@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(outcome)

@spec wire_submission_outcome(map()) :: map()

Formats a submission outcome map into its JSON wire representation.