# Schema scopes and activation-scoped facts

Schema-level fact lifecycle declarations, under a single `scope`
keyword on the schema. `scope activation` is a lifetime category and
`scope ambient` is a permissions boundary.

Paired with:
- [LFAC] — base schema syntax that
  `scope` extends.
- [LLOG] — `scope logical`
  semantics live there.
- [LCCO] — capture facts
  ([LCCO.5]) are engine-internal uses of the same activation-
  scoped primitive defined here.
- [LRST] — `as <expr>` on `after` for
  per-fact supersedure.
- [LGRA] — the `scope` keyword grammar
  production.
- [LADP] — adapter-
  published schemas adopt `scope ambient`.

## What this is not

- Not a runtime-implementation guide. Engine bookkeeping for
  activation-scoped facts (completion-path enumeration,
  capture-fact reuse) lives with runtime docs.
- Not a queue / serialisation surface. `scope activation` does
  not buffer suppressed activations. Authors who need durable
  work survival use the WorkItem decoupling pattern.

---

(LSCP-1)=
## Declaring a schema's scope [LSCP.1]

(LSCP-1-3)=
[LSCP.1.3] A schema declares its lifecycle with a single `scope`
keyword on the schema declaration that takes a value identifying the
lifecycle category, per the grammar at [LSCP.1.5].

*ratified*  —  *checks:* `idris×4`  —  *see also:* [LSCP.1.5](#LSCP-1-5)

(LSCP-1-4)=
[LSCP.1.4] `scope` is required only when the value is non-
default. Bare `schema Foo:` means `scope explicit`.

(LSCP-1-5)=
[LSCP.1.5] **An author scope is one of four values.** The `scope_value` of
[LGRA.5.1]'s `schema_lifecycle`, whose productions are the grammar's, is
`explicit`, `activation`, `logical`, or `ttl` with a duration; `logical` and
`ttl` may carry the `ambient` modifier ([LSCP.1.7]). The set is closed: no
other value is an author scope.

*ratified*  —  *see also:* [LGRA.5.1](#LGRA-5-1), [LSCP.1.7](#LSCP-1-7)

(LSCP-1-6)=
[LSCP.1.6] A schema declaration carries its lifecycle on a
`scope` clause. The compiler must reject the unprefixed forms
`schema Foo logical:` and `schema Foo ttl <dur>:` with
`DS_VALIDATE_LEGACY_SCHEMA_LIFECYCLE`, naming the offending
form and suggesting the `scope` spelling. Bare `schema Foo:` is
unaffected — it means `scope explicit` ([LSCP.1.4]).

*ratified*  —  *see also:* [LSCP.1.4](#LSCP-1-4)

(LSCP-1-7)=
[LSCP.1.7] `ambient` is a permissions modifier on a base lifecycle
([LSCP.1.5]), not a standalone scope value ([LSCP.2.5]). The compiler
must reject the bare form `schema Foo scope ambient:` with
`DS_VALIDATE_BARE_AMBIENT_SCOPE_RETIRED`, naming the schema and
suggesting the base-lifecycle spelling — a `logical`-based schema, so
every slot takes a `key` / `mutable` marker ([LGRA.5.10]).

---

*ratified*  —  *see also:* [LGRA.5.10](#LGRA-5-10), [LSCP.1.5](#LSCP-1-5), [LSCP.2.5](#LSCP-2-5)

(LSCP-2)=
## Scope values [LSCP.2]

(LSCP-2-1)=
### `scope explicit` (default) [LSCP.2.1]

(LSCP-2-1-1)=
[LSCP.2.1.1] The schema's facts are asserted by `emit` and
retracted by `withdraw` actions in rule RHSs. The engine does
not auto-manage the lifetime.

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

(LSCP-2-1-2)=
[LSCP.2.1.2] This is the bare-`schema Foo:` form's meaning;
authors do not write `scope explicit` unless documenting intent.

(LSCP-2-2)=
### `scope logical` [LSCP.2.2]

(LSCP-2-2-1)=
[LSCP.2.2.1] Facts are subject to logical-CE truth
maintenance: when the LHS that justified the assertion stops
matching, the engine auto-retracts the fact. The semantics are
[LLOG]'s.

(LSCP-2-3)=
### `scope ttl <duration>` [LSCP.2.3]

(LSCP-2-3-1)=
[LSCP.2.3.1] Facts auto-retract after the declared duration
from their assertion time. Existing v1 semantics; the duration
syntax matches [LTIM.1.3].

*see also:* [LTIM.1.3](#LTIM-1-3)

(LSCP-2-4)=
### `scope activation` [LSCP.2.4]

(LSCP-2-4-1)=
[LSCP.2.4.1] Facts are asserted by an `emit` in a rule's
activation RHS and retracted on every completion path of that
activation: synchronous end of RHS, every `after` timer
fire per [LTAC], every `on response` match, every `on timeout`
fire, every `on error` fire per [LCCO]. A capture fact of a call with more than
one `timeout` clause is not retracted by a `timeout` fire ([LCCO.7.3]).

A path that can no longer complete discharges the facts it carries. When the
token fact carrying a deferred path, a timer or a capture, is superseded by an
`as`-keyed re-fire ([LRST.2.2]) or otherwise withdrawn before it fires, the
engine **must** retract the activation's facts attached to that path at that
withdrawal, as it would have at the path's completion; a fact attached to
more than one path is retracted once, when the first of them ends. The
withdrawal's cause is `ACTIVATION_COMPLETED` ([RFCS.7a.2]).

*ratified*  —  *see also:* [LCCO.7.3](#LCCO-7-3), [LRST.2.2](#LRST-2-2), [RFCS.7a.2](#RFCS-7a-2)

(LSCP-2-4-2)=
[LSCP.2.4.2] The engine must enumerate completion paths the
same way it does for capture-fact lifetime per [LCCO.5.3] and
emit a retract on each path. Activation-scoped facts and capture
facts share runtime bookkeeping.

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

(LSCP-2-4-3)=
[LSCP.2.4.3] The schema may declare slots; each instance carries
the slot values of the `emit` that asserted it. Two activations with
different slot values produce different fact instances; they do not
dedupe.

(LSCP-2-4-4)=
[LSCP.2.4.4] The fact is a real fact: observable in operator log
entries, queryable by other rules' LHSs. External pattern-match
on a `scope activation` fact does not auto-retract it (`scope
activation` is not `scope logical`).

(LSCP-2-5)=
### The `ambient` modifier [LSCP.2.5]

`ambient` is a read-only permissions modifier over a base lifecycle,
written `scope <base> ambient` ([LSCP.1.5]). The base — `logical` or
`ttl` — supplies the fact's lifecycle and identity; `ambient` adds only
the read-only boundary. v1 admits `scope logical ambient` only
([LSCP.2.5.9]).

*see also:* [LSCP.1.5](#LSCP-1-5), [LSCP.2.5.9](#LSCP-2-5-9)

(LSCP-2-5-1)=
[LSCP.2.5.1] Facts are read-only from rule code. Rule LHSs may
pattern-match on them; rule RHSs must not `emit` or `withdraw`
them. The compiler must reject any `emit` or `withdraw` action
targeting a `scope <base> ambient` schema in a rule's RHS.

The error message names the schema and points the author at
emitting a request schema (e.g. `LightRequest`) instead.

*ratified*

(LSCP-2-5-2)=
[LSCP.2.5.2] The fact must be populated from outside rule
logic: an adapter emitting the schema, a cross-tier
propagation, a test harness. The schema's
declaration asserts that some non-rule source populates it.

*ratified*

(LSCP-2-5-3)=
[LSCP.2.5.3] The `ambient` modifier ([LSCP.1.7]) is the permissions boundary that
prevents rule-side spoofing of adapter-published facts. Adapter-
published schemas should carry it per
[LADP].

*see also:* [LSCP.1.7](#LSCP-1-7)

(LSCP-2-5-5)=
[LSCP.2.5.5] Cross-tier emit per [LLOG] counts as a non-rule
source for `scope ambient` purposes. A rule at tier Y receiving
a `scope ambient` fact via `publish X` from tier X does
not violate [LSCP.2.5.1] — the publisher is the cross-tier
propagation, not the receiver's rule code.

*see also:* [LSCP.2.5.1](#LSCP-2-5-1)

(LSCP-2-5-6)=
[LSCP.2.5.6] A multi-row **ambient relation** passes the containers
gate ([LCNT.2.1]),
on the same footing as the logical relation ([LLOG.2.7.4]): it is a
fact population matched individually by the engine's bounded,
non-authored match loop, not an author-iterable value. DS must not
grow a surface that hands the author the ambient row-set as a set or
cursor value. A `scope logical ambient` relation acquires keyed
identity from its `logical` base ([LFAC.1.7]) — the `key = all`
markers make distinct tuples distinct rows.

*see also:* [LCNT.2.1](#LCNT-2-1), [LFAC.1.7](#LFAC-1-7), [LLOG.2.7.4](#LLOG-2-7-4)

(LSCP-2-5-7)=
[LSCP.2.5.7] The `ambient` modifier composes with a base lifecycle:
the base supplies lifecycle and identity, `ambient` supplies only the
read-only boundary ([LSCP.2.5.1] / [LSCP.2.5.3]). The populating source
drives assertion and retraction *within* the base lifecycle — for a
`logical` base, keyed upsert / one-live-per-key ([LFAC.1.7]); for a
`ttl` base, accumulation.

*see also:* [LFAC.1.7](#LFAC-1-7), [LSCP.2.5.1](#LSCP-2-5-1), [LSCP.2.5.3](#LSCP-2-5-3)

(LSCP-2-5-8)=
[LSCP.2.5.8] An ambient fact is asserted by a non-rule source
([LSCP.2.5.2]), never `emit X logical`, so it carries **no TMS
justification link** ([LLOG.15]): the source owns lifetime, the base
gives keyed identity, and rules match it read-only.

*see also:* [LLOG.15](#LLOG-15), [LSCP.2.5.2](#LSCP-2-5-2)

(LSCP-2-5-9)=
[LSCP.2.5.9] v1 admits `scope logical ambient` only. The
compiler must reject `scope ttl <dur> ambient` with
`DS_VALIDATE_TTL_AMBIENT_NOT_IN_V1`.

*ratified*

(LSCP-2-6)=
### `scope internal` (built-in schemas only) [LSCP.2.6]

(LSCP-2-6-1)=
[LSCP.2.6.1] An `internal` schema has the `explicit` lifecycle
([LSCP.2.1]), except that only the engine asserts its facts. Rule LHSs may
pattern-match on them and rule RHSs may `withdraw` a fact they matched
([LSCO.4.5.9]); the compiler must reject an `emit` of an `internal` schema as
`DS_VALIDATE_INTERNAL_EMIT_FORBIDDEN`.

*ratified*  —  *see also:* [LSCO.4.5.9](#LSCO-4-5-9), [LSCP.2.1](#LSCP-2-1)

(LSCP-2-6-2)=
[LSCP.2.6.2] `internal` is not an author scope: it is absent from the
`<scope-value>` grammar ([LSCP.1.5]), so no author schema can declare it. The
built-in schemas that carry it are `ComputationError` and `TypeError`
([LERR.9.4]), which the built-in module declares ([LIMP.12.4]). They are registered schemas like any other, and an integration
enumerating schemas sees them with the lifecycle `internal` ([NDBG.2.2]). A
schema an integration feeds and rules may only read is `ambient`
([LSCP.2.5]).

---

*see also:* [LERR.9.4](#LERR-9-4), [LIMP.12.4](#LIMP-12-4), [LSCP.1.5](#LSCP-1-5), [LSCP.2.5](#LSCP-2-5), [NDBG.2.2](#NDBG-2-2)

(LSCP-3)=
## The activation lifecycle [LSCP.3]

(LSCP-3-1)=
[LSCP.3.1] `scope activation` ties a fact's lifetime to a
rule's activation. The lifecycle:

1. Rule's LHS matches; activation enters the agenda.
2. Engine selects the activation per agenda priority.
3. RHS begins executing.
4. Each `emit` of a `scope activation` schema in the RHS asserts
   its fact when the `emit` executes.
5. RHS completes its synchronous actions.
6. Async tail begins: any `after` timers asserted, any `call`
   continuations awaiting responses.
7. **As each completion path resolves** (timer fire, response
   match, timeout fire, error fire), the engine retracts the
   activation's `scope activation` facts.
8. When the last completion path resolves, the activation is
   "done" and any remaining `scope activation` facts are
   retracted.

(LSCP-3-2)=
[LSCP.3.2] For a synchronous rule (no `after`, no `call`
continuations), the activation completes within one pump and
the `scope activation` fact is asserted-and-retracted within
that pump.

*ratified*

(LSCP-3-3)=
[LSCP.3.3] Multiple completion paths on one activation each
retract the activation's facts independently. The engine must
treat retraction as idempotent — each path's retract is a no-op
if the fact is already gone. Refcounts are not required because
a given activation asserts its facts once.

(LSCP-3-4)=
[LSCP.3.4] Capture facts per [LCCO.5.2] are engine-named uses
of the same activation-scoped lifecycle. The compiler
synthesises `$CaptureBindings_<rule>_c<N>` facts that the
runtime asserts and retracts on the same completion paths it
tracks for an author-emitted `scope activation` fact. Both share
runtime bookkeeping; only the fact-naming differs.

---

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

(LSCP-6)=
## Scope commitments [LSCP.6]

(LSCP-6-1)=
[LSCP.6.1] **Single `scope` keyword for fact-lifetime
declarations.**

*ratified*

(LSCP-6-2)=
[LSCP.6.2] **Default is `scope explicit`.** Bare
`schema Foo:` declares it.

(LSCP-6-3)=
[LSCP.6.3] **`scope activation` is a lifetime category.**
Asserted by an `emit` in the activation's RHS; retracted on every
completion path enumerated by the compiler.

(LSCP-6-4)=
[LSCP.6.4] **The `ambient` modifier ([LSCP.1.7]) adds a permissions boundary.**
Rules may not `emit` or `withdraw` ambient schemas. Adapter-
published schemas adopt this convention.

*ratified*  —  *see also:* [LSCP.1.7](#LSCP-1-7)

(LSCP-6-5)=
[LSCP.6.5] **Activation-scoped facts are real facts.**
Observable in the event trace; queryable by other rules'
LHSs; participate in standard truth-maintenance per their
scope.

(LSCP-6-6)=
[LSCP.6.6] **Capture facts in CALL_CONTINUATION are engine-
internal uses of `scope activation`.** One primitive, two
consumers.

(LSCP-6-11)=
[LSCP.6.11] **Scope keyword is required for non-default
values.** Bare `schema Foo:` means `scope explicit`.
Restates [LSCP.1.4].

---

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

