# Determinism and replay

What an integrator needs to know to get **deterministic replay** out of libds:
the determinism guarantee and its scope, the platform signature that bounds it,
and the event-trace session header that identifies a recording. This is the
integrator-facing slice; the normative guarantee is the language spec's
([LDET]).

## What this is not

- Not the determinism contract — see [LDET].
- Not the event-trace wire format — see [RFCS] and [LFST].

---

(NDET-1)=
## The guarantee [NDET.1]

(NDET-1-1)=
[NDET.1.1] Per LDET, libds is deterministic over its inputs: **the same rule set
+ the same event trace produce the same output, bit-for-bit,** whatever the
platform ([NDET.2.1]).
A *live* run naturally varies (wall-clock time, arrival interleaving); a
*recorded* run replayed is reproducible.

*see also:* [NDET.2.1](#NDET-2-1)

(NDET-1-2)=
[NDET.1.2] Determinism is the backstop for testing and debugging: record a
production session as an event trace, replay it deterministically to reproduce a
bug. A run's platform signature is provenance ([NDET.2.1]), and the replayer
drives both clocks from the trace ([NDET.4.1]).

---

*see also:* [NDET.2.1](#NDET-2-1), [NDET.4.1](#NDET-4-1)

(NDET-1-3)=
[NDET.1.3] **The guarantee covers libds as its own build makes it.** libds's
`Float` arithmetic is its own ([IARC.7.10]): the integrator's C
runtime, libm or SDK cannot replace it, and libds does not change the
integrator's floating point, rounding mode included ([LDET.3.6.1]).

*checks:* `tla`  —  *see also:* [IARC.7.10](#IARC-7-10), [LDET.3.6.1](#LDET-3-6-1)

(NDET-1-4)=
[NDET.1.4] **Only admitted double arithmetic carries the guarantee.** A
target's double arithmetic — a hardware FPU, a coprocessor, an SDK's routines or
a library — is part of a release row only once it has been shown correct on that
target, against the archive that row ships. Acceleration is not excluded;
unshown acceleration is. A build that uses double arithmetic not admitted for its
target is outside the guarantee.

*checks:* `alloy×3`

(NDET-1-5)=
[NDET.1.5] **On a target with no double FPU, the guarantee covers libds's clang
build.** On the RP2350, RP2040 and nRF52833 libds's own build uses clang and
nothing else, and a libds built for those targets with another toolchain is
outside the guarantee. libds's double arithmetic there is its own ([IARC.7.11]).

*Deferred: not binding in this edition.*

*see also:* [IARC.7.11](#IARC-7-11)

(NDET-2)=
## The platform signature [NDET.2]

(NDET-2-1)=
[NDET.2.1] A **platform signature** ([LDET.2.3]) is a tuple of environment
properties plus the identities of the loaded series backends. It is
provenance, not a scope on the determinism guarantee: replay is
bit-for-bit reproducible whether or not two runs' signatures match
([LDET.3.4.1]).

*see also:* [LDET.2.3](#LDET-2-3), [LDET.3.4.1](#LDET-3-4-1)

(NDET-2-2)=
[NDET.2.2] The first five strings are compile-time constants (program-lifetime).
`backends` is engine-lifetime: one entry per registered series-backend slot, in
slot-id order. Each backend's compile-time identity affects sample-state
evolution, so it is part of the signature
([NSBK.7]). **Copy before retaining**
across `ds_engine_free`.

The engine must write each entry as `<atom>:<backend_name>:<backend_version>`,
where `<atom>` is the body message of the backing's atom in [RDSC.7.1]. The
entries are [NDET.2.5]'s.

*ratified*  —  *see also:* [NDET.2.5](#NDET-2-5), [NSBK.7](#NSBK-7), [RDSC.7.1](#RDSC-7-1)

(NDET-2-5)=
[NDET.2.5] **The `backends` entries.** An engine registers the seven backings
of [NSBK.6.1] and no other ([NSBK.2.3]), so its `backends` list must be exactly
these entries, in this order:

| Slot | Entry |
|---|---|
| 0 | `SeriesBackendWelfordEwma:ds-welford-ewma:0.1.0` |
| 1 | `SeriesBackendMonotonicDeque:ds-monotonic-deque:0.1.0` |
| 2 | `SeriesBackendFifoDeque:ds-fifo-deque:0.1.0` |
| 3 | `SeriesBackendReservoir:ds-reservoir:0.1.0` |
| 4 | `SeriesBackendIntervalPulse:ds-interval-pulse:0.1.0` |
| 5 | `SeriesBackendNumericIntegral:ds-numeric-integral:0.1.0` |
| 6 | `SeriesBackendRingBuffer:ds-ring-buffer:1.0.0` |

The names and versions are provenance ([NDET.2.1]). A backend's version must
change whenever what it answers changes, and that waits for a minor bump
([NSBK.7.1]).

*ratified*  —  *see also:* [NDET.2.1](#NDET-2-1), [NSBK.2.3](#NSBK-2-3), [NSBK.6.1](#NSBK-6-1), [NSBK.7.1](#NSBK-7-1)

(NDET-2-4)=
[NDET.2.4] **`libds.h` names its release at compile time.** It must define
four macros, each naming the release the header belongs to:

| Macro | Value |
|---|---|
| `DS_VERSION_MAJOR` | the release's major number, a decimal integer literal |
| `DS_VERSION_MINOR` | its minor number, a decimal integer literal |
| `DS_VERSION_PATCH` | its patch number, a decimal integer literal |
| `DS_VERSION_STRING` | a string literal: the three numbers, in that order, joined by `.` |

The three numbers are usable in a preprocessor `#if`, so one source tree can
build against two releases. The version is the DS version of [LDET.1.10] in its
clean form: three numbers and nothing else, with no pre-release or build suffix.
The macros are written when a release is cut, and never by hand.

**The run-time version extends the compile-time one.** For a libds a release
ships, the `libds_version` that `ds_engine_get_platform_signature` reports, and
the `ds_version` a recording's header carries ([LFST.4.3]), must begin with
`DS_VERSION_STRING` followed by `+`. What follows the `+` names the build and is
provenance only. So a header and a library from one release agree on the
`<major>.<minor>.` prefix [LDET.1.10] compares.

*ratified*  —  *see also:* [LDET.1.10](#LDET-1-10), [LFST.4.3](#LFST-4-3)

(NDET-2-3)=
[NDET.2.3] This accessor is informational: record the signature alongside a
session, and refuse (or flag) a replay whose signature differs. Replay across
signatures is bit-identical ([LDET.3.4.1]), so the signature is provenance and
a refusal key, not an input to a tolerance.

---

*see also:* [LDET.3.4.1](#LDET-3-4-1)

(NDET-3)=
## The session header [NDET.3]

(NDET-3-1)=
[NDET.3.1] A DS event trace is prefixed by a session header identifying the
recording. The engine exposes the captured subset ([NDET.3.3]).

*see also:* [NDET.3.3](#NDET-3-3)

(NDET-3-2)=
[NDET.3.2] `ds_version` is the DS version that wrote the stream, and it is the
only version a stream carries ([LDET.1.10]). A reader accepts a stream whose
`ds_version` begins with its own `<major>.<minor>.` and refuses every other with
`DS_ERR_FS_VERSION`. An
integrator comparing the field for anything other than that prefix is asserting
a guarantee DS does not make.

`dsc_hash` is `NULL` with length `0` until a DSC has been loaded; afterward it is
the 32-byte hash of that artefact — pair it with the platform signature to assert
"this recording was made against this rule set on this platform." Pointers are
engine-lifetime; copy before `ds_engine_free`.

*see also:* [LDET.1.10](#LDET-1-10)

(NDET-3-3)=
[NDET.3.3] The accessor reports `ds_version` (the DS version that wrote the
stream, a string), `session_id`, `tier_id` and the dialect-binding tuple
([RBND.1.3]): `dsc_hash`, `capabilities` and `dialect_def_sha256`. The rest of
the `StreamHeader` ([RFCS.4]) — the wall-clock anchor and the RT-config binding
— is not exposed through it.

---

*see also:* [RBND.1.3](#RBND-1-3), [RFCS.4](#RFCS-4)

(NDET-4)=
## Clock discipline [NDET.4]

(NDET-4-1)=
[NDET.4.1] Time-driven behaviour reads the engine's two clocks ([LTIM.2.8.1]); a
reproducible run must drive both from the recording, not from the host.

A replay of a recording needs no clock setup from the integrator: the replayer
drives both clocks from the trace ([NDET.8.2], [LFST.9.2.1]).

An integrator that drives an engine from its own record of inputs, rather than
replaying a recording, **must** drive the clocks itself. It switches the engine
to the mock clock before the first sample and advances it from the recorded
timestamps: `ds_engine_clock_set_mode(e, DS_CLOCK_MOCK)` then
`ds_engine_clock_set` / `_advance` — see [NLFC.5]
and [NTST]. Such a run on the live clock is not
reproducible.

---

*see also:* [LFST.9.2.1](#LFST-9-2-1), [LTIM.2.8.1](#LTIM-2-8-1), [NDET.8.2](#NDET-8-2), [NLFC.5](#NLFC-5)

(NDET-5)=
## Errors [NDET.5]

| Code | Meaning |
|---|---|
| `DS_ERR_FS_VERSION`           | a session header reports a `ds_version` outside the reader's `<major>.<minor>.` series ([LDET.1.10]) |

What else a reader refuses in a session header is [RFCS.4.7]. See [NERR.13].

---

*see also:* [LDET.1.10](#LDET-1-10), [NERR.13](#NERR-13), [RFCS.4.7](#RFCS-4-7)

(NDET-7)=
## Self-contained and lean forms [NDET.7]

[NDET.7] A replay or verification path must obtain the DSC bytes — from
the stream's embedded front if present, otherwise from a supplied
`.dsc` — and verify them against the session header's `dsc_hash`
([NDET.3], [RFCS.4.5]) before replaying; a mismatch is fatal. An
integrator must dispatch on the leading atom's field number ([RFCS.13.3]) to
tell a self-contained file (`DscHeader` first) from a lean one (`StreamHeader`
first).

---

*see also:* [NDET.3](#NDET-3), [RFCS.13.3](#RFCS-13-3), [RFCS.4.5](#RFCS-4-5)

(NDET-8)=
## Engine configuration [NDET.8]

(NDET-8-1)=
[NDET.8.1] **The settings a replay must reproduce.** libds's settings that
can change an observable are the following, and under [LDET.2.7] the integrator
must give a replaying engine each one as the recording engine had it. A replayer
does not adopt any of them from the trace.

| Setting | Why it changes an observable |
|---|---|
| The cache bounds (`ds_engine_set_cache_cap`, [NSTG.2.6.1]) | A cache write past either bound is refused ([LSTO.4.7.5]), which changes what a later `cached()` reads. |
| The run and tier identifiers (`run_id`, `tier_id` in `ds_engine_config_t`, [NLFC.1.2]) | Every `event_id` derives from them ([LFST.5.6]). The stream header records both ([RFCS.4.3], [RFCS.4.4]), so the integrator can always read them back to configure the replaying engine. |

*ratified*  —  *see also:* [LDET.2.7](#LDET-2-7), [LFST.5.6](#LFST-5-6), [LSTO.4.7.5](#LSTO-4-7-5), [NLFC.1.2](#NLFC-1-2), [NSTG.2.6.1](#NSTG-2-6-1), [RFCS.4.3](#RFCS-4-3), [RFCS.4.4](#RFCS-4-4)

(NDET-8-2)=
[NDET.8.2] **The settings a replay need not reproduce.** The rest of libds's
configuration changes no observable, so it is not a precondition of replay:

- **The admission policy** (`ds_engine_set_admission`): it decides only whether
  an artefact loads ([NLFC.10.2]).
- **The clocks:** the replayer drives both from the trace ([LFST.9.2.1],
  [NDET.4.1]).
- **The reporting callbacks** (stream, metric, log):
  they observe and answer nothing. The call callback's answers enter the
  engine as inputs, which the stream records.
- **The cache persistence callbacks** (`ds_engine_cache_callbacks`): a
  callback's status reaches the call's return ([NCBK.7.5]) and changes neither
  the cache nor the stream.

**A replay invokes no effect callback.** The
replayer compares each re-derived call against its recorded `CallIssued` and
re-derives each cache write, which no atom records ([RFCS.3.6]). It must not
perform either outside the engine ([LFST.9.2]): it must hand no call
to the call callback, and no write, delete or expiry to the cache persistence
callbacks. The engine still applies each cache write to its own cache, where
the replay's later reads find it. So neither callback need be registered to
replay a recording, and a registered one is not called: the answers a call
received are already in the stream as inputs.

**A cache seed is not configuration but starting state, and the recording
carries it** ([NSTG.9.2]). It emits no stream event, so the recorder writes
each host seed in the stream's prelude ([RFCS.26.1]) and the replayer applies
it before the first input ([RFCS.26.2]). The integrator does not seed an
engine it replays into.

---

*ratified*  —  *see also:* [LFST.9.2](#LFST-9-2), [LFST.9.2.1](#LFST-9-2-1), [NCBK.7.5](#NCBK-7-5), [NDET.4.1](#NDET-4-1), [NLFC.10.2](#NLFC-10-2), [NSTG.9.2](#NSTG-9-2), [RFCS.26.1](#RFCS-26-1), [RFCS.26.2](#RFCS-26-2), [RFCS.3.6](#RFCS-3-6)

