Skip to main content
This chapter continues the shared-state manual. The snippets below are machine fragments to adapt, with fields and constants provided by the surrounding machine. Import current_value from edsl.sharedstate for machine-context expressions. Survey scope selection uses the distinct current object from the same module.

Pattern cookbook

The same small set of fields and effects recurs across many simulations. This section collects common patterns without hiding them behind new classes.

Pattern: first-write-wins

Use this when each participant gets one binding submission, such as a vote, allocation, or sealed bid:
A retry with the same key is harmless. A conflicting later value is ignored. For a single shared value rather than a map entry, use set_once:

Pattern: latest-write-wins

Use ordinary put when participants may revise their current response:
The map always contains the latest value. If the revision history matters, keep both a current map and an append-only history in one command:
Both changes commit atomically.

Pattern: append-only event history

Use a sequence when every event should remain available:
Internal metadata can remain in state while the view projects only public fields.

Pattern: expose only recent history

The full log can persist while readers receive a bounded tail:
This bounds prompt size without discarding the audit history.

Pattern: an atomic multi-field transition

Use one command when fields must agree with one another:
Readers never see an amount without its proposer or a current offer without the corresponding history entry.

Pattern: a staged interaction

A later command can require an earlier stage:
This pattern supports ultimatum games, trust games, signaling, cheap talk, principal-agent interactions, and bilateral trade.

Pattern: terminal actions

Some actions end an interaction immediately:
The interview schedule can use this completion condition to stop further turns for that scope while other groups continue.

Pattern: sealed collection and reveal

Expose only submission counts until close:
Then compute results atomically at close:
Use this for voting, sealed auctions, beauty contests, and simultaneous-move games.

Pattern: viewer-specific private information

Use typed viewer context rather than string interpolation:
The validator checks whether a view is permitted to disclose each context path.

Pattern: private signal history

Store histories by participant and select only the viewer’s entry:
The guard is safe because choose evaluates only the selected branch.

Pattern: counters from checkbox selections

A checkbox answer may increment several configured counters atomically:
Unknown keys fail validation rather than creating accidental counters.

Pattern: keep only the latest submission per participant

An append-only history can still produce a latest-value view:
This is useful for forecasts, revised rankings, and deliberative estimates.

Pattern: dynamic payoff dictionaries

When participant names become result keys at runtime, use map_of:
This produces, for example:
No role-specific payoff helper is required.

Pattern: claim from a capacity-limited pool

The requirement checks current availability, and the effects reserve the item and record the claim together:
For iterative allocation across complete preference rankings, use a registered matching algorithm instead of constructing an unbounded loop in the DSL.

Pattern: several structures in one scope

Machines remain small and compose at the SharedState layer:
Do not combine unrelated fields into one large machine merely because they share a scope.

Pattern: the same structure across many groups

Define the machine once and vary only the scope key:
The selected space changes; the machine definition and survey remain the same.