Custom rule evaluation
A custom rule lets an edge depend on a decision expressed in code supplied by your application. Use it when the existing context, history and time rules do not express the check you need: for example, a calculation involving several values or a specific business formula.
ExecKernel provides two ways to introduce such decisions. The built-in custom_rule condition names a registered handler, which is a piece of PHP source executed through the configured sandbox driver. A plugin can instead supply a rule type of its own. Both can participate in an edge’s conditions, but they have different registration, parameter and execution contracts.
This page follows the use of an existing custom_rule handler. Writing and registering the implementation is covered in Creating custom rules.
Refer to an existing handler
Section titled “Refer to an existing handler”The Custom rules Showcase illustrates a calculation that a simple threshold does not cover. An application prepares units for delivery. It can handle them individually, accept a base route with at least six units, or require complete boxes of six with no remainder.
The demo starts at node 10. Its “Full boxes” edge is 103, leading to terminal 92. That edge declares two conditions, in this order:
{ "conditions": [ { "edge_id": 103, "rule": "context_value", "params": { "key": "quantity", "operator": ">=", "value": 6 } }, { "edge_id": 103, "rule": "custom_rule", "params": { "handler": "full_boxes", "args": { "box_size": 6 } } } ]}The first condition checks the minimum quantity with a built-in comparison. The second refers to the already installed full_boxes handler. That handler allows the choice when quantity is at least box_size and divides into boxes without a remainder.
The two inputs have different roles. args.box_size is a parameter stored in the graph: here each box holds six units. quantity is a context value supplied by the application for the current choice. The application can try another quantity without rewriting the condition.
In this example, the loader’s allowlist admits the name full_boxes, the runtime registers that name with its actual PHP source, and a sandbox driver is configured to run it. An allowlisted name does not install its code. The graph’s handler field identifies the installed code; it is not an arbitrary callback or a place to submit PHP from a form.
What the evaluation receives
Section titled “What the evaluation receives”For custom_rule, the engine passes the handler the candidate context, the condition’s args, information about the selected edge and current node, and the evaluation time. It also supplies the current node’s metadata and the edge’s labels as sandbox presentation data.
The candidate context combines the session’s current values with the updates supplied for this operation. This is the same mechanism described in Context updates and candidate values: new values can be checked before the session accepts them.
The handler runs before the transition. In the example, it checks the candidate quantity while the current node is still 10; edge 103 identifies the requested connection to 92. Passing the condition does not itself move the session or save anything. The engine must finish the rest of the navigation checks and return the accepted result.
The built-in parameter contract requires a nonempty string handler. If present, args must be an array. The engine also checks that the handler is registered before executing any condition on the edge. That validation establishes the wrapper’s shape and registration; the application’s handler contract defines what its particular arguments mean. Here, box_size is the handler’s own argument, not a universal custom_rule parameter.
A preview leaves the custom decision pending
Section titled “A preview leaves the custom decision pending”The availability model from Condition evaluation and edge availability distinguishes a known result from a deferred decision. For this handler, a normal query checks the built-in minimum quantity but does not execute full_boxes.
When the minimum fails, the edge is blocked. When the minimum passes, the custom decision is still pending, with enabled: true. The latter means your application may offer an attempt; it does not mean the boxes have already been checked.
These are the outcomes for the demo’s condition order, assuming the session and other navigation checks permit the attempt:
Candidate quantity |
Preview of edge 103 |
What happens when the application chooses it |
|---|---|---|
5 |
blocked, enabled: false |
The built-in minimum fails. The handler is not executed. |
6 |
pending, enabled: true |
The handler returns true; one complete box permits the route to terminal 92. |
10 |
pending, enabled: true |
The handler returns false; four units remain, so the transition is rejected. |
12 |
pending, enabled: true |
The handler returns true; two complete boxes permit the route to terminal 92. |
There is no background execution behind pending. A later query with the same quantity remains pending, including after a previous attempt returned false. The preview does not cache that earlier decision as the current availability.
Some problems can be detected without running the handler. For example, an unregistered name produces a blocked evaluation error, and an open custom-rule circuit produces a block. Conversely, a pending result does not prove that the configured driver will be available when needed or that the handler will return a valid result.
Use the normal choice operation
Section titled “Use the normal choice operation”Your application submits a custom-conditioned edge through the same navigation operation it uses for other edges. No separate “approve custom rule” call is required.
In this direct PHP fragment, $engine has the demo’s handler and driver configured, $graph is the loaded demo graph, and $state is its current state at node 10. The application wants to use a quantity of twelve:
use ExecKernel\Domain\Session\Context;
$candidate = new Context(['quantity' => 12]);
$view = $engine->getPayload( $graph, $state, 'en', contextUpdates: $candidate,);The “Full boxes” option in $view is pending. The query has neither run the handler nor changed $state. Once the application requests that choice with the same candidate values:
$result = $engine->choose( $graph, $state, 103, 'en', contextUpdates: $candidate,);The engine validates the edge’s condition contracts, evaluates the minimum and then runs full_boxes. For twelve units, the handler allows the choice. If the remaining navigation checks also pass, $result->state is at terminal 92 with quantity: 12 in its context and edge 103 in its history. A direct-engine integration saves that new state before presenting the progress as confirmed.
A handler is executed when actual condition evaluation reaches it. An earlier false condition or another check that rejects the request before condition evaluation can prevent it from running. Even if your application has already tested the choice on a separate state copy, the real choice evaluates it again.
A false result and a failed execution mean different things
Section titled “A false result and a failed execution mean different things”The handler’s successful return must be a boolean. true means this condition allows the edge; false means this condition rejects it. A numeric 1, a string or another return value is not a successful boolean decision.
For the custom_rule path, direct PHP reports the following outcomes:
| Outcome | What your application receives |
|---|---|
Handler returns false |
ForbiddenEdgeException, code EK-SEC-NAV-002, reason custom_rule_failed. Its blockedEkCode is EK-RULE-003. |
| Handler returns an invalid value | SandboxInvalidReturnException, code EK-RULE-022. |
| The sandbox reports a runtime failure, security violation or timeout | SandboxRuntimeException, code EK-RULE-021. |
| No driver is configured, or the driver reports an internal failure | SandboxDriverUnavailableException, code EK-FATAL-003. |
With ten units, a message such as “Choose a quantity that fills complete boxes” explains the normal false result. A broken handler or unavailable driver calls for a technical-error response; telling the person to change a valid quantity would misdescribe the problem.
A normal false return does not count as a failure for the custom-rule circuit breaker. Execution failures and separate engine performance budgets have their own treatment in Execution budgets and circuit breakers. The driver-reported timeout in the table is distinct from an engine evaluation-budget exception.
These failures do not produce an accepted transition state. Keep the known state when handling a definite local rejection, and use Rejected transitions and error recovery for the broader recovery rules, including requests whose outcome is uncertain.
How a plugin rule differs
Section titled “How a plugin rule differs”A plugin can register another RuleInterface type and have the graph refer to that type directly. The shared idea is an edge predicate; the custom_rule wrapper is not applied automatically.
| Aspect | custom_rule |
Rule supplied as an external type |
|---|---|---|
| Graph reference | rule: "custom_rule" plus params.handler |
The registered type’s own name in rule |
| Parameters | Built-in wrapper checks handler and the shape of args; the handler defines its arguments |
The external rule owns its parameter schema |
| Execution | Registered PHP source through the configured sandbox driver | Its RuleInterface::evaluate() implementation receives parameters, edge, candidate session state and evaluation time |
| Evaluation result | Boolean handler result becomes an allowed or blocked condition result | The implementation returns ConditionEvaluationResult |
| Normal preview | Handler execution is deferred | External-rule execution is deferred |
The sandbox driver and handler circuit used by custom_rule do not automatically wrap external rule types. The latter must be designed and registered according to their own extension contract. Creating custom rules covers that implementation work, while Rule sandboxing and isolation boundaries explains the execution boundaries of the sandbox drivers.
The Showcase’s Test route #103 action adds an application-level experiment: it attempts navigation on a separate copy and discards the resulting progress. That makes it possible to compare quantities before committing a choice in the demo. The actual choice evaluates the rule independently. You can inspect both the fixed handler and its registration through Source code in the Custom rules demo.