H.ExecutionEnv behaviour (H v0.3.0)

Copy Markdown View Source

Pluggable filesystem and process execution boundary for H tools.

Tools (read, write, edit, bash) interact exclusively through H.ExecutionEnv rather than calling File or System.cmd directly, allowing local workspaces, disposable test directories, and later remote/sandbox environments to share one tool implementation and conformance suite.

Summary

Functions

Binds to an existing directory. With exact: true, rejects changes to its previously resolved canonical path.

Standardized project marker filenames used when browsing directories.

Runs a command in the bound environment. Host and Remote accept raw_output: true to preserve bytes and spill_output: false to retain only the bounded output window without persisting an overflow file.

Types

command()

@type command() :: {:shell, String.t()} | {:argv, [String.t()]}

exec_result()

@type exec_result() :: %{
  exit_code: integer(),
  output: binary(),
  truncated: boolean(),
  skipped_bytes: non_neg_integer(),
  full_output_path: nil | String.t()
}

t()

@type t() :: {module(), state :: term()}

Callbacks

bind_cwd(state, cwd)

(optional)
@callback bind_cwd(state :: term(), cwd :: String.t()) ::
  {:ok, state :: term()} | {:error, term()}

canonical_path(state, path)

(optional)
@callback canonical_path(state :: term(), path :: String.t()) ::
  {:ok, String.t()} | {:error, term()}

cwd(state)

@callback cwd(state :: term()) :: String.t()

env_id(state)

(optional)
@callback env_id(state :: term()) :: String.t()

exec(state, command, opts)

@callback exec(state :: term(), command(), opts :: keyword()) ::
  {:ok, exec_result()} | {:error, term()}

kind(state)

(optional)
@callback kind(state :: term()) :: String.t()

list_dir(state, path, opts)

@callback list_dir(state :: term(), path :: String.t(), opts :: keyword()) ::
  {:ok, %{entries: [map()], next_cursor: nil | non_neg_integer()}}
  | {:error, term()}

list_files(state, path, opts)

(optional)
@callback list_files(state :: term(), path :: String.t(), opts :: keyword()) ::
  {:ok, %{entries: [map()], truncated: boolean()}} | {:error, term()}

read_file_bytes(state, path, opts)

@callback read_file_bytes(state :: term(), path :: String.t(), opts :: keyword()) ::
  {:ok, binary()} | {:error, term()}

read_file_lines(state, path, opts)

@callback read_file_lines(state :: term(), path :: String.t(), opts :: keyword()) ::
  {:ok, map()} | {:error, term()}

resolve_path(state, path)

@callback resolve_path(state :: term(), path :: String.t()) ::
  {:ok, String.t()} | {:error, term()}

write_file(state, path, content)

@callback write_file(state :: term(), path :: String.t(), content :: iodata()) ::
  :ok | {:error, term()}

Functions

bind_canonical_cwd(env, path, opts \\ [])

@spec bind_canonical_cwd(t(), String.t(), keyword()) :: {:ok, t()} | {:error, term()}

Binds to an existing directory. With exact: true, rejects changes to its previously resolved canonical path.

bind_cwd(env, cwd)

@spec bind_cwd(t(), String.t()) :: {:ok, t()} | {:error, term()}

canonical_path(env, path)

@spec canonical_path(t(), String.t()) :: {:ok, String.t()} | {:error, term()}

cwd(arg)

@spec cwd(t()) :: String.t()

default_markers()

@spec default_markers() :: [String.t()]

Standardized project marker filenames used when browsing directories.

env_id(arg)

@spec env_id(t()) :: String.t()

exec(arg, command, opts \\ [])

@spec exec(t(), command(), keyword()) :: {:ok, exec_result()} | {:error, term()}

Runs a command in the bound environment. Host and Remote accept raw_output: true to preserve bytes and spill_output: false to retain only the bounded output window without persisting an overflow file.

kind(arg)

@spec kind(t()) :: String.t()

list_dir(arg, path, opts \\ [])

@spec list_dir(t(), String.t(), keyword()) ::
  {:ok, %{entries: [map()], next_cursor: nil | non_neg_integer()}}
  | {:error, term()}

list_files(arg, path, opts \\ [])

@spec list_files(t(), String.t(), keyword()) ::
  {:ok, %{entries: [map()], truncated: boolean()}} | {:error, term()}

new(mod, state)

@spec new(module(), term()) :: t()

read_file_bytes(arg, path, opts \\ [])

@spec read_file_bytes(t(), String.t(), keyword()) ::
  {:ok, binary()} | {:error, term()}

read_file_lines(arg, path, opts \\ [])

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

resolve_path(arg, path)

@spec resolve_path(t(), String.t()) :: {:ok, String.t()} | {:error, term()}

write_file(arg, path, content)

@spec write_file(t(), String.t(), iodata()) :: :ok | {:error, term()}