🔀Policy DFA
This guide is for Nexus readers who need to understand the reusable onchain policy primitive before reading scheduler code or building a package that gates actions by an ordered trace. It introduces nexus_policy::policy, the nexus_policy::automaton DFA it wraps, and the behavior a caller can rely on when advancing policy state with witness types or object IDs.
Why Nexus uses a DFA-backed policy
Prerequisites
A Move package that can depend on the published
nexus_policyextension.A concrete guarded state transition and a finite set of witness types or object IDs that represent its allowed steps.
The Policy Reference and Automaton Reference open while implementing the package.
A policy describes which sequence of typed events is allowed. In Nexus, those events are Symbol values: either Witness(TypeName) for a Move witness type or Uid(object::ID) for a concrete Sui object. That alphabet lets a policy describe paths across packages without adding direct dependencies between every package that participates in the flow.
Move does not provide a runtime regex engine. automaton.move gives Nexus the programmable form of a regular language: a deterministic finite automaton with a finite state set, finite alphabet, start state, accepting states, and a transition table. Determinism matters because a caller with the current state and next symbol has exactly one next state. There is no ambiguous branch for an untrusted executor to reinterpret.
Keeping the policy onchain also keeps the allowed trace beside the guarded asset or process. Offchain code can drive the flow by submitting symbols, but the policy object decides whether the trace remains in the accepted language.
How the automaton stores the accepted language
nexus_policy::automaton::DeterministicAutomaton<State, Symbol> stores states, alphabet symbols, a dense transition matrix, an accepting bitmap, and a start index in TableVec storage. Construction checks that state and alphabet entries are unique, that the transition table has exactly one successor for every (state, symbol) pair, that every successor exists in the state basis, and that the start and accepting states exist.
Evaluation is constant-shape: delta_indexed reads the transition row for the current state index and the column for the symbol index. run, run_from, and accepts fold that same transition logic over a word. If a state or symbol is outside the basis, helper functions abort instead of silently treating the input as allowed.
ConfiguredAutomaton wraps a DFA with a UID, so callers can attach transition metadata as dynamic fields. Metadata can be registered for a specific (state, symbol) transition or as a wildcard (*, symbol). Lookup checks the specific transition first and falls back to the wildcard entry. Missing metadata aborts when borrowed.
How the policy wrapper adds typed symbols
nexus_policy::policy::Policy<T> fixes the automaton state type to u64 and the symbol type to Policy::Symbol, then stores caller-defined payload data of type T. It exposes the current state index, the current state value, the underlying DFA, transition metadata registration, accepting-state checks, and reset.
The main advancement functions are:
Each function resolves the typed symbol, checks that the symbol is in the alphabet, then applies the DFA transition to update state_index. is_accepting tells callers whether the current state is accepting. reset returns the policy to the start state while keeping its configured automaton and payload.
new_linear builds a prefix-enforcing DFA from a sequence of symbols. The generated states are 0..sequence_length, and the accepting state is the final index. The expected symbol at each step advances to the next state. Other in-alphabet symbols self-loop at the current state. Symbols outside the alphabet abort. If a package needs hard failure for a symbol that is otherwise in the alphabet, it should configure an explicit non-accepting sink state instead of relying on new_linear self-loops.
Concrete example: ordered approval trace
The smallest useful policy is an ordered trace. Imagine a package that lets an asset leave escrow only after the same request has been drafted, reviewed, and approved. The package can represent each step with a zero-sized witness type and build a new_linear policy over those witness symbols. The example below is written as a test helper so it can clean up the key Policy object with policy::destroy_policy; production code would store the policy inside the guarded object or share the policy object instead.
The resulting state trace is concrete:
none
start
0
no
Drafted {}
0
1
no
Reviewed {}
1
2
no
Approved {}
2
3
yes
If the caller submits Reviewed {} while still at state 0, new_linear treats it as an in-alphabet but out-of-position symbol and keeps the policy at state 0. If the caller submits a witness type that was not included in sequence, advance_with_witness aborts with the policy module's symbol-not-in-alphabet error. That distinction is why new_linear is useful for “wait until the right ordered trace appears” flows, while stricter protocols should define a custom DFA with an explicit rejecting sink state.
Build and verify
Build and test the package that owns the policy before publishing it. Verify the initial state, every expected transition, an out-of-position in-alphabet symbol, and an unknown symbol. Treat an abort or a non-accepting state as a boundary: correct the caller's trace or define an explicit rejecting transition; do not bypass is_accepting to force the guarded side effect.
Relationship to scheduling
nexus_policy is an optional standalone DFA and dynamic-configuration extension. The scheduler lifecycle does not require queue/periodic generator witnesses or two policy planes: it manages durable Task, Occurrence, recurrence, dispatch, settlement, and close transitions directly. Use a policy only when your own package needs an additional ordered transition guard; do not infer that every scheduled Task embeds one.
How to design a policy-backed flow
Define the alphabet as witness types or concrete object IDs that represent the steps you want to allow.
Build the automaton with the state set, start state, accepting states, and transition table that encode the allowed traces.
Store any per-symbol or per-transition configuration that the guarded flow needs.
Call
advance_with_witnessoradvance_with_uidbefore the side effect that depends on the trace.Gate final payout, completion, reserve collection, or state transition with
is_accepting.Reset only when the guarded process intentionally starts a new run.
For a linear approval flow, the policy alphabet might contain Witness(Draft), Witness(Reviewed), and Witness(Approved). The accepting state is reached only after those symbols arrive in order. For object routing, the alphabet can contain Uid(A), Uid(B), and Uid(C) so a package can enforce that specific objects were visited in a required sequence.
Next
Apply the policy only at the guarded state transition, then return to DAG and Execution to connect the policy boundary to workflow state.
Last updated

