H.Session.UserShell (H v0.3.0)

Copy Markdown View Source

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.

Summary

Functions

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

Requests cooperative cancellation of an active user_shell worker via abort_ref.

Buffers streamed user_shell output chunks into ephemeral live state.

Merges buffered ephemeral user_shell output into live.

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.

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

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

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.

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

Stages completion of a user_shell worker result.

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

Validates external user_shell command arguments.

Functions

busy?(state, cid)

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

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

cancel_worker(task_id, state)

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

Requests cooperative cancellation of an active user_shell worker via abort_ref.

handle_output(state, cid, tid, chunk)

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

Buffers streamed user_shell output chunks into ephemeral live state.

merge_ephemeral(live, eph)

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

Merges buffered ephemeral user_shell output into live.

recover_on_open(shell_task, state, now)

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

@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(from, cid, req_id, fp, args, state, now \\ System.system_time(:millisecond))

@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(shell_task, cid, reason, state, now)

@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(tid, attempt_id, reason, state)

@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(tid, attempt_id, arg, state)

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

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

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

validate_args(raw_args)

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

Validates external user_shell command arguments.