Skip to content

Version compatibility and session upgrades

When your application publishes a graph revision, existing sessions still refer to the revision they were using. To continue one of those sessions, your application can retrieve its original definition or explicitly authorize a compatible update. The engine does not infer that every newer version is suitable.

This guide uses the core SessionVersionUpgradePolicyInterface to resolve that decision. An accepted update may change the session’s graph and DSL revision fields while preserving its existing position, context and history. It takes effect as part of a successful choose(); your application then confirms the returned state in its store.

Establish which definition the session needs

Section titled “Establish which definition the session needs”

We return to expense-approval from Graph identity and versioning. A request begins at node 10, is submitted through edge 101 to the review at node 20, and can be approved through edge 201 to terminal 90.

Our saved session has already submitted the request under graph version 1.0.0. It is at node 20, with history nodes [10, 20] and edges [101]. Version 1.1.0 changes the review title but retains those nodes and connections.

Before calculating another choice, the engine compares the graph’s graphVersion and dslVersion with the session’s values. A difference raises VersionCompatibilityException (EK-GRAPH-060) unless a configured policy resolves it. Identity must still match: a policy cannot turn a session for one graph code into a session for another process.

There are two useful resolutions:

Your application’s decision Policy result What happens to the session
Continue on its original definition. Return the original Graph, retrieved from your catalog. The session keeps its revisions and can be read, resumed or advanced with that graph.
Continue on a reviewed compatible definition. Return a state whose revision fields match that target. The engine checks the proposal and can apply it during a choice.

For the first option, a resolver can return SessionVersionUpgradeDecision::allow(graph: $originalGraph). Here $originalGraph must be the definition matching the session’s code and stored revisions. Your application retrieves it; the policy interface does not provide an archive or a “latest compatible” lookup.

Limit the upgrade policy to the target you reviewed

Section titled “Limit the upgrade policy to the target you reviewed”

A policy receives a SessionVersionMismatch containing the supplied graph, the current session and both pairs of versions. It returns an allowed resolution or a blocked decision.

The example policy, VersionsReviewedUpgrade, receives the source version and the exact target Graph object that the application has reviewed. Its resolver is:

public function resolve(SessionVersionMismatch $mismatch): SessionVersionUpgradeDecision
{
if ($mismatch->graph !== $this->target
|| $mismatch->state->graphCode !== $this->target->graphCode
|| $mismatch->sessionGraphVersion !== $this->sourceVersion
|| $mismatch->sessionDslVersion !== $this->target->dslVersion
|| $this->target->graphVersion === null) {
return SessionVersionUpgradeDecision::block('No reviewed upgrade for this session.');
}
$data = $mismatch->state->toArray();
$data['graph_version'] = $this->target->graphVersion;
return SessionVersionUpgradeDecision::allow(state: SessionState::fromArray($data));
}

This is the method from the complete policy class. It limits the change to the named source version and the supplied target object, keeps the DSL version unchanged and copies every other state field. It does not derive compatibility merely from the target having a larger number.

The application still has to review the target’s behavior and obtain it from a trusted catalog. Preserving historical IDs is necessary, but it does not by itself prove that changed rules or business meanings are acceptable. An exact reviewed target and an explicit permitted source make that decision visible in your configuration.

The engine also checks the proposal independently. Returning allow() without resolving the mismatch is insufficient. A returned state must match the resolved graph’s graph and DSL revisions, and the policy cannot erase a known revision or alter unrelated state fields.

Load the target definition and inject the policy when building the engine:

use ExecKernel\Infrastructure\Engine\ExecKernelFactory;
use ExecKernel\Infrastructure\Loader\JsonGraphLoader;
use ExecKernel\SessionStore\HashStateVersioning;
$target = (new JsonGraphLoader())->load(
__DIR__ . '/versions-expense-1.1.json',
);
$engine = (new ExecKernelFactory())->build(
versionUpgradePolicy: new VersionsReviewedUpgrade('1.0.0', $target),
stateVersioning: new HashStateVersioning($backing),
);

Here $backing is the store containing the original session. As in Concurrency and session revisions, $store below is the conditional wrapper around that backing store. It protects the final write against another request changing the session during the operation.

Read the session and retain its original hash. The next operation approves the expense through edge 201:

$before = $store->load($sessionId)
?? throw new RuntimeException('Session missing.');
$expectedHash = $store->getStateHash($before);
$result = $engine->choose(
$target, $before, 201, 'en',
sessionId: $sessionId,
expectedStateHash: $expectedHash,
deferEvents: true,
);

The version precondition checks the original saved state before the policy runs. The policy cannot repair a stale request by changing its revision.

After accepting the revision proposal, the engine performs the requested transition normally. The two stages have different effects:

State field Saved before the operation Revision proposal Returned after choosing 201
graph_version 1.0.0 1.1.0 1.1.0
dsl_version 1.0 1.0 1.0
Current node 20 20 90
History edges [101] [101] [101, 201]
Context {"costCenter":"engineering"} Unchanged Unchanged
Creation time Original time Unchanged Unchanged

The proposal does not move the session or manufacture a new history. The subsequent choice adds the real transition and updates its timestamps. The engine retains the original context policy and history objects when applying the revision-only proposal.

The engine has returned a candidate. Confirm it using the original token, then publish its deferred result events:

try {
$saved = $store->saveWithExpectedHash(
$sessionId, $result->state, $expectedHash,
);
if (!$saved->persisted) {
throw new RuntimeException('The upgraded session was not saved.');
}
} catch (Throwable $error) {
$result->eventBatch?->discard();
throw $error;
}
$result->eventBatch?->publish();

After a confirmed save, the store contains version 1.1.0, node 90 and the complete route [101, 201]. Your application can show the approved result. If the save conflicts, discard this candidate, reload the confirmed state and decide what the user can do next. Neither the old PHP object nor the stored session is upgraded just because the policy returned an allowed proposal.

The complete session-upgrade example combines the walkthrough with the cases below. Place it in your project’s root, alongside vendor/, with the policy, the application file lock and the 1.0 definition and 1.1 definition. It uses an isolated temporary store and a fixed clock so that the before/after state is visible in its output.

Recognize a resolution that cannot be applied

Section titled “Recognize a resolution that cannot be applied”

A query must not silently change a saved revision. getPayload() and resume() can use a policy that returns the original graph, but they reject a policy that changes the state. The example’s upgrade proposal produces the reason revision_change_requires_confirmed_transition for both read operations.

A revision proposal must also preserve a complete route that still exists in the target. For every historical edge, the engine checks its ID and endpoints against the corresponding pair of historical nodes. It checks the root, the current position and the shape of the history lists as well.

Suppose a later target changes the submitted edge’s ID from 101 to 111. The current node 20 still exists, and the next edge 201 is valid, but the saved history contains 101. A policy proposing only a revision change cannot make that old route valid in the target.

Attempt Result
choose() with the reviewed title-only 1.1.0 target Returns the updated revision and the normal new transition.
getPayload() or resume() proposing that state update Rejected: revision_change_requires_confirmed_transition.
choose() with the target that removed historical edge 101 Rejected: revision_history_incompatible.
Policy rewrites context, position, history or timestamps while changing versions Rejected: revision_resolution_changed_state.

These rejection reasons accompany EK-GRAPH-060. Keep the original stored session and offer a deliberate recovery path, such as continuing with its retained graph. A change that requires remapping nodes, rewriting context or rebuilding history needs a separate migration process designed by your application. This policy contract does not perform it. Graph migrations and rollback transforms graph definitions; it does not migrate active session records.

Before any of these session decisions, a loader checks the declared versions on a graph. That check answers whether the definition satisfies the configured format compatibility rules. It does not decide whether a particular existing session may change revision.

The core loaders use a declared engineVersion and a CompatibilityMode. With their default configuration, these are 1.0.0 and LOOSE. The distinction between modes is:

Declaration or matrix result LOOSE STRICT
Missing graph, DSL or schema version May be loaded without that declaration. Rejected.
Model compatibility marked warning Loaded with a validation warning. Rejected.
Model compatibility marked incompatible Rejected. Rejected.
Numeric graph major differs from configured engine major Rejected. Rejected.

The embedded engine_model matrix has rows for engine majors 1, 2 and 3; it has no row for 0. Configuring engineVersion: '0.2.0' therefore does not establish support for model 1.0: that declared combination is rejected. The loader’s compatibility version and the installed package version must not be conflated.

The definitions in this guide use graph versions 1.0.0 and 1.1.0, DSL/schema 1.0, and the loader’s default compatibility configuration. They demonstrate that configured contract, without claiming that the installed package is release 1.0.0. Consult Version compatibility matrix for the full declarations and their scope.

Finally, two absent legacy session/graph version values can match without inventing a version. If only one side declares a graph or DSL revision, the runtime sees a mismatch. A permissive loading mode does not remove that session check.