Skip to content

Rejected transitions and error recovery

When a choice fails, your application needs to establish two things: why it failed and which state it can use to continue. Those answers determine whether to explain a blocked option, correct a request, reload the session or wait for a dependency to recover.

A rejected calculation in the PHP engine leaves the supplied state object unchanged. A failure while saving or delivering the result needs a different investigation: the next state may already have reached storage. Treat the stage at which the failure occurred as part of the error, not just its message.

Consider the learning-path graph introduced in Anatomy of a graph. At node 10, edge 101 selects a reading guide and edge 102 selects practice. Practice requires practiceEnabled: true.

Here, $engine is a configured PHP engine, $graph is the loaded definition, and the session has practiceEnabled: false. A stale interface or a crafted request might still submit edge 102. The engine checks it again:

use ExecKernel\Exception\ForbiddenEdgeException;
try {
$chosen = $engine->choose($graph, $state, 102, 'en');
$state = $chosen->state;
$payload = $chosen->payload;
$message = null;
} catch (ForbiddenEdgeException $error) {
$practiceBlocked =
$error->rule === 'context_value'
&& $error->reason === 'context_value_failed'
&& ($error->context['edge_id'] ?? null) === 102;
if (!$practiceBlocked) {
throw $error;
}
$payload = $engine->getPayload($graph, $state, 'en');
$message = 'Practice is not available. You can read the guide.';
}

For this known rule failure, the catch rebuilds the choices from the state the application already holds. The session remains at node 10; edge 102 is blocked and 101 remains available. The message is wording chosen by the application for this example. It is not a technical error message provided by ExecKernel.

The assignments after choose() run only on success. A rejected choice does not add the attempted edge to the history or adopt proposed context changes. Context updates and candidate values demonstrates that behavior with an accepted and a rejected draft.

This recovery works while the session and its graph remain valid. If rebuilding the payload itself fails, for example because the graph revision is no longer compatible, let that failure reach the application’s corresponding error path. Do not keep showing the old screen as if its options were still usable.

ForbiddenEdgeException uses the outer code EK-SEC-NAV-002 for a choice blocked by condition evaluation. That code alone does not tell your application that the user failed a business requirement.

The exception also exposes rule, reason, blockedEkCode and the associated details. For the built-in context comparison above, the reason is context_value_failed; when the key is present but its value does not match, the inner code is EK-RULE-003.

Other evaluations can fail because a custom handler raised an error, exceeded its budget or was stopped by its circuit breaker. Those failures must not be presented as “your answer is wrong”. The condition and its failure details identify the path your application should handle. Custom rule evaluation explains that distinction.

Use the typed error and structured fields for application decisions and diagnostics. Map expected cases to messages appropriate for your users. Raw exception text and context may contain implementation information that does not belong in the interface.

Choose a recovery action that fits the failure

Section titled “Choose a recovery action that fits the failure”

The following situations call for different actions. They are recovery decisions, not a complete error-code catalogue.

Situation What it tells the application Useful next action
A known condition evaluates to false That choice is not allowed with the evaluated inputs Explain the requirement and refresh the available options
The edge does not belong to the current node The submitted choice does not match this state Recover the current step and discard the stale selection
An input fails context or parameter validation The engine could not accept the submitted data Correct the input or integration before submitting again
The session is terminal, expired or incompatible with the loaded graph Normal navigation cannot continue from that supplied state Show completion or use the application’s session/version recovery path
A rule dependency or execution budget fails Evaluation could not complete normally Inspect the cause and restore the dependency or correct the slow work
Saving, a write conflict or response delivery fails The application’s previous view may not reflect stored progress Read the authoritative session before deciding on another choice

For navigation, an unknown edge or an edge from a different node produces InvalidEdgeException with EK-SEC-NAV-001. Trying to continue from a terminal produces TerminalNodeTransitionException with EK-STATE-004. These cases need a fresh understanding of the current position, rather than a different answer to a condition.

A lock conflict, a rule timeout and an expired session do not share one retry policy. Some failures need changed inputs, others need current state, and others need operational intervention. Execution budgets and circuit breakers explains the budget cases; Error codes and exceptions provides the complete contracts.

Recover after an uncertain save or response

Section titled “Recover after an uncertain save or response”

Suppose your application chooses edge 101, calculates terminal node 20 and saves it. The connection then fails before the browser receives confirmation. The browser still displays node 10, but storage already contains node 20.

Repeating edge 101 from the browser’s old view is not a way to discover what happened. First recover the state associated with the same application session identifier.

With a configured core SessionStoreInterface, the essential recovery step is:

$storedState = $store->load($sessionId);
if ($storedState === null) {
throw new RuntimeException('Session state is unavailable.');
}
$resumed = $engine->resume(
$graph,
$storedState,
'en',
sessionId: $sessionId,
);
$state = $resumed->state;
$payload = $resumed->payload;

Here, $store is your integration’s existing session store, and $sessionId is its identifier. The graph must be the one associated with the recovered session. The null branch is an application decision point: the sample stops rather than pretending that a new session is the old one.

If storage contains node 20, resume returns that terminal and the interface can show the guide result. If it contains node 10, the application can refresh its choices and decide whether another submission is appropriate. A successful read establishes the state visible to that store; it does not by itself prove what happened in a separate external service.

The standalone HTTP API follows the same distinction through its session operations: status reports the stored position, and resume rebuilds the presentation from the stored session. Their request shapes belong to HTTP API endpoints and payloads; client transport recovery is covered in Client errors, transport and diagnostics.

Preserve what you know, and resolve what you do not

Section titled “Preserve what you know, and resolve what you do not”

The key boundary is confirmation. Before a successful calculation, your application still has its original state. After an attempted write whose outcome is unknown, the original variable is only the application’s last known view.

Even a persistence wrapper cannot turn every exception into proof of rollback. A backend may write and then lose its acknowledgement. Conversely, a confirmed write must not be reported as if it never happened merely because it was slow. Execution lifecycle and confirmed state explains how calculation, storage confirmation and result publication fit together.

A session recovery also does not undo a payment, email or other external action already performed by your application. Reconcile those actions using their own identifiers and confirmations before repeating them. Concurrency controls and client retry behavior are developed in Concurrency and session revisions and the integration guide linked above.

The complete recovery example exercises both cases: a practice choice rejected before any new state is adopted, and an in-memory storage adapter that writes a guide selection and then loses its acknowledgement. Reading the store and resuming reveals node 20 without choosing the edge again. That second case is an explicit simulation of an uncertain confirmation, so the state change and recovery remain visible without requiring a network failure.