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

In-VM cooperative remote execution agent GenServer (`H.ExecutionEnv.Remote` transport seam).

Owns a host-backed `H.ExecutionEnv.t()` (`H.ExecutionEnv.Host`) and an explicit
plain environment descriptor (`%{id: id, kind: "remote", cwd: cwd}`). Process state
lives in memory for the duration of the run and excludes credentials or auth secrets.

## Future network transport

This module implements the local in-VM RPC endpoint proving fencing, lease-TTL heartbeats,
bounded execution output, and process-group reaping semantics. A future network
adapter (authenticated mTLS WebSocket/HTTPS or SSH subsystem) will replace the `GenServer`
call/message framing while preserving this protocol contract.

# `descriptor`

```elixir
@type descriptor() :: %{id: String.t(), kind: String.t(), cwd: String.t()}
```

# `fence`

```elixir
@type fence() :: {term(), term(), term(), term()}
```

# `server`

```elixir
@type server() :: GenServer.server()
```

# `abort`

```elixir
@spec abort(server(), fence()) :: :ok | {:error, :not_found | {:agent_down, term()}}
```

Aborts an active fenced execution and waits for its process group to be reaped.

# `active_fences`

```elixir
@spec active_fences(server()) :: [fence()]
```

Returns the list of currently active execution fences on this agent.

# `bind_cwd`

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

Remotely resolves `path` and verifies that it exists as a directory.

# `canonical_path`

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

Remotely canonicalizes `path` by resolving symlinks on the target agent.

# `child_spec`

Returns a specification to start this module under a supervisor.

See `Supervisor`.

# `cwd`

```elixir
@spec cwd(server()) :: String.t()
```

Returns the agent's canonical base working directory.

# `descriptor`

```elixir
@spec descriptor(server()) :: descriptor()
```

Returns the plain JSON-safe environment descriptor (`id`, `kind: "remote"`, `cwd`).

# `exec`

```elixir
@spec exec(server(), H.ExecutionEnv.command(), keyword()) ::
  {:ok, H.ExecutionEnv.exec_result()} | {:error, term()}
```

Executes `command` on the remote agent under fence and optional lease semantics.

Supports `:session_id`, `:incarnation`, `:task_id`, `:attempt_id`, `:lease_ms`, `:cwd`,
`:timeout_ms`, `:window_bytes`, `:window_lines`, `:abort_ref`, `:on_output`, and `:env`.
Rejects duplicate or older fences with `{:error, :stale_fence}`.

# `list_dir`

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

Lists a remote directory with sorting, pagination, hidden/dir filters, and markers.

# `list_files`

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

Recursively discovers bounded files and directories on the remote agent.

# `read_file_bytes`

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

Reads bounded raw bytes from a remote file.

# `read_file_lines`

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

Reads bounded UTF-8 lines from a remote file.

# `renew_lease`

```elixir
@spec renew_lease(server(), fence(), pos_integer()) ::
  :ok | {:error, :not_found | :invalid_lease_ms | {:agent_down, term()}}
```

Renews the lease TTL (in milliseconds) for an active fenced execution.

# `resolve_path`

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

Remotely resolves `path` using target OS POSIX path semantics.

# `start_link`

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

Starts a `H.RemoteAgent` process.

## Options
  * `:env` (required) - base `H.ExecutionEnv.t()` instance owned by the agent
  * `:id` - non-empty environment identifier string (defaults to `H.ExecutionEnv.env_id(env)`)
  * `:name` - optional GenServer registration name

# `write_file`

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

Atomically writes `content` to a remote file, creating missing parent directories.

---

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