Session stores and persistence modes
To continue a session in another request, your application needs to keep the SessionState returned by the engine and retrieve it when the person comes back. This state contains the current position, context, history and a reference to the graph used. When you integrate the core directly, your application chooses where to save it: calling start() or choose() produces a result, while persistence is a separate operation.
A session store provides operations to save, load and delete that state. Your choice depends mainly on how long you need to recover it and whether you use the core inside your PHP process or the standalone API.
Choose the lifetime you need
Section titled “Choose the lifetime you need”| Integration and store | What it keeps | How long you can recover it | Useful when |
|---|---|---|---|
Core: InMemorySessionStore |
A representation of SessionState inside the store instance. |
While that instance remains available. A new instance starts empty. | Execution finishes within one process, or you are testing an integration. |
Core: FilesystemSessionStore |
Serialized state in a directory. | From another instance or process accessing the same directory, while the files remain. | A PHP application needs recoverable sessions and its processes share file storage. |
API: NullSessionStore |
No sessions. | load() always returns null. |
You only need the initial response and will not continue that session through the API. |
API: InMemorySessionStore |
One SessionSnapshot per session, in memory. |
While that instance remains alive and the configured TTL has not elapsed. | An integration stays within one instance, or you are testing the API. |
API: PdoSessionStore |
The snapshot in SQLite, with its write generation. | From later connections to the same database, subject to TTL and record deletion. | API sessions must survive across requests or processes. |
The core memory store belongs to ExecKernel\Infrastructure\SessionStore; API stores belong to ExecKernelApi\Infrastructure\Session. Similar class names do not mean that they accept the same objects.
The environment also determines lifetime. A file in a directory that disappears when a container is recreated disappears with it. To retain sessions, your application must use a persistent location accessible to its processes and outside the web server’s public directory. Sharing that location makes data recoverable; coordinating writers is covered in Concurrency and session revisions.
Save the state you want to resume
Section titled “Save the state you want to resume”We will reuse the learning-choice graph introduced in Anatomy of a graph. At node 10, a person can select a guide through edge 101 or practice through edge 102; practice requires practiceEnabled: true. Here, the goal is to preserve that choice screen for a later request.
The following fragments assume that $graph contains this graph and $engine is an engine built with ExecKernelFactory. Choose a store when setting up your application:
use ExecKernel\SessionStore\FilesystemSessionStore;
$store = new FilesystemSessionStore(__DIR__ . '/var/sessions');$sessionId = 'learning-session';The directory is the location you must keep across requests. The identifier is the key used to retrieve this session; in a real integration, your application generates unique keys and checks who may access each session.
Now create the initial state and confirm its write before publishing the associated events:
use ExecKernel\Domain\Session\Context;
$started = $engine->start( $graph, 'en', new Context(['practiceEnabled' => true]), sessionId: $sessionId, deferEvents: true,);
try { $saved = $store->save($sessionId, $started->state); if (!$saved->persisted) { throw new RuntimeException('Session storage was not confirmed.'); }} catch (Throwable $error) { $started->eventBatch?->discard(); throw $error;}
$started->eventBatch?->publish();$payload = $started->payload;save() returns a SessionPersistenceResult. Its persisted field tells you whether that store confirmed the write. Once confirmed, your application can present the payload as the state it has retained. The deferred batch keeps result events from being published before confirmation; Runtime events and listeners explains that mechanism.
Confirmation has the scope of the chosen store. In memory, it confirms that the instance keeps the state. With files, it confirms writing and replacing the destination file. On its own, it establishes neither a backup policy, a guarantee against every device failure nor protection against two requests overwriting each other’s progress.
Recover and present the session again
Section titled “Recover and present the session again”In a later request, create the store with the same directory and load the identifier:
$store = new FilesystemSessionStore(__DIR__ . '/var/sessions');$state = $store->load($sessionId);
if ($state === null) { throw new RuntimeException('The session is unavailable.');}
$resumed = $engine->resume( $graph, $state, 'en', sessionId: $sessionId,);
$payload = $resumed->payload;load() retrieves the state. resume() checks whether it can be used with the supplied graph and produces the payload to display again. Your application must also retrieve the graph revision that belongs to the session and choose the presentation language: the state does not contain the full graph definition or keep the language by itself.
In this example, node 10 reappears with its context and both options. Reading does not add visits to history. If the store returns null, your application can offer to start a new session; make that an explicit decision, because the previous session is unavailable in this store. If the state exists but has expired for the engine, recovery has a different outcome, explained in Expiration, history and retention.
To use memory, the equivalent configuration is:
use ExecKernel\Infrastructure\SessionStore\InMemorySessionStore;
$store = new InMemorySessionStore();$store->save($sessionId, $started->state);
$sameInstance = $store->load($sessionId); // SessionState$newInstance = (new InMemorySessionStore())->load($sessionId); // nullThis difference explains why creating a new memory store in every request cannot continue the previous request’s session.
Configure the API’s store
Section titled “Configure the API’s store”The standalone API has its own contract. It stores a SessionSnapshot, which groups the identifier, SessionState, language and an optional write generation. The API service uses these pieces to coordinate its operations. The core contract receives the identifier and state as separate arguments.
| Contract | Basic save | Load |
|---|---|---|
ExecKernel\SessionStore\SessionStoreInterface |
save(string $sessionId, SessionState $state): SessionPersistenceResult |
SessionState or null. |
ExecKernelApi\Application\Ports\SessionStoreInterface |
save(SessionSnapshot $snapshot): void |
SessionSnapshot or null. |
In the API, a void return from the basic port does not itself confirm a commit. The service distinguishes stateless mode, checks persistence outcomes and uses conditional operations when the adapter supports them. The API memory and SQLite stores implement that conditional contract.
For SQLite, $pdo must be a connection to the API database with its session schema already prepared:
use ExecKernelApi\Infrastructure\Session\PdoSessionStore;
$store = new PdoSessionStore($pdo, ttlSeconds: 3600);Inject that store when constructing the API. Despite its name, PdoSessionStore supports SQLite; it rejects other PDO drivers. Its configuration and operations do not transfer to the core file store.
With NullSessionStore, the API can produce the start response, but a later operation by identifier will not find that session. Stateless mode is useful when the initial response is sufficient; an interaction spanning requests needs to retain the snapshot.
Add capabilities without accidentally changing lifetime
Section titled “Add capabilities without accidentally changing lifetime”The core lets you wrap a store. SignedSessionStore adds an integrity seal check; LockingSessionStore can coordinate conditional writes with a lock supplied by your application; TimeoutAwareSessionStore controls the time budget for operations. These capabilities are added to the inner storage. Wrapping a memory store does not make it survive the process.
To protect the contents, continue with State serialization and integrity. To accept progress only if nobody has changed the session you read, use the procedure in Concurrency and session revisions.
Complete example
Section titled “Complete example”stores-session.php compares memory and files using the same state, then presents the recovered session. It uses the learning-path.json graph. Place both files in your project root, alongside vendor/; the program loads vendor/autoload.php from that root. You can pass the session directory as the first argument.
The result shows node 10 when loading from the same memory instance and a new file-store instance, null from a new memory instance, equality of the recovered state, and options 101 and 102 when resuming.