Skip to main content
This example adds a reversible reservation lifecycle to the survey quota pattern. A slot can be available, held, confirmed, and released. It uses ordinary Machine commands and Survey stop rules; it makes no calendar-service calls.

Build and run the complete example

Everything needed for this example is defined on this page. Use an EDSL build that includes the Machine and Runner APIs documented in this manual. Execute the following four Python blocks in order in one session, or concatenate them into one script. They require EDSL and the Python standard library; no repository example modules or external data files are needed. The scripted run makes no model API calls. The five-person local demo releases one hold, confirms three appointments, and screens out one respondent when no slot remains. The first respondent releases 09:00; the next respondent sees and books it. No model API calls are made.

1. Define the Machine

The Machine below derives availability from held and confirmed reservations. Its commands check capacity and reservation identity before writing any state.

2. Construct the Survey

The survey reads availability, asks for a slot, attempts a hold, and reads again before offering confirmation. A stale selection cannot bypass the hold gate.

3. Define the scripted respondents

These five local agents always choose their first available slot. The first releases it; the others request confirmation. The final agent reaches a full calendar.

4. Run and inspect the answers

Model("test") supplies the Jobs model dimension. The agents above answer locally, and QuestionCompute evaluates the routing gates without a model call.

Follow five respondents through the same calendar

The follow-up Python blocks use the definitions and Results created above. Each build_survey() call creates fresh state unless you explicitly reuse a saved map. The following table is output from the scripted demo, not a hypothetical calendar. Every respondent picks their first displayed slot. R0 temporarily removes 09:00 from availability, then releases it. R1 can therefore choose 09:00. Once R1 confirms, it disappears from R2’s list. R4 is screened out before the slot question: an empty list is never presented as a multiple-choice question. The reservation ledger still includes R0’s released reservation. Availability comes from active holds and confirmations, not from deleting history. This is why a late action on R0’s old reservation can be recognized safely.

Check the choices against saved Results

This joins the answer’s displayed options to the exact state read used for that question. It does not reconstruct R2’s choices from the final, fully booked state. Save the evidence with the Results object:
Use a fresh output path if that package already exists.

State and commands

One state scope covers a study. The bookings map is keyed by a host-issued reservation_id; each entry stores respondent_id, slot, and status. Capacity is derived from all held and confirmed entries, so there is no separate counter to drift. Each slot has capacity one, and each respondent may have at most one active reservation. A released reservation is terminal. A new booking needs a new reservation ID. Replaying its original hold cannot recreate capacity consumption, and a late confirmation is rejected. A delayed release addressed to an old ID cannot release a newer reservation, even for the same respondent and slot. A failed command leaves the state unchanged. Examples include slot_unavailable, already_booked, reservation_changed, and reservation_not_owned. Unknown slot/decision values fail input validation. SQLite also retains completed operation outcomes: retrying a rejected operation replays its rejection. Trying again after availability changes requires a new operation identity, not reuse of a rejected delivery key.

Work through a stale-read race

Two people can both see 09:00 before either has submitted a choice. The following reproducible interleaving uses the transported Machine directly, separating the read from the atomic write. Real concurrent SQLite writers are covered by the application tests.
The choices shown to B were valid when read, but did not reserve capacity. The write decides admission. In the Survey, a rejected hold leads to the status gate and ends that interview; a UI that offers another attempt would need to add that interaction explicitly.

Survey routing and resumption

The survey reads available options, asks for a slot, attempts the atomic hold, and reads again. Only a successful hold proceeds to confirmation. Thus a concurrent respondent can take a displayed slot without causing overbooking; the losing respondent stops at the hold-status gate. This first adapter does not loop back to select another slot. A respondent returning with the same held reservation sees only that slot and can finish confirmation. The interrupted-hold test verifies this across jobs. Returning with a confirmed or released reservation ends before another selection. The Machine also supports post-confirmation cancellation through Release, although this short survey only asks for the initial decision. The schedule is sequential for a reproducible demonstration. Atomic writes are also tested with concurrent SQLite writers. complete_when is deliberately false: temporary fullness must not prevent a later release from reopening a slot. Reuse the serialized state map to resume; construct a new map for an independent study. Respondent and reservation IDs must be stable and assigned by the host.

Tests and design findings

The application tests cover capacity/identity invariants over randomized command streams, competing holds, ownership checks, changed payloads, release/rebook fencing, explicit close, SQLite restart and delivery replay, isolated scopes, fresh-process JSON execution, actual option snapshots in Results, and interrupted confirmation. Holds do not expire automatically. A trusted host must explicitly release an abandoned hold; the Machine has no clock or abandonment detector. Production expiry would need a host scheduling/time contract and tests for expiry racing with confirmation. ID equality is a consistency check, not authentication. This example establishes a reservation pattern, not hosted calendar or human-UI integration. The other designs’ concurrent-assignment policies remain separate. The default definition is 14,824 JSON bytes; its short corpus replay peaks at 4,530 host budget steps per transition. Retained reservation history grows over time. These are development observations, not portable cost guarantees. Maintainers with a repository checkout can optionally reproduce the measurements using python -m edsl.examples.shared_state.machine_primitives.profile_corpus.