Skip to content

Cycle detection and protection

A graph contains a cycle when a route can lead back to a node it has already passed through. A useful process may need such a return: someone reads guidance, checks their data and goes back to the available choices. ExecKernel permits cycles between different nodes when the graph meets its structural requirements.

During a session, the engine applies a more specific protection: an edge already recorded in that session’s history cannot be taken again. Returning to a node through unused edges can succeed. Attempting an earlier edge a second time is rejected. This distinction lets your application offer a return without turning that route into an indefinitely repeatable loop.

We will reuse the request-preparation graph introduced in Context and history-based rules. Root node 10 offers preparation choices. Edge 201 leads to guidance at node 15; edge 206 continues to checklist node 20; edge 203 returns to the choices at node 10. Edge 204 offers an exit to terminal 30, provided the checklist has been visited and your application supplies readyToSend: true.

The figure shows the part of that graph relevant to the return and the exit. The quick-check route and guidance-recap ending from the complete graph are omitted.

An allowed circuit uses edges 201, 206 and 203 to return from node 10 through guidance 15 and checklist 20 to node 10. Trying edge 201 again is blocked because it is already in history. Unused edge 204 can still reach terminal 30 when its conditions pass.

Here is what happens when your application requests the three edges in order:

Choice Position after acceptance Why cycle protection allows it
201 Guidance, node 15 Edge 201 has not been used.
206 Checklist, node 20 Edge 206 has not been used.
203 Options, node 10 Edge 203 has not been used, even though node 10 was visited at the start.

After the return, the relevant history is:

{
"nodes": [10, 15, 20, 10],
"edges": [201, 206, 203]
}

Node 10 appears twice. None of the edges appears twice. CycleBreaker checks the edge history, so that repeated node does not trigger a rejection.

Taking the same edge again is a different operation

Section titled “Taking the same edge again is a different operation”

At node 10, requesting edge 201 again would reuse the first edge of the route. Its other condition still passes in this session, but CycleBreaker finds 201 in history and rejects the move.

The ordinary node payload already describes this choice as blocked. These are the relevant fields for edge 201:

{
"id": 201,
"enabled": false,
"disabled_reason": "cycle_blocked",
"availability": "blocked"
}

Your application can disable or explain the choice using that result. A direct request remains protected even if an old screen still offers it.

For a local PHP integration, the rejected choice throws RaulCycleException. In the following fragment, $engine is the configured core engine, $graph is the loaded request-preparation graph, $state is the state after 201, 206, 203, still with readyToSend: false, and $lang is the selected language:

use ExecKernel\Domain\Session\Context;
use ExecKernel\Exception\RaulCycleException;
try {
$state = $engine->choose(
$graph,
$state,
201,
$lang,
contextUpdates: new Context(['readyToSend' => true]),
)->state;
} catch (RaulCycleException $error) {
$rejection = [
'ek_code' => $error->ekCode,
'edge_id' => $error->context['edge_id'],
];
$payload = $engine->getPayload($graph, $state, $lang);
}

The rejection data is:

{
"ek_code": "EK-SEC-CYC-001",
"edge_id": 201
}

The original state remains at node 10, with edges [201, 206, 203]. The proposed update is also unaccepted: readyToSend remains false. Your application can keep that state, explain that the route was already used, and display the current choices. Repeating the same request with unchanged inputs will encounter the same protection.

A different unused edge can still be valid. In this example, choosing 204 with the validated readyToSend: true value succeeds and returns the “Request ready” terminal. The engine adds 204 and node 30 to history; it does not remove the earlier circuit.

The complete PHP example, used with the JSON graph, follows the accepted return, captures this rejection and then takes the valid exit. It assumes the installed core autoloader and JSON file beside the script, and fixes the clock for a reproducible observation.

A structural cycle still needs a way to finish

Section titled “A structural cycle still needs a way to finish”

Cycle protection operates on a session. Graph validation answers an earlier question: whether the definition has a structure the engine can execute.

Every node must be reachable from the root and must have a structural path to a terminal. A nonterminal needs an outgoing edge, and a terminal has no outgoing edges. A cycle through several nodes can satisfy these requirements because an exit leads to a terminal. A closed circuit without such a path cannot.

A direct self-loop is a separate invalid case: an edge whose source and destination are the same node is rejected when constructing or loading the graph. Adding a terminal exit does not make that self-loop valid.

Definition Structural result
Different nodes form a cycle, with a path from every node to a terminal The cycle itself does not invalidate the graph. Other validation requirements still apply.
A reachable part of the graph has no path to a terminal Rejected with the underlying validation code EK-GRAPH-051.
An edge starts and ends at the same node Rejected with the underlying validation code EK-GRAPH-031.

When these definitions are rejected through JsonGraphLoader, the top-level error is EK-SEC-LOD-001; its validation_ek_code retains the underlying validation code shown above. Graph validation and diagnostics explains how to use that distinction when repairing a definition.

A structural path ignores the current context and the session’s earlier choices. Therefore, a valid graph does not guarantee that every session has an available exit at every moment. A condition may block it, or a required edge may already have been used. Check the actual return states your application can reach, including their context and history.

The request-preparation graph treats the guided and quick branches as alternatives within a session. This avoids offering a second trip to the shared checklist whose return edge 203 has already been used. That is a graph-design decision expressed through conditions; the cycle breaker remains responsible for refusing any repeated edge.

The protection depends on a complete, trusted history. The default engine history policy preserves earlier edges, including those from long routes. Saving and restoring a session should retain that history. Removing old entries to make an edge available also removes the information used by history rules and cycle protection.

An optional depth limit serves a different purpose. FlowSecurityPolicy::maxDepth counts accepted edges and can stop a session after a chosen number of transitions, even if every edge is new. Its default is null, so there is no depth cap from that policy unless your application configures one. A history rejection policy is another possible limit; it stops growth rather than silently forgetting earlier choices. Execution budgets and circuit breakers explains those controls.

Design a return around what the person must be able to do after arriving back. Provide a usable exit for the relevant state, and model any finite second review as its own intended steps and edges. A request to choose() still attempts one transition; the existence of a cycle does not make the engine traverse it automatically.

For a rejected navigation, keep the last accepted state and use the reported reason to choose the next action. Rejected transitions and error recovery develops that recovery flow and distinguishes a definite navigation rejection from uncertain persistence.