Spectre.Effect (Spectre v0.3.0)

Copy Markdown View Source

Runtime or host work represented as data.

Effects generalize the old pending-action shape. An action is now one effect kind; future capabilities such as retrieval or search can use the same contract without changing turn decisions. Action effects carry their owning module and mounted scope as first-class fields; payload data cannot override that trusted runtime origin.

Summary

Functions

Returns the Action Language text encoded in an action effect payload.

Marks a policy-gated effect as approved without executing it.

Associates an Effect with its owning Instance Run.

Marks an effect as cancelled.

Marks an effect as completed with a result.

Returns the stable key used for protection and dispatch checks.

Returns whether an effect is ready for its host capability boundary.

Marks an effect as failed with an error.

Returns hooks attached to an effect payload.

Returns the stable key that an action adapter can use to deduplicate retries.

Normalizes a terminal effect into a host-facing outcome.

Returns the module that owned the route which staged the effect.

Returns the planner that produced the action, when present.

Restores a persisted effect without inventing a missing scope.

Returns the approved provider schema hash, when present.

Returns the Agent or mounted-Skill scope that staged the effect.

Returns the selected tool encoded in an action effect payload.

Returns the source encoded in an effect payload.

Builds a pending effect outside a routed Skill context.

Builds a pending action effect with a trusted owner and scope.

Returns whether an effect reached a terminal lifecycle state.

Returns the provider identifier encoded in an action effect.

Marks an effect as waiting on a policy gate.

Types

outcome()

@type outcome() :: {:ok, term()} | {:error, term()} | {:cancelled, term()} | nil

status()

@type status() ::
  :pending | :waiting_policy | :approved | :completed | :failed | :cancelled

t()

@type t() :: %Spectre.Effect{
  args: map(),
  error: term(),
  id: term(),
  idempotency_key: String.t(),
  kind: atom(),
  metadata: map(),
  mode: atom() | nil,
  name: atom() | String.t() | nil,
  owner: module() | nil,
  payload: map(),
  policy: term(),
  result: term(),
  run_id: String.t() | nil,
  scope: Spectre.Definition.scope() | nil,
  status: status()
}

Functions

al(effect)

@spec al(t()) :: String.t() | nil

Returns the Action Language text encoded in an action effect payload.

approve(effect)

@spec approve(t()) :: t()

Marks a policy-gated effect as approved without executing it.

Approval is a durable state transition. The host may execute the effect only after the approved state has been persisted.

bind_run(effect, run_id)

@spec bind_run(t(), String.t() | nil) :: t()

Associates an Effect with its owning Instance Run.

Extension-owned Effect builders should use Spectre.Context.lifecycle_run_id/1 and bind the returned value before staging. Passing nil preserves the stateless and Session compatibility lifecycle.

cancel(effect, reason \\ nil)

@spec cancel(t(), term()) :: t()

Marks an effect as cancelled.

complete(effect, result)

@spec complete(t(), term()) :: t()

Marks an effect as completed with a result.

effect_key(name)

@spec effect_key(t() | map() | atom() | String.t()) :: atom() | String.t() | nil

Returns the stable key used for protection and dispatch checks.

executable?(effect)

@spec executable?(t()) :: boolean()

Returns whether an effect is ready for its host capability boundary.

Policy-gated effects become executable only after their durable :waiting_policy -> :approved transition.

fail(effect, error)

@spec fail(t(), term()) :: t()

Marks an effect as failed with an error.

hooks(effect)

@spec hooks(t()) :: [map()]

Returns hooks attached to an effect payload.

idempotency_key(effect)

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

Returns the stable key that an action adapter can use to deduplicate retries.

outcome(effect)

@spec outcome(t()) :: outcome()

Normalizes a terminal effect into a host-facing outcome.

Older adapters may have stored {:ok, value} or {:error, reason} inside a completed effect. These shapes are flattened for backwards compatibility. Non-terminal effects return nil.

owner(effect)

@spec owner(t()) :: module() | nil

Returns the module that owned the route which staged the effect.

Legacy persisted effects may not carry an owner and return nil.

planned_by(effect)

@spec planned_by(t()) :: module() | atom() | nil

Returns the planner that produced the action, when present.

restore(attrs)

@spec restore(map() | struct()) :: t()

Restores a persisted effect without inventing a missing scope.

Durable codecs use this behavior for legacy state. An unscoped restored effect remains observable but must be rejected by the execution boundary.

schema_hash(effect)

@spec schema_hash(t()) :: String.t() | nil

Returns the approved provider schema hash, when present.

scope(effect)

@spec scope(t()) :: Spectre.Definition.scope() | nil

Returns the Agent or mounted-Skill scope that staged the effect.

Older persisted effects may not carry a scope and return nil.

selected_tool(effect)

@spec selected_tool(t()) :: String.t() | nil

Returns the selected tool encoded in an action effect payload.

source(effect)

@spec source(t()) :: atom() | nil

Returns the source encoded in an effect payload.

stage(attrs)

@spec stage(map() | struct()) :: t()

Builds a pending effect outside a routed Skill context.

Effects staged through this generic compatibility API default to Agent scope. Routed actions should use stage_action/3 so ownership cannot be omitted.

stage_action(attrs, owner, scope)

@spec stage_action(map() | struct(), module(), Spectre.Definition.scope()) :: t()

Builds a pending action effect with a trusted owner and scope.

Explicit origin arguments replace any owner or scope supplied by the action payload. Both deterministic DSL actions and model-planned actions use this constructor.

terminal?(effect)

@spec terminal?(t()) :: boolean()

Returns whether an effect reached a terminal lifecycle state.

via(effect)

@spec via(t()) :: Spectre.Action.provider_ref()

Returns the provider identifier encoded in an action effect.

Effects written before provider separation default to the local Elixir provider.

waiting_policy(effect, policy)

@spec waiting_policy(t(), term()) :: t()

Marks an effect as waiting on a policy gate.