Context updates and candidate values
Your application often learns something after a session has started: a form answer, a selected preference or a value retrieved from its own services. To use that information in the next decision, it sends a context update with the operation.
ExecKernel combines the current context with those updates before evaluating the choice. That combined set of values is the candidate context. It lets the engine answer “would this choice be allowed with these values?” without first changing the session you already have.
A value that changes an available choice
Section titled “A value that changes an available choice”We will use the learning-path graph from Anatomy of a graph. At node 10, edge 101 leads to a reading guide and edge 102 leads to browser practice. The practice edge requires the boolean context value practiceEnabled to be true.
Assume $engine is configured and $graph contains that graph definition. We start with practice disabled and a second value that will help us see what the merge preserves:
use ExecKernel\Domain\Session\Context;
$state = $engine->start( $graph, 'en', new Context([ 'practiceEnabled' => false, 'topic' => 'graphs', ]),)->state;
$updates = new Context(['practiceEnabled' => true]);The application has now decided to enable practice. Creating $updates does not modify $state. It only prepares the values to supply to the next call.
The topic key is an application value introduced for this example. This graph has no rule that reads it; it makes the preservation of untouched keys visible.
Preview with the candidate values
Section titled “Preview with the candidate values”Pass the update to getPayload() to see the choices under the proposed context:
$preview = $engine->getPayload( $graph, $state, 'en', contextUpdates: $updates,);For this call, the engine evaluates with practiceEnabled: true and topic: "graphs". Edge 102 therefore has availability: "available". Your application can display the practice option as available in the form’s current draft.
The original state still has practiceEnabled: false. Its node, history and timestamps also remain unchanged. If you request another payload without the update, practice is blocked again:
$withoutDraft = $engine->getPayload($graph, $state, 'en');This is useful for an interface that previews options as the user edits a form. The interface must retain its draft separately. A payload calculated with draft values does not save that draft in the session.
Supply the values again when choosing
Section titled “Supply the values again when choosing”When the user confirms the practice choice, include the update in choose():
$chosen = $engine->choose( $graph, $state, 102, 'en', contextUpdates: $updates,);
$nextState = $chosen->state;The engine prepares the candidate again and re-evaluates the choice. It does not rely on the earlier preview. When this call succeeds, $nextState is at node 30 and contains the merged context:
{ "practiceEnabled": true, "topic": "graphs"}The selected edge and its destination are added to the history only after the choice is accepted. During condition evaluation, the candidate still has the pre-transition node, history and timestamps. A rule cannot treat the edge being considered as already selected merely because the application has proposed it.
If you omit contextUpdates from choose(), the engine uses the context in $state. In this example it would see false and reject edge 102, even though the previous preview showed it as available. Pass the values associated with the choice the application is actually submitting.
A rejection does not apply the draft
Section titled “A rejection does not apply the draft”Consider a different proposed update, still starting from the original node 10:
use ExecKernel\Exception\ForbiddenEdgeException;
$rejectedUpdates = new Context([ 'practiceEnabled' => false, 'topic' => 'conditions',]);
try { $rejected = $engine->choose( $graph, $state, 102, 'en', contextUpdates: $rejectedUpdates, );} catch (ForbiddenEdgeException $error) { $currentPayload = $engine->getPayload($graph, $state, 'en');}The practice condition fails. There is no successful EngineResult to adopt. The original state keeps both practiceEnabled: false and topic: "graphs"; the unrelated topic change is not partly applied.
The catch here handles this known built-in condition example. A general recovery handler must also distinguish rule evaluation failures, invalid selections and unavailable dependencies, as explained in Rejected transitions and error recovery.
| Operation from the original state | Values used to evaluate | State your application receives |
|---|---|---|
Preview with practiceEnabled: true |
true, original topic |
Payload only; original state unchanged |
Choose 102 with practiceEnabled: true |
true, original topic |
New state at 30 with the merged context |
Choose 102 with false and a new topic |
false, proposed topic |
Exception; original state unchanged |
These are separate calls from the same initial state. They are not three successive transitions.
Understand what the merge accepts
Section titled “Understand what the merge accepts”An update replaces values by top-level key. Keys absent from the update retain their current values. This makes a small update useful: the application can send just the values that changed.
The same rule applies if you have explicitly enabled arrays through ContextValidationPolicy. Updating details replaces its entire value; it does not recursively merge nested keys. Under the default policy, arrays are disabled. null is not a supported context value and cannot be used to delete a key.
Preserve the intended PHP types when building a Context. The boolean true is not the text "true"; in this graph’s equality condition, that distinction changes the result. Convert and validate form values at the application boundary.
The engine validates the candidate before running conditions. An update can therefore be rejected because of its key policy, value type, merged entry count or resulting state size. In that case there is no condition result to interpret as “the user is ineligible”: the application must correct the submitted data or its integration.
These calls describe the local PHP contract. HTTP inputs pass through a separate request contract, including its context restrictions; see HTTP API endpoints and payloads when integrating through the service.
Keep the draft and the saved session distinct
Section titled “Keep the draft and the saved session distinct”After a successful choice, your application can adopt $chosen->state and persist it through its session integration. The candidate mechanism determines what goes into that returned state; it does not itself confirm a database write.
If the application needs to preserve an unfinished form without choosing an edge, save that draft through the application’s own form workflow. getPayload() is a query, and choose() combines a choice with the values needed for that choice.
The complete context example runs the available preview, the accepted choice and the rejected draft against the same starting state. For a visual exploration, open the Context conditions demo, change its input values and compare the reported choices. The demo uses its own graph; the relationship to observe is how a context value changes availability.