# `Wardrik.Scope`

Who an operation runs for. Every operation takes one.

    # user_42
    scope = MyApp.AgentRuntime.as("user_42")

    # user_42's agent, through its own tools
    scope = MyApp.AgentRuntime.as("user_42", actor: {:agent, "agt_1"})

Your app's `Wardrik.Authorizer` decides what each scope may do.

## Fields

  * `:server` - the `Wardrik.Server` that runs its operations.
  * `:wardrik` - which Wardrik, by name.
  * `:principal` - the `WardrikCore.Principal` it runs for.
  * `:actor` - `{:agent, id}` or `{:channel, name}` when one acts for the principal, or `nil`.
  * `:context` - facts for the authorizer, like a channel's `:asked_by`. `%{}` unless set.
  * `:policy` - in an agent's scope, the policy every agent it defines is held to.
  * `:labels` - in an agent's scope, the labels of what its run has read.
  * `:operation` - the operation running, while one runs.

# `actor`

```elixir
@type actor() :: {atom(), String.t()} | nil
```

Who acts for the principal, as `{kind, id}`, or `nil`.

# `t`

```elixir
@type t() :: %Wardrik.Scope{
  actor: actor(),
  context: map(),
  labels: [String.t()],
  operation: WardrikProtocol.operation() | nil,
  policy: String.t() | map() | nil,
  principal: WardrikCore.Principal.t(),
  server: module(),
  wardrik: atom() | struct()
}
```

# `new`

```elixir
@spec new(module(),
  wardrik: atom() | struct(),
  principal: WardrikCore.Principal.t() | String.t(),
  actor: actor(),
  context: map()
) :: t()
```

Returns a scope run by `server`.

    Wardrik.Scope.new(Wardrik,
      wardrik: MyApp.AgentRuntime,
      principal: "user_42",
      actor: {:channel, "slack"},
      context: %{asked_by: "user_7"}
    )

`:principal` is a `WardrikCore.Principal` or its ID.

# `run`

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

Runs operation `id` with `input` through the scope's server.

# `stream`

```elixir
@spec stream(t(), String.t(), map()) ::
  {:ok, Enumerable.t()} | {:error, Wardrik.Error.t()}
```

Streams operation `id` with `input` through the scope's server.

---

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