Spectre.Server behaviour (Spectre v0.4.0)

Copy Markdown View Source

Application callbacks for Spectre's OTP agent server.

Spectre.Server.Runtime is the actual GenServer callback module. It owns its internal messages and delegates application callbacks in the same process. Helpers build commands on a context; they perform no I/O before return.

use Spectre.Server, prompt_root: "priv/support/prompts"

def handle_flow({:support, :pricing}, _input, ctx) do
  generate(ctx, :pricing, on_result: :pricing)
end

Start with start_link/1; the Agent supplies its server: binding. Services can be modules or {module, function} bindings receiving request, context and options. Consequences pass through the automatically owned GAM domain. The application's :gam options declare permitted operations and persistence; they cannot replace the kernel or its execution protocol.

Mounted Skill Servers run in independent GenServers with local state, workers, streams, pending intentions and checkpoint keys. They automatically reuse the root's same GAM integration, Domain and authority ledger. Mount operation restrictions can only narrow the configured authority; provenance identifies each Skill proposal, and consent cannot cross mounts or exact presentations. See the Skills guide for concurrency and lifecycle behavior.

Summary

Functions

Retrieves an approval by its scoped reference, or by an unambiguous request id.

Records the authenticated human consumer's exact decision and queues its signed delivery.

Reads one page of approval history through the authenticated root consumer.

Records the authenticated consumer's rendering acknowledgement and queues its signed delivery.

Waits for a user answer in the foreground; actor replies use await_reply/3.

Cancels the scheduled intention closure while preserving the GAM consent deadline.

Requests closure of one wait through the audited lifecycle; this is not user rejection or effect cancellation.

Schedules an audited, revision-bound closure with a durable absolute deadline.

Submits an authenticated consumer decision for one exact presentation and mounted Skill.

Delegates input to a mounted Skill; acceptance returns through on_result, output through handle_skill/3.

Acquires a durable result once per event ID through the configured checkpoint.

Returns a redacted GAM page to the configured consumer. Cursors expire when the Domain revision changes.

Returns the authoritative managed GAM projection, without exposing its Scope.

Returns the current read-only view of a foreground or suspended wait by identity.

Returns a redacted durable operational snapshot to the configured consumer. Use skill: id for a mounted Skill.

Acknowledges rendering of one managed presentation from its authenticated consumer.

Stores process-local application data without including it in checkpoints or service requests.

Receives an external approval protocol message through the trusted transport host. Decisions require configured signatures.

Requests storage recovery through the configured consumer.

Starts a fresh agent session, including mounted Skills and its GAM Domain.

Returns a mounted Skill's supervised runtime for native OTP calls or inspection.

Starts an AgentServer with its automatically managed GAM domain.

Returns coordination status, the current session ID and whether a reset is in progress.

Subscribes the configured consumer to post-commit diagnostic invalidations. Read a snapshot after subscribing.

Returns retained coordination facts and a bounded page of correlated GAM records; coverage is explicit.

Callbacks

assigns_schema()

(optional)
@callback assigns_schema() ::
  nil | %{optional(atom()) => Spectre.Server.Contracts.schema()}

code_change(term, t, term)

@callback code_change(term(), Spectre.Server.Context.t(), term()) ::
  {:ok, Spectre.Server.Context.t()} | {:error, term()}

continuation_contracts()

(optional)
@callback continuation_contracts() ::
  nil | %{optional(atom()) => Spectre.Server.Contracts.schema()}

format_status(map)

@callback format_status(map()) :: map()

handle_action(map, t)

@callback handle_action(map(), Spectre.Server.Context.t()) ::
  :default | :confirm | {:confirm, map()} | {:reject, term()}

handle_approval_inbox(map, t)

(optional)
@callback handle_approval_inbox(map(), Spectre.Server.Context.t()) ::
  :default | Spectre.Server.Context.t()

handle_call(term, from, t)

@callback handle_call(term(), GenServer.from(), Spectre.Server.Context.t()) :: tuple()

handle_cast(term, t)

@callback handle_cast(term(), Spectre.Server.Context.t()) :: tuple()

handle_continue(term, t)

@callback handle_continue(term(), Spectre.Server.Context.t()) :: tuple()

handle_flow(term, t, t)

handle_info(term, t)

@callback handle_info(term(), Spectre.Server.Context.t()) :: tuple()

handle_pending(map, t, t)

@callback handle_pending(map(), Spectre.Input.t(), Spectre.Server.Context.t()) ::
  Spectre.Server.Context.t()

handle_result(term, term, t)

@callback handle_result(term(), term(), Spectre.Server.Context.t()) ::
  Spectre.Server.Context.t()

handle_skill(term, term, t)

@callback handle_skill(term(), term(), Spectre.Server.Context.t()) ::
  Spectre.Server.Context.t()

init(term, t)

@callback init(term(), Spectre.Server.Context.t()) ::
  {:ok, Spectre.Server.Context.t()}
  | {:ok, Spectre.Server.Context.t(), term()}
  | :ignore
  | {:stop, term()}

terminate(term, t)

@callback terminate(term(), Spectre.Server.Context.t()) :: term()

Functions

approval(server, reference)

Retrieves an approval by its scoped reference, or by an unambiguous request id.

approval_decide(server, request_id, decision)

@spec approval_decide(
  GenServer.server(),
  binary() | %{id: binary(), from: binary()},
  :confirm | :reject
) :: :ok | {:error, term()}

Records the authenticated human consumer's exact decision and queues its signed delivery.

approval_inbox(server, opts \\ [])

Reads one page of approval history through the authenticated root consumer.

approval_presented(server, request_id)

@spec approval_presented(
  GenServer.server(),
  binary() | %{id: binary(), from: binary()}
) ::
  :ok | {:error, term()}

Records the authenticated consumer's rendering acknowledgement and queues its signed delivery.

assign(ctx, values)

assign(ctx, key, value)

await_input(ctx, id, opts \\ [])

Waits for a user answer in the foreground; actor replies use await_reply/3.

await_reply(ctx, id, opts \\ [])

call_actor(ctx, actor, request, opts \\ [])

cancel(server)

cancel_pending_close(ctx, id, opts \\ [])

Cancels the scheduled intention closure while preserving the GAM consent deadline.

child_spec(opts)

classify(ctx, input, opts \\ [])

close_pending(ctx, id, opts \\ [])

Requests closure of one wait through the audited lifecycle; this is not user rejection or effect cancellation.

close_pending_after(ctx, id, delay_ms, opts \\ [])

Schedules an audited, revision-bound closure with a durable absolute deadline.

The Runtime owns the timer by default. With timer: :application it records the same deadline while the application owns its native OTP wakeup. The recorded schedule identity can guard close_pending/3 through :if_schedule.

confirm_pending(ctx, id)

decide(server, skill, presentation_ref, decision)

@spec decide(GenServer.server(), atom() | nil, String.t(), :confirm | :reject) ::
  {:ok, String.t()} | {:error, term()}

Submits an authenticated consumer decision for one exact presentation and mounted Skill.

delegate(ctx, skill, input, opts \\ [])

Delegates input to a mounted Skill; acceptance returns through on_result, output through handle_skill/3.

delete_private(ctx, key)

deliver(server, event_id, tag, result, opts \\ [])

@spec deliver(GenServer.server(), binary(), term(), term(), keyword()) ::
  :ok | {:error, term()}

Acquires a durable result once per event ID through the configured checkpoint.

session: fences results from external work to the session that requested it. After a reset it is required for callback and pending tags, whose IDs may be reused by the application. Command tags carry runtime-generated unique IDs.

explain_pending(ctx, id)

gam_inspect(server, opts \\ [])

@spec gam_inspect(
  GenServer.server(),
  keyword()
) :: {:ok, map()} | {:error, term()}

Returns a redacted GAM page to the configured consumer. Cursors expire when the Domain revision changes.

gam_view(server)

@spec gam_view(GenServer.server()) :: {:ok, map()} | {:error, term()}

Returns the authoritative managed GAM projection, without exposing its Scope.

generate(ctx, prompt, opts \\ [])

get_pending(ctx, id)

Returns the current read-only view of a foreground or suspended wait by identity.

get_private(ctx, key, default \\ nil)

input(server, input, opts \\ [])

inspect(server, opts \\ [])

@spec inspect(
  GenServer.server(),
  keyword()
) :: {:ok, map()} | {:error, term()}

Returns a redacted durable operational snapshot to the configured consumer. Use skill: id for a mounted Skill.

no_response(ctx)

presented(server, presentation_ref)

@spec presented(GenServer.server(), String.t()) :: :ok | {:error, term()}

Acknowledges rendering of one managed presentation from its authenticated consumer.

propose(ctx, name, args, opts \\ [])

put_private(ctx, values)

put_private(ctx, key, value)

Stores process-local application data without including it in checkpoints or service requests.

receive_approval(server, message)

@spec receive_approval(GenServer.server(), map()) ::
  :ok | {:ok, binary()} | {:error, term()}

Receives an external approval protocol message through the trusted transport host. Decisions require configured signatures.

recover_storage(server, decision)

@spec recover_storage(GenServer.server(), :retry | :memory) :: :ok | {:error, term()}

Requests storage recovery through the configured consumer.

:retry rebuilds the same Domain from its canonical store. :memory starts a fresh Domain and coordination store in RAM, preserving the running agent and session. Earlier governed requests require reconciliation; approvals and Grants from the previous Domain cannot authorize work in the new one.

reject_pending(ctx, id)

reset(server, opts \\ [])

@spec reset(
  GenServer.server(),
  keyword()
) :: :ok | {:error, term()}

Starts a fresh agent session, including mounted Skills and its GAM Domain.

Keeps the root PID, name, identity and configuration. Application state is initialized from the original startup defaults: terminate(:reset, context) releases application-owned resources before init/2 runs again. External memory, business data and previous governance history are retained.

Only the configured consumer may reset the root. Unresolved governed work prevents reset. Storage defaults to its current mode; storage: :ephemeral explicitly selects fresh RAM. Durable resets survive restart using a new persisted generation. RAM sessions are temporary and never silently merged into the previous backend.

Returns after initialization and activation. timeout: bounds reset work (30 seconds by default). After reset, uncorrelated deliver/5 results must include session: from the Context, status/1 or the service context.

resolve_await(ctx, id, result)

respond(ctx, content, opts \\ [])

resume_pending(ctx, id)

route_current_input(ctx, opts \\ [])

skill_server(server, id)

@spec skill_server(GenServer.server(), term()) :: {:ok, pid()} | {:error, term()}

Returns a mounted Skill's supervised runtime for native OTP calls or inspection.

start_link(opts)

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

Starts an AgentServer with its automatically managed GAM domain.

status(server)

@spec status(GenServer.server()) :: map()

Returns coordination status, the current session ID and whether a reset is in progress.

subscribe(server)

@spec subscribe(GenServer.server()) :: :ok | {:error, term()}

Subscribes the configured consumer to post-commit diagnostic invalidations. Read a snapshot after subscribing.

trace(server, interaction, opts \\ [])

@spec trace(GenServer.server(), binary(), keyword()) ::
  {:ok, map()} | {:error, term()}

Returns retained coordination facts and a bounded page of correlated GAM records; coverage is explicit.