H.Catalog (H v0.3.0)

Copy Markdown View Source

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.

Summary

Functions

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

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

Returns a specification to start this module under a supervisor.

Deletes a session entry from ETS and disk atomically.

Looks up a single session entry directly from ETS.

Creates an identity without overwriting a concurrently created session.

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

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

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

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

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

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

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

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

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

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

Starts the catalog process.

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

Functions

annotate_directories(env_id, dir_entries, opts \\ [])

@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(server \\ __MODULE__, session_id)

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

Returns a specification to start this module under a supervisor.

See Supervisor.

delete_session(server \\ __MODULE__, session_id)

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

Deletes a session entry from ETS and disk atomically.

get_session(session_id, table \\ :h_catalog)

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

Looks up a single session entry directly from ETS.

insert_session(server, attrs)

Creates an identity without overwriting a concurrently created session.

list_sessions(table \\ :h_catalog)

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

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

list_sessions_for_cwd(env_id, cwd, opts \\ [])

@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(server \\ __MODULE__, attrs)

@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(server \\ __MODULE__, session_id, reason)

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

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

report_runtime(server \\ __MODULE__, session_id, runtime_fields)

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

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

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

sanitize_activity_label(val)

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

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

set_archived(server \\ __MODULE__, session_id, archived?, opts \\ [])

@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(server \\ __MODULE__, session_id, attrs)

@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(server \\ __MODULE__)

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

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

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

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