Skip to content

State export and historical evidence

A state export lets your application inspect where a session is, which path it has taken and which context values it currently holds. It is useful for a support screen, a diagnostic download or a comparison with another saved state. To explain how those values changed, your application needs evidence recorded while the session advanced.

This guide uses StateExporter for the snapshot and DebugReplayExporter for a recorded trace. They accept different inputs and answer different questions. For storing a session so the engine can continue it, use the persistence representation described in State serialization and integrity.

StateExporter receives a loaded Graph, a SessionState and a language for the node metadata. Supply the definition used by that session, including its revision: the exporter combines session data with information from that graph. Your application decides which session to export and where the result will be stored or displayed.

Consider a small expense request. The application collects an amount at node 10, moves to a review screen at node 20 through edge 101, and submits through edge 102 to terminal node 30. A context_value condition allows submission when amount <= 150. The graph decides whether this transition is allowed; your application handles any subsequent processing of the request.

The first amount entered is 180. The session has reached the review screen but has not submitted. With that state in $state and its loaded definition in $graph, export it as follows:

use ExecKernel\Application\StateExport\StateExporter;
$exporter = new StateExporter(
engineVersion: '0.2.0',
sessionId: 'expense-example',
);
$snapshot = $exporter->export($graph, $state, 'en');
$json = $exporter->exportAsJson($graph, $state, 'en');

export() returns an array; exportAsJson() returns its JSON representation. The optional sessionId associates the download with your application’s session key. engineVersion is a label supplied by your application for the runtime used; the exporter does not discover that version itself.

The relevant part of this snapshot is:

{
"current": {
"node_id": 20,
"is_terminal": false,
"available_edges": [102]
},
"history": {
"visited_nodes": [10, 20],
"used_edges": [101]
},
"context": {
"values": {
"amount": 180
}
}
}

current describes the present node, history preserves the ordered node and edge IDs, and context.values contains the current context. The complete export also carries the graph identity, format version, optional session key, applicable version labels and localized current-node metadata.

In a state export, available_edges lists the graph’s outgoing edges from the current node. It does not evaluate their conditions. Edge 102 therefore appears even though 180 <= 150 is false. In the example’s actual runtime view, that edge is blocked. Use the engine’s payload when your application needs to present current choices; Condition evaluation and edge availability explains that view and the check performed when choosing.

The person now corrects the amount to 120 and submits. The final state export contains node 30, path [10, 20, 30], edges [101, 102] and the current value amount: 120.

That final context does not reveal the earlier value 180. The visited path alone also does not tell you when each transition occurred or which rule outcomes were observed. Those facts must have been captured separately.

Question from your application State export Recorded execution trace
Where is the session now? Current node, terminal flag and current context. The recorded final boundary, if present and consistent with the steps.
Which nodes and edges were traversed? Ordered history IDs. The same path, with individual step records.
What did amount contain before submission? Only the value at the exported moment. Initial context plus the changes recorded at each step.
When was edge 102 chosen? Not recoverable from the path alone. The timestamp recorded for that step.
Which conditions were evaluated and what did they return? No per-step rule results. The outcomes actually retained in evaluated_conditions.

StateExporter can include a timeline, timeSnapshot or contextPolicy supplied to its constructor. These are additional data that your application already has; passing them does not reconstruct missing history. For example, a timeline must come from recorded events, and a supplied clock must be identified according to what it represents.

Capture the evidence at the operation boundary

Section titled “Capture the evidence at the operation boundary”

A replay trace combines an initial state with an ordered list of transitions. Each transition records its starting node, selected edge, resulting node, input context changes, timestamp, observed availability and evaluated conditions. Your application assembles and retains that record while it still has the inputs and results of the operation.

The downloadable example uses a small in-memory capture. Before each navigation it prepares the candidate context and observes edge availability at the explicit timestamp. After navigation succeeds, it reads Navigator::lastConditionEvaluationTrace() and adds the actual input changes and resulting node. In an application with persistence, coordinate this record with confirmation of the session write so the trace describes the progress your application accepted. DebugReplayExporter does not perform that capture or provide a durable history service.

Here is an extract of the captured second transition:

{
"step_number": 1,
"node_before": 20,
"node_after": 30,
"selected_edge": 102,
"available_edges": [102],
"context_diff": {
"amount": 120
},
"timestamp": 1700000010,
"evaluated_conditions": [
{
"edge_id": 102,
"rule": "context_value",
"result": true
}
]
}

Step numbers start at zero. context_diff records the update supplied for this choice: amount becomes 120 before edge 102 is evaluated. In this trace, available_edges is an observation captured for that candidate context and time, unlike the outgoing-edge list in StateExporter. The rule outcome says that the actual condition evaluation succeeded. The complete trace also retains its input parameters and observed value.

The initial timestamp is 1700000000; the two transitions occurred five and ten seconds later in this example’s explicit clock. These times came from the operations used to produce the trace. They were not inferred from the final session.

Validate a trace and select an inspection window

Section titled “Validate a trace and select an inspection window”

Use DebugReplayExporter when you already have the recorded trace as an array:

use ExecKernel\Replay\DebugReplayExporter;
$recorded = json_decode(
file_get_contents(__DIR__ . '/evidence-trace.json'),
true,
512,
JSON_THROW_ON_ERROR,
);
$debug = new DebugReplayExporter();
$full = $debug->export($recorded);
$window = $debug->export($recorded, step: 1);

The exporter validates the complete record before returning either result. It checks ordered steps, continuous node progression, required evidence and consistency with the recorded final boundary. Absolute timestamps are normalized to epoch seconds. It does not run the conditions again or establish that a supplied record is authentic.

$full['export_scope'] is full_trace. The selected view has this envelope:

{
"export_scope": "inspection_window",
"window": {
"start_step": 1,
"end_step": 1,
"source_step_count": 2
}
}

The window contains only the requested step but keeps the original trace’s initial and final information. It is useful for showing a focused part of a diagnostic record. It is not a standalone executable trace: the omitted first step established the context and history with which the second step began. To replay a segment, give the complete trace to ReplayScenario::fromTrace() and select the range there, as shown in Deterministic, comparative and partial replay.

Keep the complete evidence available for that operation. If your application needs to share a smaller or redacted diagnostic view, retain the original separately and identify the view accordingly; removed context values or steps may make historical reconstruction impossible.

The expense graph and evidence-example.php produce the snapshots, the full trace and the inspection window described above. Place both files in the root of an existing PHP project that uses ExecKernel, next to its vendor/ directory. The script loads vendor/autoload.php from that root.

php evidence-example.php en prints the four artifacts and the runtime availability before confirmation. You can also inspect the generated final state export and full execution trace directly. Compare their treatment of amount: the final export holds 120, while the trace preserves the sequence 0, 180, 120 and the times at which the two updates were used.