# `H.Session.UserShell`
[🔗](https://github.com/parlant/harness/blob/v0.3.0/lib/h/session/user_shell.ex#L1)

Durable backend lifecycle for user-invoked shell commands (`!` / `!!`).

Enforces `expected_seq` conflict guards, persists `"user_shell"` task intent
before spawning workers on the bound `H.ExecutionEnv`, coordinates per-workspace
mutating leases via `H.WorkspaceLease`, supports cooperative and immediate
`abort_ref` cancellation, and never replays unsafe interrupted work across
worker crashes or VM restarts.

# `busy?`

```elixir
@spec busy?(map(), pos_integer()) :: boolean()
```

Returns `true` if a `generation`, `user_shell`, or `compaction` run is active in `cid`.

# `cancel_worker`

```elixir
@spec cancel_worker(pos_integer(), map()) :: :ok | :not_found
```

Requests cooperative cancellation of an active `user_shell` worker via `abort_ref`.

# `handle_output`

```elixir
@spec handle_output(map(), pos_integer(), pos_integer(), term()) :: map()
```

Buffers streamed `user_shell` output chunks into ephemeral `live` state.

# `merge_ephemeral`

```elixir
@spec merge_ephemeral(map(), map() | nil) :: map()
```

Merges buffered ephemeral `user_shell` output into `live`.

# `recover_on_open`

```elixir
@spec recover_on_open(map(), map(), integer()) :: {[H.Storage.write()], map()}
```

Reconciles a non-terminal `user_shell` task (`"running"` or `"pending"`) on
session open / VM restart. Because `user_shell` is unsafe, it NEVER replays;
any persisted partial output is recorded in an interrupted entry.

# `spawn_worker`

```elixir
@spec spawn_worker(pos_integer(), pos_integer(), map(), keyword()) :: map()
```

Acquires the workspace lease, persists `"running"` task state before spawning,
and launches the supervised worker executing on `state.execution_env`.

# `stage`

```elixir
@spec stage(
  term(),
  pos_integer(),
  String.t(),
  String.t(),
  map(),
  map(),
  integer()
) ::
  {:immediate, term(), {:error, term()}, [term()], map()}
  | {:batch, map(), map()}
```

Stages a `user_shell` command batch after verifying `expected_seq` and
conversation idle state. Persists task intent (`status: "pending"`,
`replay: "unsafe"`) before spawning.

# `stage_abort`

```elixir
@spec stage_abort(map(), pos_integer(), String.t(), map(), integer()) ::
  {[H.Storage.write()], map(), [term()]}
```

Stages immediate cancellation of an active `user_shell` task during session `abort`.
Captures partial output, marks entry cancelled, aborts task, and returns worker
termination + lease release effects.

# `stage_crashed`

```elixir
@spec stage_crashed(pos_integer(), pos_integer(), term(), map()) ::
  {:skip, map()} | {:batch, map(), map()}
```

Stages failure when a `user_shell` worker crashes abnormally. Never replays
unsafe work; preserves any streamed partial output.

# `stage_done`

```elixir
@spec stage_done(
  pos_integer(),
  pos_integer(),
  {:user_shell_result, non_neg_integer(), term()},
  map()
) ::
  {:skip, map()} | {:batch, map(), map()}
```

Stages completion of a `user_shell` worker result.

# `unsafe_task?`

```elixir
@spec unsafe_task?(map()) :: boolean()
```

Returns `true` for unsafe `user_shell` tasks that must never replay on recovery.

# `validate_args`

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

Validates external `user_shell` command arguments.

---

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