Spectre.GAM (Spectre v0.4.0)

Copy Markdown View Source

Public boundary for the governed-act kernel imported from Spectre 0.4.

Agent authoring lives in Spectre.Agent and Spectre.Server. This facade is for the host's authority, observation and governed execution operations; it is not the agent authoring DSL or a hidden conversational loop.

lib/spectre/gam/ owns namespaced GAM records, kernel, durable runtime, persistence and boundary contracts. This module is the facade over that governed runtime; it coexists with the conversational runtime and its service bindings. Agent authoring is optional: callers can construct core records and use this facade without adopting the DSL.

Host code wires one explicit ledger Store into a supervised Domain, an authenticated ingress adapter supplies a Spectre.GAM.SubmissionContext, and callers durably open a Scope before proposing a Candidate. propose/3 keeps Admission and execution in one call without returning the internal Grant through this facade. Internal modules are callable on the same BEAM; @doc false is not access control. Deployment isolation and credential protection remain host responsibilities.

Administrative helpers only construct Candidates and submit them through the same kernel. This module exposes no direct ledger append, raw Grant, arbitrary observed-Evidence insertion or post-Genesis root-authority mutation.

Minimal flow

{:ok, domain} =
  Spectre.GAM.start_domain("domain:payments", store,
    ingress: MyApp.Ingress,
    constitution: constitution,
    genesis: genesis,
    principals: principals,
    host_profile: host_profile,
    surface: surface,
    root_mandates: root_mandates,
    genesis_verifier: MyApp.GenesisVerifier,
    executors: [MyApp.PaymentExecutor],
    broker: MyApp.CredentialBroker
  )

{:ok, context} = Spectre.GAM.authenticate(domain, scope_ref, request)
{:ok, scope} = Spectre.GAM.open_scope(domain, context, opened_at: now)
{:ok, result} = Spectre.GAM.propose(scope, candidate)

A Scope and Domain handle are routing values, not authority. Copying either does not copy a Mandate, reservation or capability.

See Spectre.GAM.GovernedAct for a map of the pure model implementation and its relationship to the ledger, kernel, projection and audit drivers. The HexDocs guides cover host setup, the Agent/Skill DSL, input and streaming, storage/recovery and current capability limits. In particular, turn/3 records input and proposes; propose/3 crosses the governed effect boundary. Neither is a built-in media loop or a general ask/stream/cancel engine.

Summary

Functions

Returns immutable Acts visible through an authenticated Scope.

Authenticates through the Domain's fixed ingress and returns a generation-bound context.

Returns the current capability-free authority inventory for an authenticated Scope.

Adds already-durable Evidence to a sealed deliberation context.

Returns governed Evidence declassifications visible through an authenticated Scope.

Removes explicitly authorized labels by producing new immutable Evidence.

Returns an exact governed Definition through a live authenticated Scope.

Delegates a subtractive child Mandate through a normal governance Act.

Returns declared free child Meter balances through a governance Act.

Reads durable dispatch facts for one scoped Act, never inferring cancellation from a missing Attempt.

Disposes a Duty only through the independently authorized governance path.

Returns immutable Duties visible through an authenticated Scope.

Returns causal erasure requests visible through an authenticated Scope.

Returns the verified ledger head represented by the live Domain projection.

Issues a subtractive child Mandate through a normal governance Act.

Returns the locally supervised Domain handle for domain_ref.

Records input through the Domain's declared ingress without invoking a mind.

Durably opens a Scope from an authenticated SubmissionContext.

Opens a finite Work or Vigil Scope through normal governed admission.

Records ingress Evidence and returns its sealed Turn without invoking a Mind.

Stores immutable consent material; this operation does not itself display it.

Proposes a Candidate and completes its permitted execution path.

Records derived/generated Evidence bound to one exact deliberation Turn.

Obtains and records a late world observation through the Domain's fixed observer.

Records a late or independently obtained world Outcome for an Act in this Scope.

Refreshes available durable result facts without calling an executor again.

Registers an immutable Principal through a normal governance Act; it grants no authority.

Requests executor-mediated erasure through a normal governance Act.

Proposes disposition of an AgentServer Duty through its independent reviewer.

Replaces a Mandate with an immutable, strictly narrower successor through a governance Act.

Rebinds a current authenticated context to its exact durable Scope opening.

Revises a declarative Definition through a normal governance Act.

Revises the attested host profile through a normal governance Act.

Revises the governed Surface through a normal governance Act.

Revokes an AgentServer's application mandate through the existing GAM kernel.

Immediately records a forward-only Mandate revocation through a governance Act.

Shows prepared consent material through a normal executor-mediated Act.

Starts one locally supervised Domain using an explicit ledger Store.

Submits an operation from a trusted application process to an AgentServer.

Reads the current Surface through the live Scope fence; does not copy Domain history.

Turns authenticated input into durable Evidence and capability-free proposals.

Runs one Turn with explicit, non-authoritative application state.

Returns the governed runtime API version.

Returns the read-only application projection for an authenticated Scope.

Types

domain_head()

@type domain_head() :: %{
  domain_ref: domain_ref(),
  revision: non_neg_integer(),
  head_digest: String.t()
}

domain_input()

@type domain_input() :: Spectre.GAM.Domain.t() | domain_ref()

domain_ref()

@type domain_ref() :: String.t()

Functions

acts(scope, opts \\ [])

@spec acts(
  Spectre.GAM.Scope.t(),
  keyword()
) :: {:ok, [Spectre.GAM.Act.t()]} | {:error, term()}

Returns immutable Acts visible through an authenticated Scope.

authenticate(domain_input, scope_ref, input, opts \\ [])

@spec authenticate(domain_input(), String.t(), term(), keyword()) ::
  {:ok, Spectre.GAM.SubmissionContext.t()} | {:error, term()}

Authenticates through the Domain's fixed ingress and returns a generation-bound context.

authority(scope, opts \\ [])

@spec authority(
  Spectre.GAM.Scope.t(),
  keyword()
) :: {:ok, Spectre.GAM.Authority.View.t()} | {:error, term()}

Returns the current capability-free authority inventory for an authenticated Scope.

The result lists Mandates held by the authenticated Principal, their shared state blockers and retained revocation controls. It does not decide whether any particular Candidate is admissible and cannot be used as a Grant.

continue_turn(scope, turn, evidence_refs)

Adds already-durable Evidence to a sealed deliberation context.

Rechecks Scope, seal, provenance and availability; no input is observed twice. Required context cannot be silently dropped to fit a smaller Mind window.

declassifications(scope)

@spec declassifications(Spectre.GAM.Scope.t()) ::
  {:ok, [Spectre.GAM.Declassification.t()]} | {:error, term()}

Returns governed Evidence declassifications visible through an authenticated Scope.

declassify_evidence(scope, evidence, removed_labels, candidate_attrs, opts \\ [])

@spec declassify_evidence(
  Spectre.GAM.Scope.t(),
  Spectre.GAM.Evidence.t() | map() | keyword(),
  [term()],
  map() | keyword(),
  keyword()
) :: {:ok, Spectre.GAM.Proposal.Result.t()} | {:error, term()}

Removes explicitly authorized labels by producing new immutable Evidence.

definition(scope, ref)

@spec definition(Spectre.GAM.Scope.t(), String.t()) ::
  {:ok, Spectre.GAM.Definition.t()} | {:error, term()}

Returns an exact governed Definition through a live authenticated Scope.

delegate_mandate(scope, mandate, candidate_attrs, opts \\ [])

@spec delegate_mandate(
  Spectre.GAM.Scope.t(),
  Spectre.GAM.Mandate.t() | map() | keyword(),
  map() | keyword(),
  keyword()
) :: {:ok, Spectre.GAM.Proposal.Result.t()} | {:error, term()}

Delegates a subtractive child Mandate through a normal governance Act.

devolve_mandate(scope, child_mandate_ref, amounts, candidate_attrs, opts \\ [])

@spec devolve_mandate(
  Spectre.GAM.Scope.t(),
  String.t(),
  map(),
  map() | keyword(),
  keyword()
) ::
  {:ok, Spectre.GAM.Proposal.Result.t()} | {:error, term()}

Returns declared free child Meter balances through a governance Act.

dispatch_status(scope, act_ref)

Reads durable dispatch facts for one scoped Act, never inferring cancellation from a missing Attempt.

dispose_duty(scope, disposition, candidate_attrs, opts \\ [])

@spec dispose_duty(
  Spectre.GAM.Scope.t(),
  Spectre.GAM.Duty.Disposition.t() | map() | keyword(),
  map() | keyword(),
  keyword()
) :: {:ok, Spectre.GAM.Proposal.Result.t()} | {:error, term()}

Disposes a Duty only through the independently authorized governance path.

duties(scope, opts \\ [])

@spec duties(
  Spectre.GAM.Scope.t(),
  keyword()
) :: {:ok, [Spectre.GAM.Duty.t()]} | {:error, term()}

Returns immutable Duties visible through an authenticated Scope.

erasures(scope)

@spec erasures(Spectre.GAM.Scope.t()) ::
  {:ok, [Spectre.GAM.Erasure.t()]} | {:error, term()}

Returns causal erasure requests visible through an authenticated Scope.

head(domain_input)

@spec head(domain_input()) :: {:ok, domain_head()} | {:error, term()}

Returns the verified ledger head represented by the live Domain projection.

issue_mandate(scope, mandate, candidate_attrs, opts \\ [])

@spec issue_mandate(
  Spectre.GAM.Scope.t(),
  Spectre.GAM.Mandate.t() | map() | keyword(),
  map() | keyword(),
  keyword()
) :: {:ok, Spectre.GAM.Proposal.Result.t()} | {:error, term()}

Issues a subtractive child Mandate through a normal governance Act.

lookup_domain(domain_ref)

@spec lookup_domain(domain_ref()) :: {:ok, Spectre.GAM.Domain.t()} | {:error, term()}

Returns the locally supervised Domain handle for domain_ref.

observe(scope, input, opts \\ [])

@spec observe(Spectre.GAM.Scope.t(), term(), keyword()) ::
  {:ok, [Spectre.GAM.Evidence.t()]} | {:error, term()}

Records input through the Domain's declared ingress without invoking a mind.

:ingress_opts are passed to that adapter. Supplying an :ingress override is rejected: call options cannot replace the Domain trust boundary.

open_scope(domain_input, context, opening_attrs \\ [])

@spec open_scope(
  domain_input(),
  Spectre.GAM.SubmissionContext.t(),
  map() | keyword() | Spectre.GAM.Scope.Opening.t()
) :: {:ok, Spectre.GAM.Scope.t()} | {:error, term()}

Durably opens a Scope from an authenticated SubmissionContext.

The context must already have been produced by a trusted ingress adapter. It is revalidated, bound to the exact Domain and scope reference, fenced by the currently running host generation and recorded before the handle is returned. opening_attrs contains only Scope semantics; identity and authentication fields are forced from the context. :opened_at is required so exact retries remain byte-identical. Opening a Scope creates no authority or Meter balance.

open_scope(parent_scope, child_context, opening_attrs, candidate_attrs, opts \\ [])

@spec open_scope(
  Spectre.GAM.Scope.t(),
  Spectre.GAM.SubmissionContext.t(),
  map() | keyword(),
  map() | keyword(),
  keyword()
) :: {:ok, Spectre.GAM.Scope.t()} | {:error, term()}

Opens a finite Work or Vigil Scope through normal governed admission.

parent_scope supplies the authenticated proposing context. child_context must be a current context produced by the same Domain ingress for the new Scope reference. Its seal is verified separately by the Sequencer and every durable identity field is forced into the Candidate consequence. The Scope handle is returned only after the Decision, Act and Opening are durably recovered from one atomic batch.

open_turn(scope, input, opts \\ [])

@spec open_turn(Spectre.GAM.Scope.t(), term(), keyword()) ::
  {:ok, Spectre.GAM.Mind.Turn.t()} | {:error, term()}

Records ingress Evidence and returns its sealed Turn without invoking a Mind.

This is the same preparation used by turn/3, exposed for host-controlled handler authoring. It does not submit a Candidate or replace the configured ingress. In particular, routing an input cannot make its contents trusted.

prepare_presentation(scope, presentation, opts \\ [])

@spec prepare_presentation(
  Spectre.GAM.Scope.t(),
  Spectre.GAM.Presentation.t() | map() | keyword(),
  keyword()
) :: {:ok, Spectre.GAM.Presentation.t()} | {:error, term()}

Stores immutable consent material; this operation does not itself display it.

propose(scope, candidate, opts \\ [])

@spec propose(
  Spectre.GAM.Scope.t(),
  Spectre.GAM.Candidate.t() | map() | keyword(),
  keyword()
) ::
  {:ok, Spectre.GAM.Proposal.Result.t()} | {:error, term()}

Proposes a Candidate and completes its permitted execution path.

The Scope is revalidated against the live Domain and current host generation on every call. Admission is durably sequenced before Spectre.GAM.Attempt.Runner receives the internal response. Executor-mediated Acts are routed only to the executor and broker fixed in Domain configuration; proposal options cannot replace either implementation or their private options.

The returned Spectre.GAM.Proposal.Result contains the primary durable result and, for an explicitly refused Candidate, either declared silence or the result of one fresh governed fallback Candidate. It never contains the internal Grant or checked-out capability, and fallbacks never recurse.

record_derivation(scope, turn, evidence, opts \\ [])

@spec record_derivation(
  Spectre.GAM.Scope.t(),
  Spectre.GAM.Mind.Turn.t(),
  Spectre.GAM.Evidence.t() | map() | keyword(),
  keyword()
) :: {:ok, Spectre.GAM.Evidence.t()} | {:error, term()}

Records derived/generated Evidence bound to one exact deliberation Turn.

record_late_observation(scope, attempt_ref, input, opts \\ [])

@spec record_late_observation(Spectre.GAM.Scope.t(), String.t(), term(), keyword()) ::
  {:ok,
   %{evidence: [Spectre.GAM.Evidence.t()], outcome: Spectre.GAM.Outcome.t()}}
  | {:error, term()}

Obtains and records a late world observation through the Domain's fixed observer.

The observer may verify a receipt or query the external system, but it receives no execution capability. A request cannot replace the configured adapter.

record_outcome(scope, outcome, opts \\ [])

@spec record_outcome(
  Spectre.GAM.Scope.t(),
  Spectre.GAM.Outcome.t() | map() | keyword(),
  keyword()
) ::
  {:ok, Spectre.GAM.Outcome.t()} | {:error, term()}

Records a late or independently obtained world Outcome for an Act in this Scope.

recover_proposal(scope, proposal)

Refreshes available durable result facts without calling an executor again.

register_principal(scope, principal, candidate_attrs, opts \\ [])

@spec register_principal(
  Spectre.GAM.Scope.t(),
  Spectre.GAM.Principal.t() | map() | keyword(),
  map() | keyword(),
  keyword()
) :: {:ok, Spectre.GAM.Proposal.Result.t()} | {:error, term()}

Registers an immutable Principal through a normal governance Act; it grants no authority.

request_erasure(scope, request_attrs, candidate_attrs, opts \\ [])

@spec request_erasure(
  Spectre.GAM.Scope.t(),
  map() | keyword(),
  map() | keyword(),
  keyword()
) :: {:ok, Spectre.GAM.Proposal.Result.t()} | {:error, term()}

Requests executor-mediated erasure through a normal governance Act.

resolve_duty(server, duty_ref, kind, opts \\ [])

@spec resolve_duty(
  GenServer.server(),
  binary(),
  Spectre.GAM.Duty.Disposition.kind(),
  keyword()
) ::
  {:ok, Spectre.GAM.Proposal.Result.t()} | {:error, term()}

Proposes disposition of an AgentServer Duty through its independent reviewer.

Only the process configured in gam: [reviewer: [identity: ..., consumer: pid]] may call this function. The normal agent consumer and external submitters do not gain reviewer authority. Spectre builds the proposal against the exact current Duty opening; the kernel checks its authority, supporting records and independence before recording any disposition.

:supporting_refs must name nonempty existing Evidence, Outcome or Act refs. :meter_resolution defaults to :none. Disposition retains the original cause and history; it does not turn an uncertain effect into a successful one.

restrict_mandate(scope, predecessor_ref, successor, candidate_attrs, opts \\ [])

@spec restrict_mandate(
  Spectre.GAM.Scope.t(),
  String.t(),
  Spectre.GAM.Mandate.t() | map() | keyword(),
  map() | keyword(),
  keyword()
) :: {:ok, Spectre.GAM.Proposal.Result.t()} | {:error, term()}

Replaces a Mandate with an immutable, strictly narrower successor through a governance Act.

resume_scope(domain_input, context)

@spec resume_scope(domain_input(), Spectre.GAM.SubmissionContext.t()) ::
  {:ok, Spectre.GAM.Scope.t()} | {:error, term()}

Rebinds a current authenticated context to its exact durable Scope opening.

Authentication and host generation must still match the immutable opening. A changed generation or authentication requires a new Scope, not an implicit rewrite of the old session or its authority.

revise_definition(scope, definition, candidate_attrs, opts \\ [])

@spec revise_definition(
  Spectre.GAM.Scope.t(),
  Spectre.GAM.Definition.t() | map() | keyword(),
  map() | keyword(),
  keyword()
) :: {:ok, Spectre.GAM.Proposal.Result.t()} | {:error, term()}

Revises a declarative Definition through a normal governance Act.

revise_host_profile(scope, previous_ref, profile, candidate_attrs, opts \\ [])

@spec revise_host_profile(
  Spectre.GAM.Scope.t(),
  String.t(),
  Spectre.GAM.HostProfile.t() | map() | keyword(),
  map() | keyword(),
  keyword()
) :: {:ok, Spectre.GAM.Proposal.Result.t()} | {:error, term()}

Revises the attested host profile through a normal governance Act.

revise_surface(scope, previous_ref, surface, candidate_attrs, opts \\ [])

@spec revise_surface(
  Spectre.GAM.Scope.t(),
  String.t(),
  Spectre.GAM.Surface.t() | map() | keyword(),
  map() | keyword(),
  keyword()
) :: {:ok, Spectre.GAM.Proposal.Result.t()} | {:error, term()}

Revises the governed Surface through a normal governance Act.

revoke(server)

@spec revoke(GenServer.server()) ::
  {:ok, Spectre.GAM.Proposal.Result.t()} | {:error, term()}

Revokes an AgentServer's application mandate through the existing GAM kernel.

Only the root Server's configured consumer may request revocation. This is forward-only: existing Acts, Outcomes and Duties remain in the ledger. A restart does not restore the revoked authority.

revoke_mandate(scope, mandate_ref, candidate_attrs, opts \\ [])

@spec revoke_mandate(
  Spectre.GAM.Scope.t(),
  String.t(),
  map() | keyword(),
  keyword()
) :: {:ok, Spectre.GAM.Proposal.Result.t()} | {:error, term()}

Immediately records a forward-only Mandate revocation through a governance Act.

show_presentation(scope, presentation_ref, candidate_attrs, opts \\ [])

@spec show_presentation(
  Spectre.GAM.Scope.t(),
  String.t(),
  map() | keyword(),
  keyword()
) :: {:ok, Spectre.GAM.Proposal.Result.t()} | {:error, term()}

Shows prepared consent material through a normal executor-mediated Act.

start_domain(domain_ref, store, opts \\ [])

@spec start_domain(domain_ref(), Spectre.GAM.Ledger.Store.config(), keyword()) ::
  {:ok, Spectre.GAM.Domain.t()} | {:error, term()}

Starts one locally supervised Domain using an explicit ledger Store.

This is host wiring, not a governance mutation. The Store is never inferred or created implicitly. Bootstrap records, clock, identifier source, constitution and Grant secret are forwarded as Domain options and validated by the Sequencer. :ingress is mandatory and fixes the only adapter reference accepted in authenticated contexts for that running Domain. :domain_ref, :store, :name and :registry are reserved so callers cannot silently replace the identity or uniqueness boundary.

This function starts Domains below Spectre.GAM's Domain supervisor coupled to the unique Domain registry. For static host supervision, use the existing Spectre.GAM.Domain.child_spec/1 instead; do not start the same Domain in both trees.

:recovery_policy optionally supplies {module, options} implementing Spectre.GAM.Domain.Recovery.Policy: an external witness check after verified recovery, and an optional failure notification for application-owned repair. :recovery_timeout bounds each callback (default 5 seconds). No policy can bypass ledger or governed-act validation.

:clock_tolerance_ms bounds wall-clock drift from a boot-local monotonic anchor (default 60,000 ms). :infinity explicitly disables the fence for host-managed/virtual clocks. It cannot verify a bad initial clock reading.

submit(server, operation, arguments, opts \\ [])

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

Submits an operation from a trusted application process to an AgentServer.

This uses the same operation catalog, handle_action/2, GAM admission, confirmation and result callbacks as Spectre.Server.propose/4. The caller must be the Server's configured consumer or one of its startup :submitters PIDs. Submission never constitutes user consent.

Returns {:ok, command_id} after acquisition, not after execution. The result is delivered to handle_result/3 under :on_result (default :submission). :on_result, :confirmation and an optional :key are accepted. A busy Server returns {:error, :busy} without acquiring an operation; callers may retry that rejected submission. An acquired operation must not be blindly retried. Mounted Skills receive work through their parent rather than this API.

A :key is a nonempty string of at most 256 bytes. Retrying the same key and request returns the original command ID; changing the request returns a conflict. Keys are acquired atomically with the command checkpoint and are retained across recovery. Each Server retains up to 4096 keys and refuses new keyed submissions when full; it never evicts keys and silently reexecutes. The guarantee follows the configured storage lifetime, not external delivery.

surface(scope)

@spec surface(Spectre.GAM.Scope.t()) ::
  {:ok, Spectre.GAM.Surface.t()} | {:error, term()}

Reads the current Surface through the live Scope fence; does not copy Domain history.

turn(scope, input, opts \\ [])

@spec turn(Spectre.GAM.Scope.t(), term(), keyword()) ::
  {:ok,
   %{
     turn: Spectre.GAM.Mind.Turn.t(),
     evidence: [Spectre.GAM.Evidence.t()],
     candidates: [Spectre.GAM.Candidate.t()]
   }}
  | {:error, term()}

Turns authenticated input into durable Evidence and capability-free proposals.

The mind fixed in Domain configuration receives each Turn. The Domain's declared ingress first observes the raw input under the Scope's trusted SubmissionContext; Spectre.GAM verifies the resulting Evidence binding and commits it before invoking the mind. An :ingress option is rejected, while :ingress_opts are passed to the fixed adapter. The mind receives no executor or authority view and returns only Candidates. Callers may submit those Candidates separately with propose/3.

Optional :context_evidence_refs select additional already-durable Evidence for deliberation. Labels on the Turn are the conservative union of all selected Evidence.

turn(scope, input, state, opts)

@spec turn(Spectre.GAM.Scope.t(), term(), term(), keyword()) ::
  {:ok,
   %{
     turn: Spectre.GAM.Mind.Turn.t(),
     evidence: [Spectre.GAM.Evidence.t()],
     candidates: [Spectre.GAM.Candidate.t()],
     state: term()
   }}
  | {:error, term()}

Runs one Turn with explicit, non-authoritative application state.

This is the explicit-state counterpart of turn/3: the supplied value is passed only to the optional Spectre.GAM.Mind.deliberate/3 callback and its successor is returned to the caller. Spectre.GAM neither persists that value nor treats it as authority, Evidence or a durable execution fact. Minds without the optional callback use deliberate/2 and return the value unchanged. Like turn/3, this call records ingress Evidence before deliberation; it is not a pure function and Mind failure does not roll back those observations.

A stateful Mind must define deliberate/2 and deliberate/3 as separate functions. A default argument on /3 would generate a misleading /2 that interprets ordinary Mind options as state; see Spectre.GAM.Mind.

version()

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

Returns the governed runtime API version.

view(scope)

@spec view(Spectre.GAM.Scope.t()) ::
  {:ok, Spectre.GAM.Scope.View.t()} | {:error, term()}

Returns the read-only application projection for an authenticated Scope.