What respondents encounter
The enrollment check is a convenience for arrivals after the study fills. It is not the reservation. Several respondents can all observe an open study at once; the atomicscreen command checks the latest state before admitting each one.
The first committed eligible submissions fill each quota. A survey launch or an
earlier read does not establish priority.
If A fills first, new A respondents stop while B respondents can still qualify.
When both quotas fill, new arrivals stop before the screener. Respondents already
admitted retain access to the main survey, including the respondent who took the
last place. A race between the initial check and the screener is harmless: the
atomic admission can still refuse the now-full quota.
Define and run the survey
From the repository root, with the development package installed:states map identifies the durable study. Every respondent uses
the same study scope; creating one scope per respondent would incorrectly
create a separate quota for each person. A fresh map starts a fresh study.
Preserve the serialized survey/map to resume the existing study. Changing limits
on an existing database is a definition mismatch, not a quota-reset mechanism.
How the Machine reserves places
The state stores oneadmissions map, from stable respondent ID to admitted type,
and an enrollment_closed flag for explicit early closure. The map’s value type
is StateType.choice(["A", "B"]). Screened-out respondents do not add entries to this
map. Counts are derived, so there is no separately incremented counter to drift:
build_machine(). Its screen command
requires text inputs respondent_id and group and applies these guards in order:
- Reject a blank ID with
missing_respondent_id. - Reject changing an admitted ID’s type with
respondent_type_changed. - For a new ID, reject manual closure with
enrollment_closed. - For a new ID, reject types outside A/B with
ineligible_type. - For a new ID, reject both quotas being full with
quotas_full. - For a new ID, reject its particular quota being full with
type_quota_full. - Insert the ID/type using
put(..., once=True).
complete_when=full describes quota completion. It does not itself cancel
interviews. Explicit close sets enrollment_closed=True to end recruitment early
without revoking admissions. Neither closure nor completion releases places.
Connect admission to the screener answer
The adapter creates this respondent-facing question:current_value("respondent_id", ""). It does not expose the admission
roster. After the write, an explicit read refreshes that view. A QuestionCompute
turns it into a survey answer that an ordinary stop rule can use:
build_survey() also puts an enrollment read and computed stop gate before the
screener, preventing new interviews from asking it after closure.
The gate checks the persisted personal admission, not whether the latest global
count is below 10 and not the advisory write acknowledgement’s accepted flag.
The last admitted respondent must pass even though their admission filled the
quota. Matching the stored type to the answer also prevents a returning admitted
respondent from continuing after changing their type.
The adapter uses current reads. Do not run its admission gate under a schedule
that freezes state reads to a round-start snapshot: that snapshot may predate the
respondent’s own admission. The write would still respect capacity, but the gate
could incorrectly screen out someone who just reserved a place.
These are computed routing answers, not eligibility questions for the respondent
to answer. This example verifies the local Runner. Deploying the same mechanism
in a hosted human survey requires server support for the serialized state steps,
computed gates, durable shared study identity, and trusted respondent IDs; a
Mintlify example does not establish that deployment support.
Tests and deliberate limits
test_survey_quota.py checks randomized arrivals against an independent reference, zero/full quotas, manual closure, same-ID retries, changed types, separate studies, and the privacy of the projected view. Concurrent tests give all 32 contenders an open snapshot before their writes and still admit exactly 20, using independent SQLite backend objects. Reopened databases replay both admitted and refused decisions. Survey integration tests transport the whole survey through JSON, verify that screened-out respondents never answer the main question, and verify that later arrivals stop before the screener. A fresh-process test executes the Machine without importing its authoring helper. The quota counts admissions, not completions. Abandoned surveys keep their place; tests preserve that behavior across restart. Reclaiming reservations would need an explicit expiry/cancellation policy and reliable host events. Stable IDs must come from the host, not respondent-entered names. The example prevents counting an ID twice but does not prevent that admitted person from submitting multiple completed responses. Self-reported types are not externally verified. The admitted map has at most 20 entries at the default quotas. Event history still grows with attempts and reads, and each event has the backend’s existing storage and access requirements. The early enrollment check is advisory; correctness comes from transactional admission and an immutable admission record.What this application reveals about composition
The current definition is 8,980 JSON bytes and contains 133 expression occurrences, with 37 distinct expression trees. The samecount_by tree appears 11 times.
Assigning it to a Python variable makes authoring shorter but does not introduce a
shared serialized binding. let shares work within one expression body, not
across command assertions, views, and completion conditions.
For the deterministic Machine demo, the first successful admission uses 2,398
host budget steps and the twentieth uses 3,883. These numbers include definition
preflight and value checks; they are not milliseconds or a portable gas schedule.
Counts are recomputed from an increasingly large admitted map. The approach is
small and easy to audit for 20 places; large quota systems need profiling and a
choice between shared expressions and explicitly maintained aggregates.
Reproduce measurements for all corpus examples:
The occurrence counts include type expressions and nested references. Measurements
cover each example’s checked-in demo, not its worst-case input size.
A second gap is the survey adapter: state reads and
QuestionCompute gates are
needed to bridge a Machine decision to ordinary survey branching. A direct typed
branch on a state view could reduce that glue. Neither reusable serialized
functions nor a bounded runtime trace is introduced here; those remain in
the composition and debugging checklist.