Skip to main content
This chapter continues the shared-state manual. The poll examples build on the activity_poll definition in Machines, commands, and views. Alternative fragments are labeled separately.

Creating one SharedState space

Place the machine in a state definition:
One space now contains a machine named poll. The rest of this walkthrough uses this definition. The following alternative illustrates multiple machines; it requires a message_log defined as in Collections and lifecycle:
The poll and message log have separate fields and commands but share one scope lifecycle. No file path appears here: this defines state, not storage.

Creating many scopes with SharedStateMap

Most simulations need one independent space per group:
Conceptually, it is a dictionary:
Spaces need not all be created in advance. Selecting a new key can create its initial state:
In a survey, selection usually comes from agent traits:
One SharedStateMap contains many scopes. One SharedState contains the named machines within a scope.

Connecting the poll to a survey

Define the question:
Jinja belongs inside the presentation. State commands use typed references. Put an explicit read before the question and the write after it:
Read the sequence literally:
  1. read this family’s poll;
  2. ask the question using that snapshot;
  3. submit the answer as a vote.
activity.answer means the validated answer to this question. It is not a placeholder string.

Why reads are explicit

A state value rendered when a survey launches can be stale before a participant reaches the question. family.poll.read() states exactly when freshness matters. Every read is logged at the Results level with a read ID, selected scope, machine name, snapshot ID, interview ID, and survey position. This supports auditing without promising that a snapshot remains newest after it is read.

Agent grouping and scheduling

Scope and schedule fields are ordinary agent traits:
The serializable schedule consumes those trait names:
Different families may run concurrently. Within a family, the schedule controls turn order. The state definition does not schedule interviews.

Storage and execution

Attach storage when executing, not when defining data:
The test model supplies a deterministic hike answer without an API key or model call. Replace it with a configured model for real responses. The survey’s explicit state steps carry the serializable definition and state ID; no database handle belongs in the job definition. Local execution manages a SQLite state service. Remote execution creates or attaches to a remote store and sends a durable reference with the job:
Results contains state provenance for each binding:
Entry and exit values are snapshots; reads and writes appear in the ordered event history. Together they record which state the run inherited, what it did, and what state it left behind.

Which options did this respondent see?

For completed ordinary model questions and agent-direct answers, the Runner captures the question’s options before execution. Results uses this snapshot instead of resolving the template against later answers or shared state. The snapshot retains option order, matrix row order, question text and type, and option labels when present. It survives JSON and .ep save/reload. For example, with the pairwise survey:
source="prompt" identifies the resolved question used to construct a model prompt; source="agent_direct" identifies the question object passed to a direct-answering method. Direct methods can receive unrendered question text; the snapshot records that object faithfully. It does not certify what an external human-survey UI displayed when replaying previously collected answers. The references identify all shared-state reads available in that question’s context, including reads inherited from an earlier question. They are not an analysis of which fields the template used. No private state is copied into the question metadata. A model-cache hit retains the current question’s read references, even when another run produced the cached answer. The presentation marker is absent for older stored answers, skipped questions, computed/functional questions, failed tasks, and the special interview-question execution path. Those entries retain the existing reconstructed metadata, which must not be treated as evidence that a question was presented. Check for the marker when auditing historical choices.

Durable state and retries

When binding a state map directly to SQLiteStateBackend, the backend records the serialized definition fingerprint. Reopening the path with a behaviorally different definition is an error. Databases created before fingerprints existed require an explicit, audited adopt_legacy_definition=True; normal execution never silently adopts them. Write retries are content-bound. Reusing the same operation identity with the same inputs and runtime context returns the earlier outcome. Reusing it with different content is an error, preventing an idempotency retry from becoming a different scientific action.

Fresh state versus resuming state

A new SharedStateMap(group_state) generates a new state ID. Reusing a saved map preserves its ID and reconnects to its stored state. Use a new map for an independent experiment; preserve the serialized map and its definition when resuming one. Reusing a poll’s ID can retain earlier first-write-wins ballots. Scope keys such as family-1 isolate groups within that map; they do not reset its data between runs.