# `dynamic_ttl` — Dynamic Per-Emit TTL Override

The `dynamic_ttl` capability.
Prefix `LDTL`.

This document is an **overlay** on the base language and runtime specs.
It does not modify any base spec paragraph; it states how those paragraphs
are interpreted or restricted when `dynamic_ttl` is absent (the default)
or active.  Each overlay section names the base paragraph it constrains.

---

(LDTL-1)=
## What `dynamic_ttl` is [LDTL.1]

(LDTL-1-1)=
### The default: schema-fixed TTL [LDTL.1.1]

Every `ttl`-lifecycle schema declares its TTL at the schema level, and every
fact of the schema expires that long after it enters working memory:

$\mathit{deadline} = t_{\mathit{assert}} + d_s$

where $t_{\mathit{assert}}$ is the fact's entry stamp ([LTIM.2.8.6]) and $d_s$
the schema's declared TTL, a **compile-time constant** fixed at DSC load.

This is the default and the common case.  Because all facts of the same
schema expire in assertion order (FIFO), the engine can use a
**per-schema FIFO TTL queue** — no deadline heap, O(1) per assert and
O(expired × log n_ttl_schemas + n_ttl_schemas) per tick ([LDTL.3.1],
[LDTL.6.3]). The engine's tick in this edition costs [LDTL.6.4]'s.

*see also:* [LDTL.3.1](#LDTL-3-1), [LDTL.6.3](#LDTL-6-3), [LDTL.6.4](#LDTL-6-4), [LTIM.2.8.6](#LTIM-2-8-6)

(LDTL-1-2)=
### The opt-in: per-emit TTL override [LDTL.1.2]

`dynamic_ttl` enables the `retain: <expr>` kwarg on `emit`
actions for `ttl`-lifecycle schemas. It overrides the
schema-level TTL for that one emission: the fact's deadline is its entry
stamp plus the evaluated duration ([LDTL.4.1]), not known until
rule-firing time.  Because different facts of the same schema can now
have different expiry times, the FIFO order within a schema is broken:
a later-asserted fact may expire before an earlier one.

The engine falls back to the **deadline min-heap** to correctly order
arbitrary expiry deadlines.  Cost: O(log N_ttl) per insert/remove,
O(expired × log N_ttl) per tick ([LDTL.4.2], [LDTL.6.3]). The engine's tick in
this edition costs [LDTL.6.4]'s.

*see also:* [LDTL.4.1](#LDTL-4-1), [LDTL.4.2](#LDTL-4-2), [LDTL.6.3](#LDTL-6-3), [LDTL.6.4](#LDTL-6-4)

(LDTL-1-3)=
### Why this is not a feature flag but a capability opt-in [LDTL.1.3]

The performance difference is real and significant.  For N_max = 100,000
facts and expired = 1,000 per tick:

| Mode | Tick cost | Formula |
|---|---|---|
| Default (per-schema FIFO queue) | O(expired × log n_schemas + n_schemas) | n_schemas ≈ 10–50; ≈ 1,000 × 6 = 6,000 at 50 |
| `dynamic_ttl` (deadline heap) | O(expired × log N_ttl) | ≈ 1,000 × 17 = 17,000 |

The default removes N_ttl from the tick cost: its one log factor is in the
number of TTL schemas, a compile-time constant ([LDTL.6.1]).  A
program that never uses per-emit TTL override pays no cost for the
capability.

---

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

(LDTL-2)=
## Extension-capability registration [LDTL.2]

(LDTL-2-1)=
### Name and registration [LDTL.2.1]

`dynamic_ttl` is a construct capability of [LCAP.1]. A program has
`dynamic_ttl: true` through its dialect or a line of its `use` block
([LDIA.2.1]). `default` has `dynamic_ttl: false` ([LDIA.9.2]).

With `dynamic_ttl: false`, [LDTL.3.2] refuses a `retain:` duration kwarg on
an `emit` that targets a `ttl`-lifecycle schema. A `publish` takes no `retain:` in
any dialect ([LTIE.4.9]).  The schema-level TTL duration is the sole expiry
mechanism.

*see also:* [LCAP.1](#LCAP-1), [LDIA.2.1](#LDIA-2-1), [LDIA.9.2](#LDIA-9-2), [LDTL.3.2](#LDTL-3-2), [LTIE.4.9](#LTIE-4-9)

(LDTL-2-2)=
### Composition [LDTL.2.2]

`dynamic_ttl` is orthogonal to every other capability:

| With `dynamic_ttl: true` and | Effect |
|---|---|
| `linear_scan: false` | Orthogonal. `linear_scan: false` restricts join and slot types; `dynamic_ttl` affects TTL expiry only. Both may be set. |

Composition with `realtime` is [LDTL.2.4].

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

(LDTL-2-4)=
### Composition with `realtime` [LDTL.2.4]

| With `dynamic_ttl: true` and | Effect |
|---|---|
| `realtime: true` | Deadline heap in RT mode. The engine must reserve and size the deadline heap ([LDTL.4.2]) during INIT, and must check its capacity at the transition to LIVE. No new RT pre-conditions. |
| `realtime: true`, `linear_scan: false` | All three constraints apply independently. |

*Deferred: not binding in this edition.*

*see also:* [LDTL.4.2](#LDTL-4-2)

(LDTL-2-3)=
### Without `dynamic_ttl` and `realtime` [LDTL.2.3]

When `realtime` is active but `dynamic_ttl` is not, the engine must reserve
everything the per-schema FIFO TTL queues ([LDTL.3.1]) need during INIT, before
the transition to LIVE, and the TTL path must allocate nothing during LIVE
([RTCT.12.4]). The queues hold at most N_max facts together, because each fact
belongs to exactly one schema's queue.

---

*Deferred: not binding in this edition.*

*see also:* [LDTL.3.1](#LDTL-3-1), [RTCT.12.4](#RTCT-12-4)

(LDTL-3)=
## Default TTL behavior (without `dynamic_ttl`) [LDTL.3]

(LDTL-3-1)=
### Per-schema FIFO TTL queue [LDTL.3.1]

In the default mode, each `ttl`-lifecycle schema maintains a
**per-schema FIFO TTL queue** ordered by assertion time.  Because all
facts of the same schema share the same TTL duration, assertion order
equals expiry order: the oldest-asserted fact always expires first.

| Operation | Time | Notes |
|---|---|---|
| Fact assert (TTL) | O(1) — append to schema TTL tail | No heap sift |
| Fact retract (explicit) | O(1) — intrusive doubly-linked splice | Must remove from queue |
| TTL tick | O(expired × log n_ttl_schemas + n_ttl_schemas) | Merge the schemas' queue fronts by deadline, then entry ([LDTL.6.1]); expire each front while its deadline is at or before the tick's instant |

`n_ttl_schemas` is the number of `ttl`-lifecycle schemas in the DSC — a
compile-time constant, not a function of WM population, so the merge's log
factor is a constant too: about 6 for 50 TTL schemas.

*Deferred: not binding in this edition.*

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

(LDTL-3-2)=
### `retain:` kwarg blocked [LDTL.3.2]

With `dynamic_ttl: false`, the `retain: <expr>` kwarg on `emit`
targeting a `ttl`-lifecycle schema is a compile error when
the expression is not a compile-time constant ([LEXP.8.8]) equal to the schema's
declared TTL duration.

A `retain:` kwarg that is a compile-time constant matching the schema's
own TTL is redundant but not an error — it has no effect.  A `retain:`
kwarg with a different or non-constant value is a compile error
(`DS_VALIDATE_CAPABILITY_DISABLED`, [LDIA.3.4.2]).

*ratified*  —  *see also:* [LDIA.3.4.2](#LDIA-3-4-2), [LEXP.8.8](#LEXP-8-8)

(LDTL-4)=
## `dynamic_ttl` behavior [LDTL.4]

(LDTL-4-1)=
### Per-emit TTL override enabled [LDTL.4.1]

With `dynamic_ttl`, the author may write the `retain: <expr>` kwarg on
`emit` for a `ttl`-lifecycle schema with any expression of type
`Duration`.  The evaluated duration replaces the schema's declared TTL for
that specific fact.

The compiler must refuse a `retain:` expression whose type is not
`Duration` with `DS_VALIDATE_TYPE_MISMATCH`, as it refuses a kwarg value of
another type than its slot's ([LEXP.7.3b]). The compiler must refuse an
un-narrowed `Duration?` expression with `DS_VALIDATE_NULLABLE_UNNARROWED`:
`retain:` demands a value, so it is a concrete-value position ([LNUL.5.1]). On a target of any other
lifecycle `retain:` has nothing to override, and the compiler must refuse it with
`DS_VALIDATE_RETAIN_NON_TTL`.

The engine must fix the deadline of a fact emitted with a per-emit TTL override when
it asserts the fact:

$\mathit{deadline} = t_{\mathit{assert}} + d$

where $t_{\mathit{assert}}$ is the fact's entry stamp ([LTIM.2.8.6]) and $d$
the evaluated `retain:` duration.

**The engine evaluates `retain:` last.** The engine **must** evaluate the `retain:` expression only after it has
evaluated every slot value of the action. So where a slot value and the
`retain:` expression would both fault, the fault it records is the slot
value's, and the `retain:` expression is not evaluated. The `ts` fill comes
before both ([LFAC.9.2]).

*ratified*  —  *see also:* [LEXP.7.3b](#LEXP-7-3b), [LFAC.9.2](#LFAC-9-2), [LNUL.5.1](#LNUL-5-1), [LTIM.2.8.6](#LTIM-2-8-6)

(LDTL-4-2)=
### Deadline heap restored [LDTL.4.2]

With `dynamic_ttl`, the deadline min-heap holds the deadline of every
`ttl` fact, whether or not it carries a per-emit override.
This is necessary because, even without explicit overrides, the presence
of the kwarg in the language means the engine cannot assume FIFO
expiry order across schemas.

| Operation | Time | Notes |
|---|---|---|
| Fact assert (TTL) | O(log N_ttl) | heap insert |
| Fact retract | O(log N_ttl) | heap remove |
| TTL tick | O(expired × log N_ttl) | Drain the heap while its earliest deadline is at or before the tick's instant |

---

*Deferred: not binding in this edition.*

(LDTL-5)=
## Overlays on base spec paragraphs [LDTL.5]

(LDTL-5-1)=
### LFAC.1.3.3 — per-schema or per-emit TTL [LDTL.5.1]

Overlays [LFAC.1.3.3] (ttl facts expire via per-schema or per-emit TTL).

In the default (without `dynamic_ttl`), [LFAC.1.3.3] is restricted:
the "per-emit TTL" path is not available.  Expiry is always per-schema.
[LFAC.1.3.3]'s mention of per-emit TTL is the full spec; it becomes
accessible only when `dynamic_ttl` is declared.

*see also:* [LFAC.1.3.3](#LFAC-1-3-3)

(LDTL-5-2)=
### LGRA.5.2 / LGRA.16.5 — schema TTL duration [LDTL.5.2]

Overlays [LGRA.5.2] (schema_lifecycle `ttl <time_expr>`) and [LGRA.16.5]
(per-emit override via `retain: <DUR>`).

In the default, the `retain:` kwarg on `emit` is restricted
per [LDTL.3.2], and a `publish` takes none ([LTIE.4.9]).  [LGRA.16.5]'s note that "per-emit override remains
available via `emit X retain: <DUR>`" is conditioned on `dynamic_ttl`
being declared.

*see also:* [LDTL.3.2](#LDTL-3-2), [LGRA.16.5](#LGRA-16-5), [LGRA.5.2](#LGRA-5-2), [LTIE.4.9](#LTIE-4-9)

(LDTL-5-4)=
### LIVE deadline heap [LDTL.5.4]

The deadline heap exists only under `dynamic_ttl`. Its presence or absence is
what separates the two LIVE tick paths.

In LIVE **without** `dynamic_ttl`: there is no heap, and the TTL tick is
O(expired × log n_ttl_schemas + n_ttl_schemas) ([LDTL.6.1]). What the engine
reserves before the transition to LIVE is each schema's TTL queue ([LDTL.3.1]),
not a heap.

In LIVE **with** `dynamic_ttl`: the engine reserves the heap before the
transition to LIVE, inserting a deadline is O(log N_ttl), and the TTL tick is
O(expired × log N_ttl) ([LDTL.4.2]).

Both paths are bounded and neither allocates during LIVE, which is what
[RTCT.12.4] requires of the pump.

---

*Deferred: not binding in this edition.*

*see also:* [LDTL.3.1](#LDTL-3-1), [LDTL.4.2](#LDTL-4-2), [LDTL.6.1](#LDTL-6-1), [RTCT.12.4](#RTCT-12-4)

(LDTL-6)=
## Runtime behavioral guarantees [LDTL.6]

(LDTL-6-1)=
### Without `dynamic_ttl` [LDTL.6.1]

- The facts one tick expires leave by deadline, earliest first, and facts
  that share a deadline by when each entered working memory ([LDET.5.11]).
  Within one schema that is assertion order. The order in which the schemas
  are visited does not affect it.
- The tick's cost is [LDTL.6.4]'s.

*checks:* `idris×16`  —  *see also:* [LDET.5.11](#LDET-5-11), [LDTL.6.4](#LDTL-6-4)

(LDTL-6-2)=
### With `dynamic_ttl` [LDTL.6.2]

- Per-emit `retain:` override is available.
- The facts one tick expires leave by deadline, earliest first, and facts
  that share a deadline by when each entered working memory ([LDET.5.11]).
  Within one schema this may differ from assertion order when overrides are
  used.
- The tick's cost is [LDTL.6.4]'s.

*see also:* [LDET.5.11](#LDET-5-11), [LDTL.6.4](#LDTL-6-4)

(LDTL-6-3)=
### The tick's cost [LDTL.6.3]

- Without `dynamic_ttl`, the TTL tick is **O(expired × log n_ttl_schemas +
  n_ttl_schemas)**: it merges the fronts of the per-schema queues
  ([LDTL.3.1]). No term grows with N_ttl. Each expiry decision is O(1): one
  comparison of a queue front's deadline ([LDTL.1.1]) with the tick's
  instant. No heap allocation is made anywhere in the TTL path.
- With `dynamic_ttl`, the TTL tick is **O(expired × log N_ttl)**: it drains
  the deadline heap ([LDTL.4.2]).

*Deferred: not binding in this edition.*

*see also:* [LDTL.1.1](#LDTL-1-1), [LDTL.3.1](#LDTL-3-1), [LDTL.4.2](#LDTL-4-2)

(LDTL-6-4)=
### The tick's cost in this edition [LDTL.6.4]

[LDTL.6.3]'s bounds do not bind this edition. In either mode the TTL tick's
cost is at most linear in N_ttl, the number of live facts that carry a
deadline: a runtime may examine each of them on every tick, and expires those
whose deadline is at or before the tick's instant, in [LDTL.6.1]'s or
[LDTL.6.2]'s order. The tick may allocate to hold the facts it expires.

---

*see also:* [LDTL.6.1](#LDTL-6-1), [LDTL.6.2](#LDTL-6-2), [LDTL.6.3](#LDTL-6-3)

(LDTL-7)=
## T_wcet improvements [LDTL.7]

*Deferred: not binding in this edition.*

(LDTL-7-1)=
### `max_expired_per_tick` — a derived compile-time bound [LDTL.7.1]

`expired_max` is not a free deployment parameter — it is derivable
from values the compiler already knows:

$\mathit{max\_expired\_per\_tick} = \left\lceil \dfrac{N_{\max} \cdot \Delta_{\mathit{tick}}}{d_{\min}} \right\rceil$

where $N_{\max}$ is the declared maximum working-memory size (a deployment
parameter), $\Delta_{\mathit{tick}}$ the interval between TTL ticks (an
integration parameter, declared alongside $N_{\max}$), and $d_{\min}$ the
shortest TTL among the program's `ttl`-lifecycle schemas (a compile-time
constant from their declarations).

Rationale: in the worst case, all facts have the minimum possible TTL.
At steady state with $N_{\max}$ facts, at most
$N_{\max} \cdot \Delta_{\mathit{tick}} / d_{\min}$ facts accumulate between
ticks.  That many expire on the next tick.  This is the ceiling on expired
facts per tick.

The compiler will carry `max_expired_per_tick` in the artefact's realtime
contract ([RTCT.6]) beside its other bounds, and the engine will check the TTL
queues' capacity against it at the transition to LIVE.

For example, with $N_{\max} = 10{,}000$, $\Delta_{\mathit{tick}} = 100$ ms and
$d_{\min} = 30$ s, `max_expired_per_tick` $= \lceil 10{,}000 \times 0.1 / 30 \rceil = 34$.
Not proportional to $N_{\max}$ in the large-$N_{\max}$ limit; proportional
to $\Delta_{\mathit{tick}} / d_{\min}$, which is a deployment design choice.

*see also:* [RTCT.6](#RTCT-6)

(LDTL-7-2)=
### All remaining T_wcet parameters are compile-time constants [LDTL.7.2]

After substituting `max_expired_per_tick` for `expired_max`, every
parameter in the T_wcet formula is a compile-time constant:

| Parameter | Source |
|---|---|
| `n_ttl_schemas` | DSC schema count — fixed at load |
| `max_expired_per_tick` | Derived ([LDTL.7.1]) |
| `F_max` | The compiler's activation-chain bound — a rule-topology constant |
| `A_max` | The most actions in one rule — a DSC constant |
| `E_rhs` | The deepest RHS expression — a DSC constant |
| `α_s` | Schema selectivity — data-dependent but N_max-bounded |
| `T_s` | Alpha test count per node — DSC constant |

None of these grow with N_max beyond what is already captured in
`max_expired_per_tick` and `α_s` (both of which are bounded by
`N_max` but divided by a deployment-chosen denominator).

Viewed as a function of N_max (the variable the integrator controls),
the T_wcet formula is **O(1) in N_max** under `linear_scan: false` with
default TTL.  The formula computes a fixed number; it does not express
a growth rate.

*see also:* [LDTL.7.1](#LDTL-7-1)

(LDTL-7-3)=
### TTL term: log and linearity both eliminated [LDTL.7.3]

```
# With dynamic_ttl (heap):
  c_ttl × expired_max × log₂(N_ttl)        ← log factor, N_max-proportional

# Without dynamic_ttl (FIFO queue):
  c_ttl × (max_expired_per_tick × log₂(n_ttl_schemas) + n_ttl_schemas)   ← constant
```

The heap term has two problems: a log factor in N_ttl and proportionality
to `expired_max` which itself grows with N_max.  The FIFO term replaces
both with a single constant derived from schema TTL durations, tick
interval and the number of TTL schemas; its log factor is in that number,
which is fixed at compile time ([LDTL.6.1]).

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

(LDTL-7-4)=
### Combined T_wcet under `linear_scan: false`, default TTL [LDTL.7.4]

```
T_wcet(linear_scan=false, dynamic_ttl=false) =
    c_alpha × Σ_s(α_s × T_s)                 ← DSC constants
  + c_join  × F_max                           ← O(1) per firing, F_max = DSC constant
  + c_agenda × F_max                          ← O(1) 3-bucket queue
  + c_fire  × F_max × A_max × E_rhs          ← DSC constants
  + c_ttl   × (max_expired_per_tick × log n_ttl_schemas
               + n_ttl_schemas)               ← derived constant
```

**All parameters are compile-time constants.**  T_wcet is a single
fixed number determined by the DSC, N_max, and tick_interval.  There
are no log factors in N_max or N_ttl (the TTL term's is in
`n_ttl_schemas`, fixed at load, [LDTL.6.1]), no N_max-growth terms, and
no remaining variables.
This is the theoretical floor for a Rete engine with expression
evaluation — the only remaining non-constant factor is E_rhs, the RHS
expression depth, which is eliminated only by AoT specialization.

---

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

(LDTL-8)=
## Compile errors and diagnostics [LDTL.8]

| Code | Condition |
|---|---|
| LDTL.3.2 | `retain: <expr>` on `emit` for a `ttl` schema where `expr` is not the schema's own TTL constant, without `dynamic_ttl` declared |

All violations are compile errors.  The error names the schema, the
conflicting expression, and the missing extension.

---

*see also:* [LDTL.3.2](#LDTL-3-2)

(LDTL-9)=
## What `dynamic_ttl` does not affect [LDTL.9]

- All non-TTL schema lifecycles (`logical`, `explicit`, `activation`,
  `ambient`) — unaffected.
- TTL schemas that use only the schema-level TTL duration — no behaviour
  change; their assert/retract cost is the same either way.
- Series TTL (sliding window duration) — independent of this extension.
- Cooldown timing — independent; it compares readings of the monotonic
  clock ([LTIM.2.8.2]), not WM expiry.
- The `retain:` kwarg on `cache` actions — `cache` is not WM; this
  extension does not affect cache TTL semantics.

---

*see also:* [LTIM.2.8.2](#LTIM-2-8-2)

