# Series backends

The **aggregation backends** behind the samples plane, as an integrator meets
them. A backend is the code that maintains a series' sliding state (mean,
sliding min/max, count-distinct, …) and answers queries. This is a distinct
surface from the samples-plane *observation* callbacks in
[NSTG.5]: those forward absorbed samples *out* to
your storage; a backend computes the aggregation the rules read.

Every backend is libds's own. The seven of [NSBK.6.1] are compiled into
`libds.a` and registered by libds when it constructs an engine; an integrator
supplies and registers none ([NSBK.2.3]). What an integrator meets of them is
here: which backings exist, their identities in the platform signature, and
the integrator's read. How libds shapes them is the impl tree's
[ISBK].

## What this is not

- Not the samples-plane observation surface — see [NSTG.5]
  (`sample_cb` / `series_declared_cb`).
- Not the author-side series language — see
  [LSER].
- Not the algorithm catalogue. The bundled algorithms document themselves.
- Not an extension point. No external backend is admitted ([NSBK.2.3]).

---

(NSBK-1)=
## When a backend is needed [NSBK.1]

(NSBK-1-1)=
[NSBK.1.1] The compiler chooses a backing algorithm per series at compile time
and records it in the `.dsc` as the series' **atom**, one of the seven of
[NSBK.6.1] (`SeriesBackendRingBuffer` for a ring buffer,
`SeriesBackendWelfordEwma` for Welford running stats). At `ds_engine_load_dsc`,
the engine must have a backend **registered for every backing the artefact
uses**; a missing one fails the load with `DS_ERR_BACKEND_NOT_REGISTERED`. An engine
registers all seven ([NSBK.2.3]), so an artefact that names one of them does not
meet this refusal.

*see also:* [NSBK.2.3](#NSBK-2-3), [NSBK.6.1](#NSBK-6-1)

(NSBK-1-2)=
[NSBK.1.2] libds registers every backend itself, once, when it constructs an
engine ([NSBK.2]). Each backing has exactly one implementation, so there is no
resolution order and nothing an integrator can override ([NSBK.2.3]).

*see also:* [NSBK.2](#NSBK-2), [NSBK.2.3](#NSBK-2-3)

(NSBK-1-3)=
[NSBK.1.3] An integrator registers nothing before `ds_engine_load_dsc`: the
engine `ds_engine_new` returns already serves every backing of [NSBK.6.1], so
the loader resolves every series declaration a conforming artefact carries.

---

*see also:* [NSBK.6.1](#NSBK-6-1)

(NSBK-2)=
## Registering backends [NSBK.2]

**Every series backend is supplied by libds, and an integrator supplies and
registers none.** The seven backends of [NSBK.6.1] are compiled into `libds.a`,
and libds registers all of them when it constructs an engine — no path, no
`dlopen`, on every target libds runs on (including those without `dlopen`, e.g.
Xtensa/esp32). The public surface has no registration call.

A registration failure is a construction failure: libds must not return an
engine whose backends did not all register, and must return the registration
error instead. A `.dsc` series atom whose backing no registered backend serves
is refused with `DS_ERR_BACKEND_NOT_REGISTERED`, not silently degraded.

How libds shapes and checks its backends is the impl tree's
([ISBK.1], [ISBK.3]).

*see also:* [ISBK.1](#ISBK-1), [ISBK.3](#ISBK-3), [NSBK.6.1](#NSBK-6-1)

(NSBK-2-3)=
[NSBK.2.3] **The backend set is closed, and so are its implementations.** v1
accepts no integrator-supplied vtable. The seven backings are fixed
([NSBK.6.1]), and for each one the implementation libds ships is the only one
that runs.

**This is a determinism boundary, not a packaging preference.** A series
aggregation feeds rule conditions, so two implementations of one backing that
disagree in the last place make the same `.dsc` fire different rules over the
same facts. That divergence would be undetectable: series backings are not
serialised ([LSER.9.2]) and carried by no atom — so nothing reaches a replay to
compare. `[LDET.2.2a]` rests on the backings rebuilding *identically*, which
holds only while one implementation exists.

Contrast the `call` interface, which admits external nondeterminism and fences
it: a call's result re-enters as a recorded fact, so a replay compares it and a
divergence is caught. Series backings have no such fence, so the substitution
is closed off instead.

*see also:* [LDET.2.2a](#LDET-2-2a), [LSER.9.2](#LSER-9-2), [NSBK.6.1](#NSBK-6-1)

(NSBK-5)=
## The element type [NSBK.5]

(NSBK-5-3)=
[NSBK.5.3] **The element type is `proto/series_backend_ring_buffer.proto#SeriesElementType`.**
libds's internal mirror of it, which a backend's declaration carries, is held
to it by [ISBK.2.1].

*defined in:* [proto/series_backend_ring_buffer.proto#SeriesElementType](#proto-series_backend_ring_buffer-proto-SeriesElementType)  —  *see also:* [ISBK.2.1](#ISBK-2-1)

(NSBK-6)=
## The backings [NSBK.6]

(NSBK-6-1)=
[NSBK.6.1] The seven backings, each named by the body message of its atom in
[RDSC.7.1], and the algorithm each runs:

| Atom | Backs |
|---|---|
| `SeriesBackendWelfordEwma` | Welford running stats (cumulative) — mean / stdev / variance accumulators |
| `SeriesBackendRingBuffer` | ring buffer — windowed exact values |
| `SeriesBackendMonotonicDeque` | monotonic deque — exact sliding min/max |
| `SeriesBackendFifoDeque` | FIFO deque — first / bool sliding |
| `SeriesBackendReservoir` | reservoir sampling — k-sample from an unbounded stream |
| `SeriesBackendIntervalPulse` | interval-pulse deque — `active_fraction` ([LSIG.2.2]) |
| `SeriesBackendNumericIntegral` | numeric integral — `integral` ([LSIG.2.3]) |

All seven are built into `libds.a` and registered at engine construction
([NSBK.2]).

*see also:* [LSIG.2.2](#LSIG-2-2), [LSIG.2.3](#LSIG-2-3), [NSBK.2](#NSBK-2), [RDSC.7.1](#RDSC-7-1)

(NSBK-6-3)=
[NSBK.6.3] **The aggregations are the registry `specs/registry/series_agg.ebnf`.**
libds's internal mirror of it is held to it by [ISBK.1.1]. A member's value is
also its bit in a backend's `supported_aggs`, so no allocation may exceed 63.
Values 10–14 are allocated, not members, and never
reissued ([STYL.2.8.7]).

A series has meaning only to the rules
that read it, and its values reach an integrator only through what those rules
do: a call, or a fact they emit or publish.

*see also:* [ISBK.1.1](#ISBK-1-1)

(NSBK-7)=
## Determinism note [NSBK.7]

(NSBK-7-1)=
[NSBK.7.1] A backend's `backend_name` / `backend_version` are part of the
platform signature ([NDET.2.5]), and like the rest of it they are provenance
([NDET.2.1]). A series-bearing replay is bit-for-bit across platforms
([LDET.3.4.1]) and across every release of the recording's minor series
([LDET.1.10]). Each backing has one implementation ([NSBK.2.3]), and a change to
what it answers waits for a minor bump, as any change to what a program computes
does.

---

*see also:* [LDET.1.10](#LDET-1-10), [LDET.3.4.1](#LDET-3-4-1), [NDET.2.1](#NDET-2-1), [NDET.2.5](#NDET-2-5), [NSBK.2.3](#NSBK-2-3)

(NSBK-8)=
## Errors [NSBK.8]

(NSBK-8-1)=
[NSBK.8.1] Registration / load failures use the `-620..-625` backend-ABI range
(`DS_ERR_BACKEND_ABI_VERSION`, `_BAD_MAGIC`, `_BAD_AGGS`, `_NOT_REGISTERED`).
The range is distinct from the
native-Rete loader codes — see [NERR.10.2]. libds registers only its own
backends ([NSBK.2]), so `DS_ERR_BACKEND_ABI_VERSION` (-620),
`DS_ERR_BACKEND_BAD_MAGIC` (-621) and `DS_ERR_BACKEND_BAD_AGGS` (-622) report a
defect in that registration, which fails engine construction ([ISBK.1.7],
[ISBK.3]); -620 also reaches an integrator through the cache callbacks
([NSTG.2.4]). -624 and -625 are reserved: libds loads no backend from a shared
object ([NERR.10.2]).

---

*see also:* [ISBK.1.7](#ISBK-1-7), [ISBK.3](#ISBK-3), [NERR.10.2](#NERR-10-2), [NSBK.2](#NSBK-2), [NSTG.2.4](#NSTG-2-4)

