Cycle detection across the rule graph#

DS's cross-cutting stance against unbounded computation, and the compile-time cycle-detection pass that closes the one enforcement gap at the rule-graph level. Zero grammar surface — this is purely a compiler analysis over already-parsed rules.

Paired with:

  • [LSCO] — the bounded scope this doc systematises.

  • [LLOG] — the worst-case cycle shape lives here (rules that defeat each other through negation).

  • [LCRS] — cooldown partially mitigates call cycles but doesn't prevent them.

  • [LERR.1] — cycle rejection is a compile error and fits the compile-error policy exactly.

The stance, named [LCYC.1]#

[LCYC.1.1] DS admits no unbounded computation. The principle is cross-cutting; it is enforced at each of the following points:

ratified — checks: idris×135

  • [LCYC.1.2] A call resolves to a built-in or a unit formula's function, and a formula's body calls only built-ins ([LGRA.9.8.11]), so no call recurses.

see also: LGRA.9.8.11

  • [LCYC.1.3] An action runs at most once per firing, and a rule repeats only by firing again on a new match ([LCYC.1.8]).

ratified — see also: LCYC.1.8

  • [LCYC.1.4] Series have bounded memory: every observation triple compiles to a backing with a known cost characteristic per [LSER.18.1]; unbounded-cost cells are rejected per [LSER.15.2].

see also: LSER.15.2, LSER.18.1

  • [LCYC.1.5] ttl facts expire.

  • [LCYC.1.6] Hydration results have mandatory retain: windows.

Deferred: not binding in this edition.

  • [LCYC.1.7] call has mandatory cooldown (or nocooldown declared explicitly).

  • [LCYC.1.8] Refraction bounds how often a single activation can fire.

  • [LCYC.1.9] let and const bindings require their references to be acyclic: each references only earlier bindings in source order (a const also imported consts — [LGRA.13.3]; let per [LLET]), so reference cycles are structurally impossible.

see also: LGRA.13.3

[LCYC.1.10] The underlying principle: any construct that could grow without bound is either syntactically forbidden or bounded by an author-declared parameter.

Cycle shapes in the rule graph [LCYC.2]#

[LCYC.2.1] The rule graph is the construct that needs the guarantee: rules can produce facts that feed other rules, which can produce facts that feed back into the first rule.

Direct shape [LCYC.2.2]#

[LCYC.2.2.1] A cycle may run through several rules: rule A matches X and emits Y, rule B matches Y and emits Z, and rule C matches Z and emits X, so each rule's emit feeds the next rule's LHS and C's feeds A's.

ratified

[LCYC.2.2.2] A emits Y. B matches Y, emits Z. C matches Z, emits X. A matches X, emits Y again. Left to run, this saturates working memory. It does not run: the compiler rejects the cycle ([LCYC.3.1]) and names the edge that closes it ([LCYC.3.3.1]).

see also: LCYC.3.1, LCYC.3.3.1

The nastiest shape: rules that defeat each other [LCYC.2.3]#

[LCYC.2.3.1] Each rule fires only if the other's fact is absent, and each rule's firing asserts a fact that makes the other's condition false. The smallest case is two rules: x, whose one pattern is no Y and whose RHS is emit X logical, and y, whose one pattern is no X and whose RHS is emit Y logical.

checks: idris×4

[LCYC.2.3.2] There is no sensible runtime behaviour to fall back on. Such a rule set could oscillate, settle on one answer depending on which rule the engine evaluated first, or behave differently across platforms — the author has written something without a clear meaning. That is why [LCYC.3.4] gives this shape its own diagnostic rather than the generic cycle error: the failure is an underspecified rule set, not merely a loop.

checks: idris — see also: LCYC.3.4

Through calls [LCYC.2.4]#

[LCYC.2.4.2] Two rules, each of which calls a service whose response fact or CallError the other matches by correlation, form a cycle: each rule's RHS triggers the other's LHS via the call's outcome ([LCYC.3.2.2]). A rule that matches the outcome of its own call under the same correlation is a one-rule cycle. cooldown bounds the rate but doesn't prevent the cycle — once started, the loop continues indefinitely at the cooldown cadence.

ratified — see also: LCYC.3.2.2

Through hydration [LCYC.2.5]#

Deferred: not binding in this edition.

[LCYC.2.5.1] A rule whose LHS matches hydrated facts issues its own call hydrate_*, bringing in more facts of the same schema, triggering the rule again. Less common than direct cycles but still possible.

The cycle check [LCYC.3]#

[LCYC.3.1] The compiler builds the rule dependency graph at compile time and rejects cycles in it with DS_VALIDATE_RULE_CYCLE.

ratified — checks: souffle idris×23

[LCYC.3.1.1] An SCC of the rule graph is admitted despite [LCYC.3.1] when both hold: no schema is both emitted by a rule of the SCC and matched, positively or under no, by a rule of the SCC; and the SCC consumes nothing its rules produce through a call or the cache, and no rule of the SCC matches, via latest or then, a schema a rule of the SCC emits. Every internal edge of an admitted SCC is then a withdraw edge under [LCYC.3.2.2].

The SCC consumes what a call produces when a rule of it carries the call and a rule of it has a pattern, positively or under no, over the service's response schema or over CallError; for a service declared with no response, over CallError alone ([LCRS.17.1]). The SCC consumes what the cache holds when a rule of it carries a cache or cache_delete of a key and a rule of it reads cached(), in a where or a let, on that key ([LSTO.4.3.1b]). A key that is not a literal is the same key as every other. A call or a cache write whose product no rule of the SCC consumes does not stop the SCC being admitted.

An admitted program stands on one of two grounds, decided by the rule graph before [LCYC.6.1] drops any edge. A program whose every SCC of that graph meets the conditions above stands on the stratification they describe: no SCC emits a schema it matches, so a firing cannot re-enter its own SCC whatever values it emits. A program that meets them only after [LCYC.6.1] has dropped an edge stands on the break's ground: each dropped edge joins an emit to a pattern whose literal test the emitted literal fails ([LCYC.6.1.2]), so the firing creates no activation across that edge, and the SCCs that remain meet the conditions above. The first ground does not read values; the second reads the literal comparison [LCYC.6.1.2] defines, and holds exactly as far as that comparison is by value.

ratified — checks: alloy×8 idris×55 — see also: LCRS.17.1, LCYC.3.1, LCYC.3.2.2, LCYC.6.1, LCYC.6.1.2, LSTO.4.3.1b

[LCYC.3.1.2] An SCC admitted under [LCYC.3.1.1] terminates: from every input working memory, every legal firing sequence reaches a state with no pending activation of any rule of the SCC.

see also: LCYC.3.1.1

What the graph contains [LCYC.3.2]#

  • [LCYC.3.2.1] Nodes: rules.

checks: alloy×2 idris×2

  • [LCYC.3.2.2] Edges: an edge from rule A to rule B exists when A's RHS produces something that could trigger B's LHS, or removes something B's LHS relies on:

    • emit X ... in A's RHS + an X pattern, positive or under no, in B's LHS → edge A → B.

    • call domain.method as "c" in A's RHS + a pattern, positive or under no, over that service's response schema ([LCRS.4.3.2]) or over CallError ([LCRS.4.2.1]) whose corr is "c" or could resolve to it, in B's LHS → edge A → B. The call both produces such a fact ([LCRS.4.2.2]) and retracts the prior one ([LCRS.6.2.1]).

    • cache key: k value: v in A's RHS + let v = cached("k") in B's let-RHS that references the same key → edge A → B.

    • withdraw <binding> of a fact of schema X in A's RHS

      • a positive X pattern in B's LHS → edge A → B.

    • withdraw <binding> of a fact of schema X in A's RHS

      • a no X pattern in B's LHS → edge A → B.

ratified — checks: alloy×6 idris×18 — see also: LCRS.4.3.2, LCRS.4.2.1, LCRS.4.2.2, LCRS.6.2.1

What a cycle looks like [LCYC.3.3]#

[LCYC.3.3.1] Any directed cycle through the graph. The compiler refuses it with DS_VALIDATE_RULE_CYCLE ([LCYC.3.1]), unless [LCYC.3.4.1] gives it the more specific code; what the message names is [LCYC.3.3.2]'s.

ratified — checks: alloy — see also: LCYC.3.1, LCYC.3.3.2, LCYC.3.4.1

[LCYC.3.3.2] The output names rules by name and says exactly what edge closes the cycle — not a theoretical framing, just "these four rules feed into each other in a loop."

ratified

[LCYC.3.3.3] A rule whose RHS both emits a schema and withdraws a binding of the same schema is a one-rule SCC [LCYC.3.1.1] never admits (the schema is emitted and matched by a rule of the SCC). The compiler reports it at the RHS, as DS_VALIDATE_WITHDRAW_EMIT_SAME_SCHEMA, rather than as a graph-cycle diagnostic.

ratified — see also: LCYC.3.1.1

Rules that defeat each other [LCYC.3.4]#

[LCYC.3.4.1] Cycles that cross a no pattern get a more specific diagnostic because the failure is worse than a loop — it is an underspecified rule set. The compiler must refuse a cycle that contains an edge from an emit into a no pattern ([LCYC.3.2.2]) with DS_VALIDATE_RULE_DEFEAT, not DS_VALIDATE_RULE_CYCLE. A withdraw into a no pattern enables its target rather than defeating it, so that edge does not make a cycle a defeat. What the message names is [LCYC.3.4.2]'s.

ratified — checks: alloy×2 — see also: LCYC.3.2.2, LCYC.3.4.2

[LCYC.3.4.2] The message names what went wrong in operator language. No jargon, no need to understand anything beyond "these two rules contradict each other."

Rejection, not resolution [LCYC.3.5]#

[LCYC.3.5.1] Cycles are compile errors. The author restructures their rules. The engine does not try to make cycles work at runtime via clever ordering, partial evaluation, or runtime cycle-breaking. That would complicate the engine with no author-visible benefit.

ratified

[LCYC.3.5.2] This matches DS's existing approach to unbounded-computation risk: syntactically forbidden, not runtime-mitigated.

Properties of the check [LCYC.4]#

  • [LCYC.4.4] No runtime surface. The check runs at compile time; rule files either compile or get a specific error.

  • [LCYC.4.5] No author-visible vocabulary change. The error message names the offending rules.

Limits of the cycle check [LCYC.6]#

  • [LCYC.6.1] Slot-value refinement. A rule that matches Alert a where a.severity > 5 and emits an Alert with severity: 7 is technically self-referential but may not be infinite — it depends on whether re-matching happens with a value that would re-trigger. The compiler applies a literal-equality cycle-breaking check to every edge of the rule graph: if every non-negated LHS pattern the target rule has for the schema carries a binding.slot == X where-clause constraint that the source rule's RHS emit violates (emits slot: Y with literal Y ≠ X), the edge is dropped. The emit provably cannot satisfy that LHS, so it cannot close a loop through it. This handles the common "kind flip" pattern (match kind == "enter", emit kind: "leave") whether the flip sits on one rule or is spread across several, without opening up genuine infinite loops. Non-literal expressions (arithmetic, slot references) remain strict.

ratified — checks: alloy×3

[LCYC.6.1.1] A toggle is not refutable, and the rule count is not what decides it. The break drops an edge only where the emit cannot satisfy the target's LHS. In a literal toggle it can — A: kind == "x" → emit B   kind: "y"; B: kind == "y" → emit A kind: "x" — so neither edge drops, the loop closes, and the compiler rejects it with DS_VALIDATE_RULE_CYCLE. The cycle is real: the toggle oscillates between two stable states and the compiler has no termination proof. Neither a cooldown nor a ttl breaks it ([LCYC.6.2], [LCYC.7.2]). A state that holds while a condition holds is derived as a logical fact, and a transition no condition justifies enters from the host or an adapter ([LLOG.15.3]).

The test is whether the literals close the loop, not how many rules span it. Two rules that both match val == "A" and emit val: "B" and val: "D" respectively close nothing — neither emit satisfies the other's LHS, both edges drop, and there is no cycle.

ratified — see also: LCYC.6.2, LCYC.7.2, LLOG.15.3

[LCYC.6.1.2] What "different literals" means. The check compares two values: (a) the literal on the LHS where-clause binding.slot == X, (b) the literal on the RHS emit's slot: Y. Different here is literal inequality by value, not just textual difference. kind == "enter" vs kind:   "leave" differs; kind == "enter" vs kind:   "enter" does not (the self-edge stays — it is a real cycle). Slot expressions that aren't a pair of comparable literals (slot references, arithmetic, function calls, interpolated strings) leave the edge in the graph: the check is conservative.

Two literals are compared as == compares them. An Int and a Float are a comparable pair, the Int promoted ([LEXP.7.3]): kind == 1 vs kind: 1.0 does not differ, and its self-edge stays; kind == 1 vs kind: 2.0 differs. 0.0 and -0.0 do not differ.

ratified — checks: idris×3 — see also: LEXP.7.3

[LCYC.6.1.3] A bounded staged derivation is expressible. One rule per stage, each guarded where n.level == k and emitting level: k+1, has every backward and re-entrant edge refuted by [LCYC.6.1]: the emitted literal cannot satisfy the guard of any stage but the next. What remains is a DAG across as many stages as the author wrote, so the derivation advances a bounded number of steps and stops.

This does not weaken [LCYC.1.1]. The derivation's length is the number of rules, fixed at compile time, so it is bounded computation on the same terms as that clause's other enforcement points. It is not recursion ([LCYC.1.2]) — there is no rule that invokes itself — and nothing here admits a derivation whose length depends on the fact base.

see also: LCYC.1.1, LCYC.1.2, LCYC.6.1

  • [LCYC.6.2] Transitive edges through ttl facts. A rule emits a ttl fact that expires before downstream rules match. The cycle may still close — just slowly — so the edge remains in the graph. Don't use TTL as a cycle-breaking argument; explicit acyclicity is what the check enforces.

ratified

  • [LCYC.6.3] Cross-tier edges. Cross-tier publish/ subscribe creates edges between rule graphs on different tiers; the receiving tier asserts what crosses under its own schema ([LTIE.9.4]). The compile-time cycle check does not follow those edges — it runs per-tier — so a cycle that closes across a tier boundary is not detected.

see also: LTIE.9.4

  • [LCYC.6.4] Bounded iteration is a staged derivation. An author who needs "fire this up to N times, then stop" (e.g., a counter incrementing until a threshold) writes one rule per stage ([LCYC.6.1.3]), or restructures the rule to avoid the cycle.

see also: LCYC.6.1.3

  • [LCYC.6.5] Correlation-string inference for call cycles. The call/response edge requires matching A's as "c" to B's where r.corr == "c". If the correlation is computed rather than a literal, the check may be unable to prove the match. An unprovable edge is reported as "possible cycle, inspect manually" and does not fail the compile — the check rejects cycles it can prove ([LCYC.3.1]), never ones it merely suspects.

Deferred: not binding in this edition.

see also: LCYC.3.1

  • [LCYC.6.6] Cycle check for let lands naturally here. let bindings (see [LLET]) require acyclic binding references. The general cycle-check pass handles let-internal cycles as a special case of graph traversal; no separate check is needed in the let implementation.

What this doesn't close [LCYC.7]#

  • [LCYC.7.1] Cycles external to DS. An adapter that observes facts DS emits and re-injects them is a cycle the compiler can't see. Adapter boundary is opaque; adapter authors are responsible for not creating feedback loops through their own code.

  • [LCYC.7.2] Slow oscillations within cycle bounds. A cycle that fires once an hour due to TTLs and cooldowns is still a cycle. The compile check rejects it regardless of runtime pacing — "slow" is not "acyclic."

ratified

  • [LCYC.7.3] Correctness of rule logic, only cycle-freedom of the rule graph. A rule set without cycles can still be wrong; a rule set with cycles is definitely wrong. Narrow claim.