# Realtime mode and the runtime contract

The integrator's surface for **bounded-memory, bounded-time** operation: the
working-memory bound and the pool setters (`set_wm_max`, the `set_rt_*`
setters), the realtime mode and its contract binding, the INIT→LIVE
transition, and the errors each produces. Which of these bind is stated per
clause.

## What this is not

- Not the realtime-contract spec — that is
  [RTCT] and the binding spec [RBND].
- Not a board bring-up guide. Per-board setup (clock, heap, flash partition)
  lives with each board's runtime tree; the INIT→LIVE transition itself is the
  `ds_engine_go_live` call ([NRTM.5]), which those runtimes invoke during
  bring-up.

---

(NRTM-1)=
## What RT mode is [NRTM.1]

*Deferred: not binding in this edition.*

(NRTM-1-1)=
[NRTM.1.1] In RT mode the engine runs from **pre-allocated slab pools** — facts,
tokens, and activations are carved from arenas sized at configuration time, so
steady-state operation does no `malloc`. The working-memory bound `N_max`
becomes a **hard** limit, and join fan-out is capped. This is what makes
worst-case execution time and memory bounded for embedded / deadline-driven
deployments.

(NRTM-1-2)=
[NRTM.1.2] RT mode is reached one of two ways:

- A **`realtime` artefact** carries a contract atom (`RealtimeContract`); loading it with
  `ds_engine_load_dsc` sets the engine pending-RT. The subsequent
  `ds_engine_set_wm_max` ([NRTM.2]) completes RT initialisation.
- An integrator **explicitly** configures the pools and bound via the setters
  below.

*see also:* [NRTM.2](#NRTM-2)

(NRTM-1-3)=
[NRTM.1.3] In **non-RT** mode the same setters are accepted but `N_max` is only
advisory and objects are `malloc`'d on demand. Most server-class integrations
run non-RT and never call these.

---

(NRTM-2)=
## Configuring RT [NRTM.2]

*Deferred: not binding in this edition.*

(NRTM-2-1)=
[NRTM.2.1] The pool-shaping setters must be called **before**
`ds_engine_set_wm_max`, which is what allocates the pools.

*checks:* `alloy×2`

(NRTM-2-2)=
[NRTM.2.2] Each setter takes `0` to mean "use the default":

| Setter | Controls | Default | Embedded recommendation |
|---|---|---|---|
| `ds_engine_set_rt_token_depth`    | `k_max`, the per-token embedded `facts[]` length (`tok_size = 32 + k_max×4` on 32-bit) | 16 | 4 (e.g. SAMD51) |
| `ds_engine_set_rt_token_pool_cap` | token pool capacity | `N_max × 8` | `N_max × 2` or `× 4` |
| `ds_engine_set_rt_presz_cap`      | hash-table pre-size for alpha / beta / terminal / WM tables | `next_pow2(N_max × 4)` | `N_max` (e.g. 64) |

The desktop defaults size for heavy churn; the embedded recommendations avoid
multi-kilobyte-per-terminal allocations on constrained targets.

(NRTM-2-3)=
[NRTM.2.3] When a `RealtimeContract` contract was loaded and the engine is pending-RT,
`ds_engine_set_wm_max` also runs the full RT initialisation (RT mode on, pool
allocation, table pre-sizing) — see RDSC.16.3.7. After it returns the engine is
fully RT-configured.

---

*see also:* [RDSC.16.3.7](#RDSC-16-3-7)

(NRTM-3)=
## The working-memory bound [NRTM.3]

(NRTM-3-1)=
[NRTM.3.1] **This edition has no working-memory bound.**
`ds_engine_set_wm_max` is not declared ([NRTM.9.1]), and the engine **must
not** refuse an assert for the size of working memory. A
loaded program is not a trigger: the only thing that makes `N_max` a hard
limit is a realtime contract, and realtime is deferred ([NRTM.1.1]).
`DS_ERR_WM_FULL` ([NERR.18]) is realtime's and is
not raised in this edition.

*ratified*  —  *see also:* [NERR.18](#NERR-18), [NRTM.1.1](#NRTM-1-1), [NRTM.9.1](#NRTM-9-1)

(NRTM-3-2)=
[NRTM.3.2] A join whose fan-out would exceed the token budget is rejected with
`DS_ERR_TOKEN_BUDGET`. An RT-mode operation attempted with a required bound unset
returns `DS_ERR_RT_UNSAFE`.

---

*Deferred: not binding in this edition.*

(NRTM-5)=
## Going live [NRTM.5]

*Deferred: not binding in this edition.*

(NRTM-5-1)=
[NRTM.5.1] RT execution has two phases: an **INIT** phase, in which schemas /
rules load, the pools are sized, and the intern table is warmed, and a **LIVE**
phase, in which the engine pumps under the bounded contract with no allocation
and no structural change.

*checks:* `alloy×3` `tla×10`

(NRTM-5-2)=
[NRTM.5.2] The INIT→LIVE transition is the public call **`ds_engine_go_live`**. It is **one-way** and MUST be called after `ds_engine_set_wm_max`
(which sizes the pools and locks the intern table) and after the bundle is
loaded and warmed — every string the workload uses must be interned before the
lock. After it returns `DS_OK` the pump runs zero-alloc under the loaded
contract. The board runtimes (under `runtime/esp32*`, `runtime/rp2350`,
`runtime/samd51`, …) call it as part of bring-up, so a board integration rarely
invokes it directly; a custom or bare-metal port calls it itself.

*checks:* `alloy×3` `tla×6`

(NRTM-5-3)=
[NRTM.5.3] Two error codes report transition problems:

- `DS_ERR_RT_PRECOND` — a go-live pre-condition was not satisfied,
  e.g. `ds_engine_set_wm_max` was not called first.
- `DS_ERR_RT_LIVE` — an operation forbidden in LIVE mode was attempted
 : structural changes (loading rules, registering schemas) are
  INIT-only.

*checks:* `alloy` `tla×7`

(NRTM-5-4)=
[NRTM.5.4] An integrator targeting a server-class deployment that wants the
bounded-memory cap without the embedded LIVE protocol loads a program carrying a
realtime contract, sets `N_max` with `ds_engine_set_wm_max`, and pumps normally,
without calling `ds_engine_go_live`; the LIVE-phase protocol is an embedded
concern. Without a contract the setter caps nothing ([NRTM.3.1]).

---

*see also:* [NRTM.3.1](#NRTM-3-1)

(NRTM-6)=
## The contract binding [NRTM.6]

*Deferred: not binding in this edition.*

(NRTM-6-1)=
[NRTM.6.1] A `realtime` artefact's contract is summarised in a small binding
tuple the integrator can read and compare:

```c
typedef struct ds_rt_config_binding {
    char rt_config_sha256[65]; /* hex; "" when no contract is loaded */
} ds_rt_config_binding_t;

ds_rt_config_binding_t b;
ds_engine_get_rt_config_binding(engine, &b);
```

(NRTM-6-2)=
[NRTM.6.2] Use the tuple to verify that artefacts which must agree do (RBND.1.4):
a mismatch surfaces at load as `DS_ERR_BND_RT_CONFIG_MISMATCH`. Compare two
tuples by value (the struct is fixed-size, NUL-terminated fields).

*see also:* [RBND.1.4](#RBND-1-4)

(NRTM-6-3)=
[NRTM.6.3] To investigate a mismatch, `ds_contract_dump_canonical(engine,
&buf, &len)` returns the canonical (RBND.4b) serialization of the loaded
contract: its bytes in a buffer the caller owns and frees with `ds_free`
([IALC.4.3]), and their length. Diff the canonical bytes of two engines. It
returns `DS_ERR_CONTRACT_MISSING` when no contract is loaded, and allocates
nothing then.

---

*see also:* [IALC.4.3](#IALC-4-3), [RBND.4b](#RBND-4b)

(NRTM-9)=
## The realtime entry points in this edition [NRTM.9]

(NRTM-9-1)=
[NRTM.9.1] **Realtime is not in this edition, and none of its entry points is
declared.** `ds_engine_go_live` ([NRTM.5.2]),
`ds_engine_get_rt_config_binding` ([NRTM.6.1]), `ds_contract_dump_canonical`
([NRTM.6.3]), the pool-sizing setters ([NRTM.2.1], [NRTM.2.2]) and the
working-memory bound ([NRTM.2.3], [NRTM.5.4]) return with realtime, as those
deferred clauses describe them.

*ratified*  —  *see also:* [NRTM.2.1](#NRTM-2-1), [NRTM.2.2](#NRTM-2-2), [NRTM.2.3](#NRTM-2-3), [NRTM.5.2](#NRTM-5-2), [NRTM.5.4](#NRTM-5-4), [NRTM.6.1](#NRTM-6-1), [NRTM.6.3](#NRTM-6-3)

