# Hydration implementation

**Not in this edition.** Every clause in this document is deferred or retired, so nothing here binds a 1.0 implementation. It is kept so that its ids and the references to them resolve.


The reference-runtime contract for fact hydration. [LHYD] defines hydration as a language and author surface; this document
defines how a conforming runtime and its adapters implement it — the compiler's
`corr` check, the adapter's assertion protocol, the engine's budget and
retraction mechanisms, and the integrator API that drives them.

In detail: the declared `corr`
slot on a hydration target, the adapter assertion protocol, the engine-side budget /
supersede / series-skip mechanisms, the auto-registered builtin schemas, and the
integrator C API.

Prefix `RHIM`.

Parents:
- [LHYD] — the language-side hydration contract this document implements.
- [NCBK.9] — the integration callback surface hydration rides on.
- [RLOW.8.4] — series lowering, which the series-skip rule (RHIM.5.3) mirrors.

---

(RHIM-1)=
## 1. Division of responsibility [RHIM.1]

*Deferred: not binding in this edition.*

(RHIM-1-1)=
[RHIM.1.1] Hydration has three participants. The **compiler** must check that
every hydration target declares its `corr` slot (RHIM.2). The **adapter**
(integrator code behind the call callback) must satisfy the assertion protocol
(RHIM.3). The **engine** must provide the budget gate, the
superseding-correlation sweep, and the series-skip behaviour (RHIM.5), and must
auto-register the builtin schemas (RHIM.4).

*see also:* [RHIM.2](#RHIM-2), [RHIM.3](#RHIM-3), [RHIM.4](#RHIM-4), [RHIM.5](#RHIM-5)

(RHIM-1-2)=
[RHIM.1.2] Hydration does not introduce a dedicated callback. The runtime
dispatches a `call hydrate_*` to the integrator over the ordinary call
surface ([NCBK.9]); the integrator's call callback is the only entry point.

*see also:* [NCBK.9](#NCBK-9)

(RHIM-1-3)=
[RHIM.1.3] The runtime must not assert hydrated facts on the adapter's behalf.
The engine provides mechanisms keyed on a hydration target's `corr`; the adapter
performs every assertion. A fact that the adapter leaves with an empty `corr` is
an ordinary live fact, and none of the mechanisms in RHIM.5 apply to it.

---

*see also:* [RHIM.5](#RHIM-5)

(RHIM-2)=
## 2. The `corr` slot [RHIM.2]

*Deferred: not binding in this edition.*

(RHIM-2-1)=
[RHIM.2.1] The compiler must check that every hydration target declares `corr`
as a non-nullable `String`, marked `key` on a `logical` schema, and that no
schema is both a hydration target and a response schema ([LHYD.4.1.7]). It must add no slot to any schema: the slot an
integrator, either runtime and every binding sees is the one the author
declared.

*see also:* [LHYD.4.1.7](#LHYD-4-1-7)

(RHIM-2-2)=
[RHIM.2.2] The schemas checked are exactly the hydration targets
[LHYD.4.1.7] names: those in a hydration service's `hydrates:` list, the
`schema:` of each `hydrate_facts` call, and the builtin
`HydratedSample`. A schema a rule merely reads through `.corr` is not one.

*see also:* [LHYD.4.1.7](#LHYD-4-1-7)

(RHIM-2-4)=
[RHIM.2.4] The engine must give `corr` the empty string `""` on any fact of a
hydration target asserted without a value for it, and that value is the interned
empty string, so a live fact matches the live-only test ([LHYD.4.1.4]). A
live-emitted fact therefore carries `corr == ""`; only the adapter sets it
non-empty.

---

*see also:* [LHYD.4.1.4](#LHYD-4-1-4)

(RHIM-3)=
## 3. Adapter obligations [RHIM.3]

*Deferred: not binding in this edition.*

*checks:* `tla×10`

(RHIM-3-1)=
[RHIM.3.1] On receiving a `hydrate_*` call, the adapter must recognise the service
name, read the request body (the target schema, the time window, and any `limit:`
and `filter:`), and read the correlation id from the call's `as` binding.

(RHIM-3-2)=
[RHIM.3.2] The adapter must assert each hydrated fact with its `corr` slot set
to the call's correlation id ([NCBK.9.2]). This tag is the key on which every
engine mechanism in RHIM.5 operates.

*checks:* `tla×3`  —  *see also:* [NCBK.9.2](#NCBK-9-2), [RHIM.5](#RHIM-5)

(RHIM-3-3)=
[RHIM.3.3] The adapter must apply the call's required `retain:` duration
([LHYD.6.2]) as a per-fact TTL override, asserting with an explicit TTL
(RHIM.6). The adapter must not rely on the schema's own lifecycle to bound a
hydrated fact's lifetime.

*see also:* [LHYD.6.2](#LHYD-6-2), [RHIM.6](#RHIM-6)

(RHIM-3-4)=
[RHIM.3.4] The adapter must assert the `HydrationComplete` fact strictly **after**
all hydrated facts for the call ([LHYD.5.3]). A rule that matches
`HydrationComplete` is thereby guaranteed to observe the full hydrated set; the
ordering mechanism that achieves this is an adapter concern.

*checks:* `tla×2`  —  *see also:* [LHYD.5.3](#LHYD-5-3)

(RHIM-3-5)=
[RHIM.3.5] When `HydrationComplete` is asserted, the adapter must set its `count`
to the number of facts asserted for the call and its `truncated` to `true` when
the result hit the caller's `limit:` ([LHYD.7.2.1]).

*see also:* [LHYD.7.2.1](#LHYD-7-2-1)

(RHIM-3-6)=
[RHIM.3.6] When the backing query fails, the adapter must assert a `CallError` for
the correlation rather than completing silently, so a waiting rule observes the
failure.

---

*checks:* `tla`

(RHIM-4)=
## 4. Builtin schemas [RHIM.4]

*Deferred: not binding in this edition.*

(RHIM-4-1)=
[RHIM.4.1] When a program contains a hydration call, the compiler must register
the builtin schemas the protocol uses, so the integrator asserts them without
declaring them: `HydrationComplete` and `CallError` for every hydration call, and
`HydratedSample` additionally for any `hydrate_samples` call. The integrator
resolves these schemas by name.

(RHIM-4-2)=
[RHIM.4.2] `HydrationComplete` carries the shape defined in [LHYD.5.1] — `corr`,
`schema`, `count`, `truncated`, `t_start`, `t_end`. The adapter asserts one
`HydrationComplete` per hydration call (RHIM.3.4).

*see also:* [LHYD.5.1](#LHYD-5-1), [RHIM.3.4](#RHIM-3-4)

(RHIM-4-3)=
[RHIM.4.3] `HydratedSample` carries the shape defined in [LHYD.4.3.1] — `adapter`,
`entity_id`, `field`, `ts`, `value`, `value_type`, `corr`. The adapter
asserts one `HydratedSample` per raw sample returned by a `hydrate_samples` call.

*see also:* [LHYD.4.3.1](#LHYD-4-3-1)

(RHIM-4-4)=
[RHIM.4.4] `CallError` carries `corr`, `code`, `message`, and `ts`. The engine
uses it for the budget-rejection path (RHIM.5.1); the adapter uses it for query
failures (RHIM.3.6).

---

*see also:* [RHIM.3.6](#RHIM-3-6), [RHIM.5.1](#RHIM-5-1)

(RHIM-5)=
## 5. Engine mechanisms [RHIM.5]

These three behaviours key on the `corr` slot of a fact of a hydration target. Each is the engine's
half of a contract whose adapter half is in RHIM.3.

*Deferred: not binding in this edition.*

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

(RHIM-5-1)=
### 5.1 Budget gate [RHIM.5.1]

*checks:* `tla×2`

(RHIM-5-1-1)=
[RHIM.5.1.1] The engine must maintain a global hydration budget — the maximum
number of facts of hydration targets with a non-empty `corr` permitted in working memory
([LHYD.7.4]). The compiled default budget is 100 000 facts.

*see also:* [LHYD.7.4](#LHYD-7-4)

(RHIM-5-1-2)=
[RHIM.5.1.2] Before dispatching a hydration call to the adapter, the engine must
count the working-memory facts of hydration targets whose `corr` is non-empty. When that count
has reached the budget, the engine must reject the call: it must assert a
`CallError` with `code` set to `"budget_exhausted"` for the call's correlation,
and must not dispatch the call to the adapter.

*checks:* `tla`

(RHIM-5-1-3)=
[RHIM.5.1.3] The budget is per-engine and applies across all schemas. The
integrator sets it (RHIM.8) after the DSC is loaded; it does not persist across a reload, so the integrator must
re-set it after each load.

*see also:* [RHIM.8](#RHIM-8)

(RHIM-5-2)=
### 5.2 Superseding correlation [RHIM.5.2]

(RHIM-5-2-1)=
[RHIM.5.2.1] When a `hydrate_*` call reuses the correlation id of a prior
hydration, the engine must retract every fact of a hydration target carrying that
`corr`, and every live `HydrationComplete` and `CallError` carrying it
([LCRS.6.2.1]), after the budget gate passes (RHIM.5.1.2) and before it dispatches
the call, so before the adapter asserts the new set ([LHYD.6.3]). The integrator
does not track and retract the prior batch.

*see also:* [LHYD.6.3](#LHYD-6-3), [LCRS.6.2.1](#LCRS-6-2-1)

(RHIM-5-2-2)=
[RHIM.5.2.2] An empty correlation must not trigger the sweep. Because a live fact's
`corr` is `""` (RHIM.2.4), a sweep on the empty correlation
would match every never-hydrated fact; the engine must treat an empty correlation
as a no-op.

*see also:* [RHIM.2.4](#RHIM-2-4)

(RHIM-5-3)=
### 5.3 Series skip [RHIM.5.3]

(RHIM-5-3-1)=
[RHIM.5.3.1] The series runtime must skip facts of hydration targets whose `corr` is
non-empty
when absorbing values into series state ([LHYD.10.4.1]). A retrospective
hydration therefore does not feed historical values into a live-window series.

*see also:* [LHYD.10.4.1](#LHYD-10-4-1)

(RHIM-5-3-2)=
[RHIM.5.3.2] Hydrated facts do remain in working memory for rules to match
([LHYD.10.4.2]); the skip applies only to series absorption, not to fact
visibility.

---

*see also:* [LHYD.10.4.2](#LHYD-10-4-2)

(RHIM-6)=
## 6. Retain and TTL [RHIM.6]

*Deferred: not binding in this edition.*

(RHIM-6-1)=
[RHIM.6.1] The runtime must thread the call's `retain:` duration into the hydrate
request body so the adapter receives it. The adapter applies it per fact
(RHIM.3.3).

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

(RHIM-6-2)=
[RHIM.6.2] An assert carrying an explicit TTL must override the asserted fact's deadline
with the supplied TTL for that single assertion, including on a `logical` schema.
The engine's TTL sweep retracts the fact when the clock passes
`assert_time + retain_ns`. Hydration imposes no `logical` semantics on hydrated
facts ([LHYD.6.5]): their lifetime is the `retain:` expiry, a superseding
correlation (RHIM.5.2), or explicit retraction.

---

*see also:* [LHYD.6.5](#LHYD-6-5), [RHIM.5.2](#RHIM-5-2)

(RHIM-7)=
## 7. Logical-fact lifetime [RHIM.7]

*Deferred: not binding in this edition.*

(RHIM-7-1)=
[RHIM.7.1] A logical fact whose left-hand side matches a hydrated fact must have
its lifetime bounded above by the shortest-lived element of that left-hand side
([LHYD.10.5]). When the left-hand side includes a hydrated fact, the logical
fact's upper bound is that hydrated fact's `retain:` expiry, so the logical fact
does not outlive the hydrated data that justifies it.

---

*see also:* [LHYD.10.5](#LHYD-10-5)

(RHIM-8)=
## 8. Integrator obligations [RHIM.8]

*Deferred: not binding in this edition.*

(RHIM-8-1)=
[RHIM.8.1] The integrator drives hydration through three engine
operations:

| Operation | Obligation it serves |
|---|---|
| assert-with-TTL | assert each hydrated fact with the `retain:` TTL (RHIM.3.3, RHIM.6) |
| set-hydration-budget | override the global budget after load (RHIM.5.1.3) |
| schema-lookup-by-name | resolve `HydrationComplete` / `HydratedSample` / `CallError` (RHIM.4.1) |

*see also:* [RHIM.3.3](#RHIM-3-3), [RHIM.4.1](#RHIM-4-1), [RHIM.5.1.3](#RHIM-5-1-3), [RHIM.6](#RHIM-6)

(RHIM-8-2)=
[RHIM.8.2] The hydration budget must be set after the DSC is loaded. Calling it before the budget global is in scope returns
`DS_ERR_INVAL`.

---

(RHIM-9)=
## 9. Conformance [RHIM.9]

*Deferred: not binding in this edition.*

(RHIM-9-1)=
[RHIM.9.1] A conforming runtime must pass the `hydration_runtime` conformance
kind, which exercises the protocol end to end: the `corr` declaration
check and defaulting (RHIM.2), correlation tagging and completion ordering (RHIM.3), the
budget gate and its `CallError` (RHIM.5.1), the superseding-correlation sweep
(RHIM.5.2), and series-skip (RHIM.5.3).

*see also:* [RHIM.2](#RHIM-2), [RHIM.3](#RHIM-3), [RHIM.5.1](#RHIM-5-1), [RHIM.5.2](#RHIM-5-2), [RHIM.5.3](#RHIM-5-3)

