# Timed actions

The `after` continuation clause on a rule's
RHS. An `after <duration>: <actions>` clause schedules an action
list to run after a delay on the monotonic clock. The compiler lowers each
clause to a synthetic timer fact plus a follow-up rule; the
declarative core is preserved and no engine state changes are
required beyond ordinary fact assertion and retraction.

Paired with:
- [LGRA] for action grammar.
- [LLOG] for the `$ClockTick`
  substrate that drives timer firing.
- [LLEX.10.6] for the synthetic names
  timer facts take.
- [LCCO] for `on`,
  `timeout`, and `error` continuations on `call` actions; `after`
  composes with them per §7.

## What this is not

- Not a coroutine, yield, or await mechanism. `after` creates a
  follow-up rule, not a paused execution frame.
- Not a replacement for `timeout` per [LCCO.7], which fires when
  a call's response does not arrive. `after` fires on a delay
  regardless of any call state.
- Not a cancellation primitive. A scheduled `after`
  cannot be cancelled mid-flight. The supersedure
  mechanism at [LRST] puts
  `as <expr>` on `after` for per-fact supersedure on
  author-named keys (re-fire replaces the prior timer).

---

(LTAC-1)=
## Scope [LTAC.1]

(LTAC-1-1)=
[LTAC.1.1] An `after` clause is an optional trailing clause on a
rule's RHS. Each clause schedules its action list to run after
the declared duration, measured from the firing of the activation
that produced the schedule.

(LTAC-1-2)=
[LTAC.1.2] A rule's RHS may carry zero or more `after` clauses.
Each clause must lower to one independent timer fact and one
follow-up rule per §3.

(LTAC-1-3)=
[LTAC.1.3] An `after` clause's action list is an ordinary action
list per [LGRA]. Any action legal at the top level of a
rule's RHS is legal inside an `after` body, including further
`after` clauses (nested follow-ups) and `call` actions with their
own continuations per [LCCO].

(LTAC-1-4)=
[LTAC.1.4] The semantics of an `after` clause are defined by the
lowering in §3. The compiler's behaviour on a rule with `after`
clauses must be indistinguishable from its behaviour on the
lowered timer-fact-plus-follow-up-rule form, modulo synthetic
names.

---

(LTAC-2)=
## Surface [LTAC.2]

(LTAC-2-1)=
[LTAC.2.1] The compiler must accept `after` clauses after a rule's immediate
actions, in place of them, and inside an `after` body, as [LGRA.6.1]'s
`action_body` and `after_clause` give them.

*ratified*  —  *see also:* [LGRA.6.1](#LGRA-6-1)

(LTAC-2-2)=
[LTAC.2.2] An `after` clause's duration is a duration literal, a `UNIT_LIT`
whose unit is on the `time` axis ([LGRA.6.1]): `30_s`, `5_min`, `2_h`. A named or
computed duration does not parse. The compiler must refuse a unit of another
dimension with `DS_VALIDATE_BAD_UNIT_DIMENSION`, as at every duration position
([LLEX.12.8]). An author needing a runtime duration writes a sibling rule that
asserts a custom timer fact.

*ratified*  —  *see also:* [LGRA.6.1](#LGRA-6-1), [LLEX.12.8](#LLEX-12-8)

(LTAC-2-2-1)=
[LTAC.2.2.1] The optional `as <expr>` clause declares a
correlation key that drives per-fact supersedure on re-fire per
[LRST]. A new activation with the same
evaluated key supersedes the prior timer; the prior timer's
deferred actions do not fire. Without `as`, each `after` clause
schedules an independent timer that fires regardless of re-fires.

*ratified*

(LTAC-2-3)=
[LTAC.2.3] An RHS's `after` clauses follow its immediate actions, and may
stand alone ([LGRA.6.1]'s `action_body`). Several `after` clauses are
independent: the source order between them carries no firing guarantee beyond
each clause's own duration.

---

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

(LTAC-3)=
## Lowering [LTAC.3]

(LTAC-3-1)=
[LTAC.3.1] The compiler must rewrite each `after <dur>: <actions>`
clause into one synthetic timer fact assertion appended to the
parent rule's immediate action list, plus one synthetic follow-up
rule.

*ratified*

(LTAC-3-2)=
[LTAC.3.2] The timer fact's schema is named
`$Timer_<rule>_<key>`, where `<rule>` is the parent rule's name
and `<key>` is the clause's key path: the source-order index for a
top-level `after`, and `<outer>_<inner>` for one nested inside
another ([LTAC.6.2.2]). The schema carries two slots, in this order:
`fire_at`, a `Duration`, and `corr`, a `String`.

`fire_at` is the monotonic-clock reading ([LTIM.2.8.2]) at or after
which the follow-up rule may fire. It is a `Duration` because
`monotonic_ns()` returns one ([LTIM.2.2.1]) and the follow-up's guard
compares against it. An `after` delay is relative time, so it runs on
the monotonic clock: a change to the wall clock neither hastens nor
delays it.

`corr` carries the supersedure key from the clause's `as <expr>`
([LRST.1.1]), and is empty when the clause declares none.

The schema declares no `scope` and no `key` / `mutable` markers
because neither applies: the timer fact's lifecycle is explicit —
the follow-up rule retracts it — so it is not a `logical` schema,
and [LFAC.1.7.1]'s declare-every-slot requirement is scoped to
those. An author cannot write this schema in any case.

*checks:* `idris`  —  *see also:* [LFAC.1.7.1](#LFAC-1-7-1), [LRST.1.1](#LRST-1-1), [LTAC.6.2.2](#LTAC-6-2-2), [LTIM.2.2.1](#LTIM-2-2-1), [LTIM.2.8.2](#LTIM-2-8-2)

(LTAC-3-3)=
[LTAC.3.3] The parent rule's RHS gains an action that asserts the
timer fact with `fire_at: monotonic_ns() + <dur>`. The assertion is
appended after the rule's original immediate actions; the
ordering matches source.

*ratified*  —  *checks:* `idris×3`

(LTAC-3-4)=
[LTAC.3.4] The follow-up rule is named as [LTAC.8.1] states. Its one
pattern matches a fact `t` of the timer schema under the guard `monotonic_ns() >= t.fire_at`, so it
matches the timer whose `fire_at` is at or before the current clock. Its RHS
is the `after` clause's action list, followed by the retraction of `t`.

The retraction has no author syntax: the timer schema's lifecycle is
explicit, and the compiler emits the retraction directly into the synthetic
rule. Neither this
rule nor its schema is writable by an author — `retract` is not one
of the action heads ([LGRA.10.1]) — which is why `after` is a
language form rather than a macro over one.


**A `logical` emit in the body is refused** ([LLOG.11.9]): the follow-up
retracts the timer, so the activation that would justify the fact ends
inside its own firing.

*checks:* `idris×7`  —  *see also:* [LGRA.10.1](#LGRA-10-1), [LLOG.11.9](#LLOG-11-9), [LTAC.8.1](#LTAC-8-1)

(LTAC-3-5)=
[LTAC.3.5] Captured bindings from the parent rule's LHS that the
`after` body references must be carried into the follow-up rule
the same way [LCCO.5] carries captures across a call boundary:
the compiler emits a `$CaptureBindings_<rule>_after_<N>`
fact alongside the timer assertion, and the follow-up rule joins
it on a correlation per [LCCO.5.2]. Capture is
snapshot-always per [LCCO.5.4].

**The correlation is the clause's `as` key where it has one, and a
per-activation value otherwise.** With `after … as <expr>` the timer fact
and its capture fact both carry the evaluated key, so the supersedure sweep
[LTAC.2.2.1] requires finds the prior pair and the author's key is what
decides identity. Without `as` the compiler generates a value unique to the
activation, so independent timers do not collide.

**Both facts are swept, not only the timer.** A new activation with a key
already in flight retracts the prior timer **and** the prior capture fact
before asserting its own ([LRST.4.5]). Sweeping the timer alone would leave
two capture facts on one correlation, and the surviving follow-up would join
both and run its body twice.

*ratified*  —  *checks:* `idris×6`  —  *see also:* [LCCO.5](#LCCO-5), [LCCO.5.2](#LCCO-5-2), [LCCO.5.4](#LCCO-5-4), [LRST.4.5](#LRST-4-5), [LTAC.2.2.1](#LTAC-2-2-1)

(LTAC-3-6)=
[LTAC.3.6] If the `after` body references no LHS bindings, the
compiler must skip the capture-fact emission. Empty captures are
not emitted.

This differs from a correlated call, which emits the capture fact even with
nothing to capture ([LCCO.5.2], [LCCO.5.6]). There the capture fact is what
carries a generated or computed correlation to the sibling rules. Here the
timer fact carries the correlation itself ([LTAC.3.5]), so an empty capture
fact would carry nothing the follow-up rule needs.

*checks:* `idris`  —  *see also:* [LCCO.5.2](#LCCO-5-2), [LCCO.5.6](#LCCO-5-6), [LTAC.3.5](#LTAC-3-5)

(LTAC-4)=
## Timer facts [LTAC.4]

(LTAC-4-1)=
[LTAC.4.1] Synthetic timer facts are engine-managed, and their schemas have
synthetic names ([LLEX.10.6.1]). Author code cannot pattern-match a timer fact
directly: no source text can write a `$Timer_…` name.

*checks:* `idris`  —  *see also:* [LLEX.10.6.1](#LLEX-10-6-1)

(LTAC-4-2)=
[LTAC.4.2] Timer facts are one-shot. The follow-up rule's action
list ends with the engine's retraction of the timer fact
([LTAC.3.4]). A timer fact must not survive its firing.

*ratified*  —  *see also:* [LTAC.3.4](#LTAC-3-4)

(LTAC-4-3)=
[LTAC.4.3] If the parent rule fires N times before any of its
timer-fact retractions take effect, N independent timer facts
exist concurrently. Each carries its own `fire_at` and is matched
by an independent follow-up activation.

*ratified*

(LTAC-4-4)=
[LTAC.4.4] Timer facts have no TTL beyond their one-shot nature.
A timer fact that has been asserted fires its follow-up at or
after its `fire_at`, regardless of how long the engine has been
running. Engine restart and timer persistence belong in the
runtime spec.

---

(LTAC-5)=
## Tick granularity [LTAC.5]

(LTAC-5-1)=
[LTAC.5.1] The follow-up rule's LHS re-evaluates on every clock
tick, and the engine takes one tick per pump ([IRET.19.5]): a timer's
follow-up fires on the first tick at or after `fire_at`. How soon after
`fire_at` that is depends on how often the integrator pumps
([LLOG.11.2]); the engine has no tick interval of its own.

*checks:* `idris`  —  *see also:* [IRET.19.5](#IRET-19-5), [LLOG.11.2](#LLOG-11-2)

(LTAC-5-3)=
[LTAC.5.3] A missed tick — when the engine is paused, sleeping,
or otherwise unable to evaluate during a scheduled tick — must
not skip the timer firing. The follow-up rule's LHS uses
`monotonic_ns() >= t.fire_at`, which holds on every later tick; the
follow-up fires late, never lost. The guard is `>=` against an
absolute instant rather than equality against a scheduled one,
and that is the whole mechanism — an equality test would lose
exactly the tick this clause is about.

*checks:* `idris×3`

(LTAC-5-4)=
[LTAC.5.4] If multiple timers' `fire_at` fall in the same tick,
the engine must fire all of them in that tick. Their follow-up
activations complete together, on that tick ([LTAC.5.1]), and are
ordered as any activations are: those of different rules by
[LDET.5.4], and those of one rule by the order [LDET.5.5] gives
matches that complete together.

---

*ratified*  —  *see also:* [LDET.5.4](#LDET-5-4), [LDET.5.5](#LDET-5-5), [LTAC.5.1](#LTAC-5-1)

(LTAC-6)=
## Composition [LTAC.6]

(LTAC-6-1)=
### With `call` continuations [LTAC.6.1]

(LTAC-6-1-1)=
[LTAC.6.1.1] An `after` clause may appear at the rule top level
alongside `on`, `timeout`, or `error` clauses on a `call` action.
The clauses target different events: `on` / `timeout` / `error`
fire on call response state per [LCCO]; `after` fires on
a monotonic-clock delay ([LTIM.2.8.2]) regardless of any call's state. The compiler
must lower each clause independently per its own §3 / [LCCO.3].

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

(LTAC-6-1-2)=
[LTAC.6.1.2] An `after` clause may appear inside an `on`,
`timeout`, or `error` continuation body. The inner `after` lowers
under the synthetic continuation rule that hosts it; the timer
fact and follow-up rule are scoped to the inner rule, sharing the
inner correlation for capture purposes.

(LTAC-6-1-3)=
[LTAC.6.1.3] An `after` body may contain a `call` action with its
own continuations. Chain depth limits per [LCCO.10.2] count
`after` boundaries the same way they count `on` boundaries: a
`call` inside an `after` body is depth two; that call's `on`
bodies must not themselves contain calls with continuations.

*see also:* [LCCO.10.2](#LCCO-10-2)

(LTAC-6-2)=
### With other actions [LTAC.6.2]

(LTAC-6-2-1)=
[LTAC.6.2.1] An `after` body may contain any action legal in a
rule's RHS ([LGRA.10.1]). The action list executes when the
follow-up rule fires; the actions' own semantics are unchanged.

*checks:* `idris`  —  *see also:* [LGRA.10.1](#LGRA-10-1)

(LTAC-6-2-2)=
[LTAC.6.2.2] An `after` body may contain further `after` clauses.
Nested timed actions chain naturally: the inner timer is asserted
when the outer follow-up fires. A chain's depth is its nesting in
the source.

---

(LTAC-7)=
## Compile-time validation [LTAC.7]

(LTAC-7-1)=
[LTAC.7.1] The compiler must reject an `after` clause whose duration is
zero, with `DS_VALIDATE_AFTER_NONPOSITIVE_DURATION`: a zero delay is no timer.
A negative duration does not parse ([LGRA.6.1]).

*ratified*  —  *checks:* `idris`  —  *see also:* [LGRA.6.1](#LGRA-6-1)

(LTAC-7-3)=
[LTAC.7.3] LHS bindings referenced inside an `after` body must
be declared on the parent rule's LHS, the same as for any
captured binding per [LCCO.11.5]. References to undeclared names
produce a compile error.

*ratified*  —  *see also:* [LCCO.11.5](#LCCO-11-5)

(LTAC-7-4)=
[LTAC.7.4] No author rule's LHS can reference a timer fact: its schema's name
is synthetic, and no source text writes one ([LTAC.4.1]).

---

*see also:* [LTAC.4.1](#LTAC-4-1)

(LTAC-8)=
## Synthetic rule names [LTAC.8]

(LTAC-8-1)=
[LTAC.8.1] The compiler must name follow-up rules
`$<rule>_after_<N>`, where `<N>` is the source-order index
among `after` clauses on the parent rule. A synthetic name
([LLEX.10.6.1]) can't equal an author-written rule name.

*see also:* [LLEX.10.6.1](#LLEX-10-6-1)

(LTAC-8-2)=
[LTAC.8.2] Source-order indexing is not stable across `after`
clause insertion. An author inserting a new clause may shift the
index of later clauses on the same rule. The cost is a one-time
log-correlation annoyance after edits, and it is accepted. Same
tradeoff as [LCCO.4.3].

*see also:* [LCCO.4.3](#LCCO-4-3)

(LTAC-8-3)=
[LTAC.8.3] The operator log per [LDEB]
surfaces synthetic follow-up names directly. Debug tooling may
render the source location of the originating `after` clause
alongside the synthetic name.

---

(LTAC-9)=
## Timed-action commitments [LTAC.9]

(LTAC-9-1)=
[LTAC.9.1] **Declarative-only.** `after` lowers to a timer fact
plus a follow-up rule; no engine state changes, no
cancellation, no serialisation. Per-fact supersedure on
author-named keys (`as <expr>`) is the mechanism at
[LRST]; it preserves the declarative
core (the supersedure mechanism is the same as
[LCRS.6.2]). Pending `after`s
are not queued.

*see also:* [LCRS.6.2](#LCRS-6-2)

(LTAC-9-2)=
[LTAC.9.2] **Literal durations only.** An `after` duration is a
duration literal. An author needing a runtime
duration writes a sibling rule that asserts a custom timer fact,
so the lowering needs no runtime-computed assertion path.
Restates [LTAC.2.2].

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

(LTAC-9-3)=
[LTAC.9.3] **A duration shorter than the host's pump interval compiles.** The
follow-up fires at the first pump at or after its deadline.

*ratified*

(LTAC-9-4)=
[LTAC.9.4] **Timer facts are opaque to authors.** An author rule
must not pattern-match one. Cross-cutting "is something pending"
rules use author-emitted explicit facts, not introspection of the
timer substrate.

*ratified*

(LTAC-9-5)=
[LTAC.9.5] **`after` composes with `on` / `timeout` / `error`
clauses.** They target different events; the lowering generates
independent sibling rules for each.

(LTAC-9-6)=
[LTAC.9.6] **Capture mechanics are shared with [LCCO.5].** The
compiler reuses the `$CaptureBindings` fact pattern
introduced by call continuations rather than defining a separate
mechanism for `after`.

---

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

