ByzantineSystems.Automata
Search Results for

    Public API guide

    This guide summarizes the implemented public surface. The source XML comments remain the detailed contract for individual fields, union cases, and members.

    Core

    ByzantineSystems.Automata.Core defines validated StateId, MachineId, and phantom-typed EntityId<'entity> identifiers with create and value functions. The auto-open helpers stateId, machineId, and entityId are shorthand constructors.

    A Chart<'State,'Event,'Action,'Err> can be created directly with Chart.create or with the statechart computation expression. The expression supports:

    • Top-level root and classify declarations.
    • state and compound nodes, with initial and terminal markers.
    • onEntry and onExit actions.
    • Closure rules: on, attempt, goto, and internalOn.
    • Condition rules: onWhen, attemptWhen, gotoWhen, and internalWhen.

    Both construction paths validate the hierarchy and return Result<Chart<_,_,_,_>, ChartError list>. A goto naming a node the chart does not declare is refused there, as UnknownGotoTarget, rather than on the first event that reaches it. Chart.resolve is pure and returns either a Resolution or a typed TransitionError. Query functions expose the root, classifier, nodes, children, and terminal or compound status.

    A state or compound block is a NodeDraft value, so fragments are reusable: yield one wherever it is needed, splice a list with yield! (in a chart or inside a compound), and give a second copy of the same fragment its own ids with Fragment.prefix, which renames every node the fragment declares along with the initial children and goto targets that name them. The classifier is the host's code and must return the prefixed ids; see chart fragments.

    Condition builds normalized boolean syntax around named predicates. Condition.matches evaluates it and Condition.syntax exposes its function-free form. Predicate names are semantic identifiers; delegate behavior stays opaque.

    The lower-level Rule module keeps the closure constructors and adds transitionWhen, attemptWhen, gotoWhen, internalWhen, and guardedWhen. Rule.syntax exposes normalized RuleSyntax; Rule.kind remains the compatibility combinator projection. Codec<'T> and its module functions provide typed store boundaries.

    Chart.fingerprint hashes the root, node structure, rule order, and normalized rule and condition syntax. Sibling order is normalized; rule order is retained.

    The fingerprint describes declared syntax, not behavior. Predicate delegates, transforms, entry and exit actions, and classifiers remain opaque. Reusing a predicate name for different behavior is a broken caller contract, and replay correctness still requires a new ChartVersion for any behavior change.

    ChartEquivalence.trace runs an event sequence and observes each successful resolution as a typed action list, classified leaf, and terminal flag. It stops at the first typed TransitionError and returns that error with the successful prefix. equivalentOn compares traces over caller-supplied samples and explicit action/error equality functions; finite samples are evidence, never proof. declaredEquivalent compares canonical declarations directly, ignoring sibling order and opaque callbacks. Declaration equality and sampled behavioral agreement are intentionally separate questions.

    Runtime

    The machine computation expression creates a Machine<'EntityId,'State,'Event,'Action,'Err> from a machine id and declarations. A chart, a declared chartVersion, an initial state and an IMachineStore are required; processor, onTransition, logger and timeProvider are optional, and the builder returns every MachineConfigError at once rather than the first. The action queue is not declared here: the store owns it, so it is named once, where it is created.

    A machine no longer processes anything. Machine.enqueue records an event in the durable inbox and returns a SubmissionOutcome; some worker, here or on another host, claims it and finishes it. Machine.commandResult reads what became of a command. Machine.send is enqueue plus a wait for the durable outcome, and remains the headline call because it is what most callers want: the documentation states plainly that it now costs a round trip through the database, and points throughput-sensitive callers at enqueue and commandResult. Machine.state reads the current snapshot.

    Machine.startAsync takes an IChartRegistry, boots the store when it offers IStoreBoot, then registers the chart's structure against its declared version before any work begins. It answers Startup.Started with the registration, or Startup.Refused with the BootDefects that stopped it, in which case nothing was registered or started. Registered and Matched proceed; Mismatched is reported as data, because whether it is fatal is the host's decision. When the store offers IWorkNotifications, the machine also listens for work announced by other processes, and a failed listener is retried on the polling interval rather than faulting anything.

    Machine.processor builds the CommandProcessor that drains the inbox: claim a batch, resolve each command against the chart with pure Chart.resolve, and finalize it. At most one non-terminal command per entity is claimable, so a claim already excludes that entity across every process; the processor never serialises anything itself and never needs to know which entities exist. A batch therefore cannot contain two commands for one entity, which is why processing it concurrently cannot reorder anything. PollAsync does one round and reports a PollReport; RunAsync loops and answers ProcessorStop.Drained or ProcessorStop.Escalated, so a supervisor can tell an orderly shutdown from a worker that stopped because something is wrong.

    ProcessorPolicy carries the batch size, lease, renewal point, polling interval, concurrency, attempt limit, backoff envelope, and Classify: the one decision the library cannot make for an application, which is what a failure means for the command that caused it. Machine construction validates the raw batch and lease once into ClaimBatch and LeaseDuration; a batch outside 1 through 1000 or a lease outside the positive provider-safe range fails before any worker polls. ProcessorPolicy.defaultClassify retries transient infrastructure failures, records a chart's own refusal as a domain error, and dead-letters everything else with a machine-level reason rather than retrying it, because an event the chart has no rule for will not acquire one by waiting. Dead-lettering releases the entity, which matters more here than anywhere else: a command that keeps failing would otherwise hold up every command behind it for that entity, forever.

    Machine.dispatcher builds the ActionDispatcher that delivers what a commit queued, through an ActionHandler that receives the whole LeasedAction so a destination can deduplicate on (CommandId, Ordinal). A handler that throws is logged with its exception; one that reports an Error is logged as having failed, without the value, which is the application's own.

    Both refuse poison: work delivered more often than MaxAttempts is dead-lettered or abandoned without being run. The ordinary retry path cannot get there, because a reported failure raises the attempt count along with the delivery count; only deliveries that ended with no outcome at all, a handler that took its process down or ran past its lease, push past it, and nothing else would ever stop them.

    Resilience

    TransientPolicy describes short-horizon retry, per-attempt and total timeouts, and optional circuit breaking, for the I/O one machine performs. Durable retry is not here: a command that fails is rescheduled in the inbox, with a backoff the database computes, and keeps its place in its entity's order while it waits.

    TransientPolicy.toPipeline takes the policy, a name for telemetry, a predicate saying which of a driver's exceptions are worth repeating, and an event sink. The predicate is a parameter because only a store knows which of its exceptions mean "again in a moment" and which mean "never"; this assembly has no reference to any driver. The pipeline it returns is applied inside the store adapter, around the driver call, because Polly reads exceptions and everything above that line reads Result. A pipeline wrapped outside the conversion would retry nothing.

    SupervisorSpec and the Supervisor module model and validate supervision policy. Supervisor.start returns an ISupervisor whose Completion observes every child, and whose Events/EventsAsync expose the ordered supervision event log for audit persistence.

    DependencyInjection

    AddAutomata registers a BackgroundService from typed AutomataOptions. AutomataSupervisorOptions.defaults name fills in the supervisor, and Actions says whether this host delivers the machine's actions: ActionDelivery.registered<'EntityId, 'Action, 'EffectError> resolves the registered IActionHandler per action, and ActionDelivery.Elsewhere leaves delivery to another fleet. It is a union rather than a flag because the handler's type is what fixes 'EffectError; with nothing mentioning it, F# would infer obj and the host would ask for a handler nobody registered. A generation's workers start only after its machine has started, so none polls a queue its boot has not created yet. The root supervisor owns machine generations, each generation runs a CommandProcessor and optionally an ActionDispatcher, and an ISupervisionEventStore receives ordered audit records. A generation that reports ProcessorStop.Escalated faults, which is what a supervisor restarts on. AutomataSupervisorOptions carries the restart strategy, kind, intensity, period and shutdown budget. IActionHandler is the scoped delivery contract, resolved from a fresh scope per action. InMemorySupervisionStore keeps one process's restart history for a host that has not registered a durable writer; PostgresSupervisionStore is the durable one.

    No resilience pipeline is registered here. A host configures short-horizon retry when it builds its PostgresContext and durable retry through the machine's processor policy.

    A store that refuses to boot faults the generation with AutomataBootRefusedException, the same way a fingerprint mismatch does.

    AddAutomataMaintenance registers MaintenanceService once per database: announcements every NotifyEvery, and a pass every RunEvery that reaps leases, applies retention and reports drift. Its raw Batch option is validated once into a ClaimBatch, so values outside 1 through 1000 fail during registration rather than on the first pass. MaintenanceScheduler.InDatabase asks pg_cron to run both ticks and falls back to InProcess when it cannot, because maintenance is not optional; while the database runs them, the service still checks drift so it reaches ILogger. Drift is repaired only with RepairDrift = true. A failed tick is logged and waited out; a loop that faults with something not understood stops its sibling and faults the service.

    Storage

    Required

    ByzantineSystems.Automata.Storage is four required interfaces. ICommandInbox submits commands idempotently, leases runnable ones with a ClaimBatch and LeaseDuration, and finishes a lease through fenced writes that require the LeaseToken<CommandWork> the claim issued; at most one non-terminal command per entity is claimable, so a claim excludes that entity across every process rather than only within one. IStateReader reads the current Snapshot and pages the transition log. A Page carries an exclusive Epoch cursor rather than an offset, which is what makes a page deterministic: an offset shifts as rows arrive, so paging a growing log with one silently skips or repeats, while a page pinned to an epoch returns the same rows however much has been committed since. ICommandProcessorStore is the atomic end of processing a command. IActionQueue is the consumer side of delivery; its claims use the same bounded types and AbandonAsync requires a non-blank, trimmed Reason. IMachineStore aggregates the four.

    Supporting types: CommandId, ChartVersion, CommandRecord, Leased<'kind,'Work>, SubmissionOutcome, LeaseUpdateOutcome, ClaimBatch (1 through 1000), LeaseDuration (positive and at most Int32.MaxValue milliseconds), Reason (non-blank and trimmed), Backoff, AuditContext, and EventEnvelope, which pairs an event with its idempotency key, audit context and optional visibility and arrival instants. Each constrained argument exposes tryCreate, create, and value; Backoff also caps its ceiling at the same provider-safe millisecond maximum.

    ICommandProcessorStore.CommitAsync appends a transition, advances the belief, queues the actions and closes the inbox row; RejectAsync and DeadLetterAsync record the failure instead and leave the state alone. All three release the entity, and all three are idempotent on the command: a worker whose connection dropped mid-write can repeat the call and be told AlreadyFinalized with the epoch it wrote and the status that was stored, rather than that its lease is gone. The status lets a retry that sends a different resolution tell its answer was not the one recorded. That is what makes an ambiguous failure safe to retry.

    CommandFailure is Domain of 'Err or Machine of reason. Two cases because there are two kinds of answer and conflating them loses the more useful one: a chart that refuses an event says so in the application's own vocabulary, while a row that will not decode or an event with no rule is a fact about the machine, and no 'Err exists to express it. A processor cannot invent one.

    TransitionDraft is what pure chart resolution produces. CommittedTransition adds what only the database can decide: the authoritative Epoch, the CommandId, the ChartVersion and CommittedAt. FinalizeOutcome is Finalized, AlreadyFinalized of stored: Epoch * status: CommandStatus, Conflict or LeaseLost.

    Every store method answers failures as StoreError, and the case says what a caller can do. Unavailable means no answer arrived and is retried; Concurrency is retried from a fresh snapshot; Serialization and NotFound are dead-lettered. Unexpected is a failure the store does not recognise, such as a routine refusing its arguments or a constraint nothing expected to fire: a defect rather than an outage, which the default processor policy escalates to supervision. The bundled stores never throw for a driver failure; they classify it into one of these cases. A custom store should do the same, and may also let a defect propagate as an exception, which reaches supervision the way it always has.

    SubmissionOutcome is Accepted, AlreadySubmitted, or Refused of SubmissionRefusal. An idempotency key identifies the command kind, chart version, event JSON, and correction fields. An identical retry returns the original id; changed content returns Refused(KeyReused existing) without changing the command or allocating a sequence. Audit, visibility and arrival metadata do not redefine identity. PostgreSQL hashes its normalized jsonb request in the database; SQLite hashes a structured request at its boundary with sorted JSON object keys. Digest bytes are provider-local. Machine.send translates key reuse to MachineError.Rejected(MachineRejection.KeyReused existingId) immediately.

    Arrival time is nondecreasing in each entity's sequence order. Database-stamped arrivals are sampled inside the ordering transaction and clamped to their predecessor, including corrections. An explicit earlier arrival returns Refused(ArrivedBeforePredecessor predecessor) without inserting a command. Identical retries still return the original command regardless of retry metadata.

    Claims isolate undecodable rows. A poison command is dead-lettered with a machine failure beginning undecodable:; a poison action is abandoned with a durable action dead letter. These follow-up writes use the raw claim fence, so decoding an entity id is not required to quarantine it. Healthy work from the same claim is returned. If a follow-up write fails, that poison stays leased for reclamation and another quarantine attempt.

    Optional capabilities

    Two capabilities sit outside the required four, discovered by Store.tryTemporal and Store.tryCorrections rather than declared on IMachineStore. A type test is the only shape where adding a capability breaks nobody: members returning option would force every provider to write None for capabilities it has never heard of, and declaring one separately when a machine is wired would let a caller hand over a reader belonging to a different store than the one it configured. Machine.temporal and Machine.corrections return the options; Machine.history is always available because history is required.

    ITemporalReader reads the past. ValidAtAsync gives today's opinion about an instant of business time; AsOfAsync gives the opinion held at a second instant, about the first. Holding one still and moving the other is how a change of mind becomes visible as a change of mind. Both answer with a Belief, which is a Snapshot placed on two time axes plus the command and chart version that produced it, so "what is it now" and "what was it then" yield values that compare directly. An open bound crosses as None rather than as a sentinel instant.

    ICorrectionStore is the bitemporal operator surface. CorrectAsync(machineId, entityId, expected, validFrom, beliefs, cancellationToken) supersedes the belief timeline in one transaction under the entity lock. A stale core epoch returns CorrectionOutcome.Conflict actual without writing; this belief-only operation never changes the core snapshot or epoch. The first belief starts exactly at validFrom, subsequent starts strictly increase, and the last is open. An empty list erases beliefs from that instant while preserving the current snapshot.

    Machine corrections use three smaller interfaces: ITransitionLog.SuffixAsync, IPriorState.BeforeAsync (strictly before the supplied instant), and ICorrectionCommitter.CommitCorrectionAsync. CorrectionCapability holds all three as Log, Prior, and Committer. Store.tryCorrections and Machine.corrections return Some only when the store implements all three at the machine's exact types. Temporal tables are not required. IReplayStore and Store.tryReplay are removed. See custom corrections.

    Replaying a missed event. Machine.correct machine entityId envelope at policy enqueues a correction: the event should have happened at at. Without a complete correction capability it returns MachineError.Rejected MachineRejection.CorrectionsUnsupported before writing an inbox row. Preview returns CorrectionError.Rejected MachineRejection.CorrectionsUnsupported at the same boundary. The processor retains a dead-letter backstop for corrections already queued when a store is replaced. A worker replays the suffix under the charts that first decided it. CorrectionPolicy controls divergence and replay budget. No historical action is re-emitted.

    Earlier versions' charts are declared with chartCatalog; start refuses a catalog chart whose fingerprint differs from the registered one. Every version shares one set of types and codecs. ITransitionLog.SuffixAsync reports an incomplete suffix when the purge watermark indicates lost history; replay then answers ReplayError.HistoryPurged. The watermark records the greatest effective instant purged, rather than a surviving epoch, so back-dated corrections cannot fool it.

    Reading the past and changing it are two interfaces rather than one because they are two separate rights: a store may offer time travel without offering back-dating, and a deployment may want the reader everywhere while the correction path is reachable only where an operator has a reason.

    IStoreBoot checks a store's preconditions and performs the idempotent writes it needs before serving, and writes nothing when a check fails. BootReport is Ready or Refused of BootDefect list, where a BootDefect is SchemaMissing, SchemaBehind, SchemaAhead, MissingPrerequisite or Misconfigured. IWorkNotifications calls back when a store says work may be waiting; a callback is a hint, and losing every one costs the polling interval and never a command. Both are discovered by Store.tryBoot and Store.tryNotifications.

    IDatabaseMaintenance maintains a whole database rather than one machine: NotifyPendingAsync, RunAsync, DetectDriftAsync, RepairDriftAsync, ScheduleAsync and UnscheduleAsync. RunAsync and ScheduleAsync take a bounded ClaimBatch, so provider interpreters never receive an invalid batch. RetentionPolicy says how long terminal commands, superseded beliefs and delivered actions are kept, each as Retain.Forever or Retain.For span; RetentionPolicy.keepEverything is the default, because deleting audit data is never a default.

    IChartRegistry is the durable record of which chart structure each declared version belongs to. RegisterAsync answers Registered, Matched, or Mismatched of stored. The first sighting of a version claims it and nothing ever overwrites that, so a process whose chart no longer matches its declared version finds out before it writes anything. Every command pins a chart version and a foreign key holds it to one that a chart declared: submitting under an unregistered version returns StoreError.NotFound.

    Storage.Postgres

    MachineStoreOptions.forEntityId<'Tag, 'State, 'Event, 'Action, 'Err> context actionDelivery fills in every default a host would otherwise write out: JSON codecs, EntityId keys, keeping everything, a LeaseDuration that reaps leases five minutes after they expire, and listening on the store's own data source. Override any of it with { … with }. ActionDelivery is Deliver of ActionQueueName or Discard. ActionQueueName.tryCreate validates [a-z_][a-z0-9_]* and pgmq's 47-character limit; create throws for invalid configuration.

    PostgresMachineStore implements all four required interfaces from one MachineStoreOptions, composing PostgresCommandInbox, PostgresCommandProcessorStore and PostgresActionQueue. Its boot checks the extensions, the schema and the migration journal, creates a queue for Deliver, and registers delivery, Retention and ReapAfter. Commits route actions using that registration, and the processor no longer accepts a queue override. Discard records actions in transitions without enqueueing them; a commit with actions and no registration raises a named invalid_parameter_value and rolls back. EnsureQueueAsync only creates a physical queue; it does not replace boot's registration.

    SQLite uses the same explicit delivery argument. Deliver queues actions in the file's machine-keyed table; Discard retains them only in the transition. No physical queue is needed for a discard consumer in either provider.

    Listener says where the machine listens for announcements. ListenerConnection.SameDataSource holds one connection from the store's data source; Direct opens a dedicated unpooled one, for a data source behind a transaction-mode pooler, where LISTEN does not survive; Off relies on polling alone. The listener never joins an ambient transaction, pings on a keepalive so a silently dead connection fails rather than hangs, and fires both callbacks whenever it subscribes, since anything announced while it was away is gone.

    PostgresMaintenance implements IDatabaseMaintenance over a PostgresContext, one fsm.* routine per member, which are the same routines pg_cron runs. PostgresSupervisionQueries.listRecent takes a ClaimBatch, bounding each newest-first page at 1000 records.

    PostgresContext pairs a pooled NpgsqlDataSource with the resilience every call through it runs under, so one database has one pool and one circuit. DataSource.create enables automatic statement preparation; DataSource.resilience builds the pipeline with the driver-failure classifier this assembly owns. Serialization.systemTextJson creates F#-aware JSON codecs and Serialization.listOf derives a list codec from an element codec, which is what keeps the action list in fsm.transition agreeing element for element with the messages in the queue. EntityKey.forEntityId provides the common projection pair, whose decode returns a Result so a corrupt row becomes StoreError.Serialization rather than an exception.

    PostgresMachineStore now defaults to snapshot-only. It implements the four required interfaces, boot, notifications, and ITransitionLog, and advertises neither temporal reads nor bundled corrections. PostgresBitemporalMachineStore opts in through options |> MachineStoreOptions.bitemporal |> PostgresBitemporalMachineStore, composing the core store with temporal reads, operator corrections, prior-state reads, and a correction committer. BeliefModel is PostgreSQL-specific: Snapshot or Bitemporal. Boot refuses either kind of mismatch against the installed store_profile. TemporalOptions no longer accepts an application clock; ValidAtAsync selects the current database opinion directly.

    Migrator.migrate logger target takes { ConnectionString; Schema; Beliefs } and returns Result<MigrationReport, MigrationError>. SchemaName is the validated SQL injection boundary (default fsm). The tool accepts --schema / BS_AUTOMATA_SCHEMA and --beliefs snapshot|bitemporal / BS_AUTOMATA_BELIEFS; snapshot is the default. Enabling bitemporality later seeds the current snapshots. Disabling it returns MigrationError.ProfileMismatch, never an automatic drop. Phase E uses a clean reset of pre-E databases; no data-preserving upgrade is supplied.

    For a Generic Host, AddAutomataPostgres services configuration binds PostgresStoreSettings from Automata:Postgres (ConnectionStringName, Schema, Beliefs, Listener) and validates on startup before creating a pool. It registers the shared NpgsqlDataSource, PostgresContext, typed BeliefModel, and ListenerConnection. Callers choose the matching typed store at the composition root; boot checks the database profile. Generated …Storage.Postgres.Schema types remain unsupported. See schema evolution for layout and reset/opt-in recipes.

    An ordinary SQL commit uses the caller's FromState only at the initial epoch; continuing commits derive their origin from the locked current snapshot, so an unused draft source is never encoded, and an expected-epoch conflict still refuses the entire write. Correction commits retain an explicit origin. TransitionDraft.Event describes the decision, but the persisted event is authoritative from the submitted command: finalization does not re-encode or store a second event copy, and a mismatched low-level draft cannot rewrite the submitted request. History, result, and replay interfaces return the event through their existing types.

    • Edit this page
    In this article
    Back to top Generated by DocFX