Run the example
Lower price, Better quality, and the routing option
Other. The Machine does not know the other three reasons or the total number
of possible reasons. Only the simulated population knows the ground truth.
With the default seed reason-discovery-2026, the actual survey run produces:
It stops after 21 interviewed agents, leaving the remaining 79 unasked. All
five reasons happened to be discovered in this run. The JSON report includes the
seed, discovered labels, final streak, stop reason, undiscovered ground truth,
and the sequence of valid observations. Selecting Other does not create an
observation until its text is submitted.
The demo uses scripted local agents and a test model, with no model API calls.
Scripts simulate choosing their assigned reason or supplying it through Other;
the Machine handles catalog growth and stopping. This verifies the mechanism and
survey plumbing, not an LLM’s ability to paraphrase or categorize reasons.
State and transition rules
Construct the Machine with two seed labels and a patience threshold:
Reason identity is defined by trimming surrounding whitespace and applying
case folding. The first supplied spelling is kept for display. Thus
" Convenience " and "CONVENIENCE" identify the same reason. Internal spacing,
punctuation, synonyms, and paraphrases are not merged. Other is reserved for
routing and cannot be a seed or a supplied reason. Seed labels must be nonempty
and distinct under the same normalization; patience must be a positive integer.
Two commands express the survey’s two paths:
select(respondent_id=..., reason=...)records a reason already in the catalog. Selecting the literalOtheris a no-op that routes to the free-text question.suggest(respondent_id=..., reason=...)records the free-text answer. If its normalized text is new, it appends the label and resets the streak to zero. If it is already known, it increments the streak.
respondent_reason_changed. Empty IDs, empty reasons, the reserved
Other label, and selecting an unknown option are rejected without changing the
catalog or streak. Such invalid submissions do not count as quiet observations.
Once quiet_streak >= patience, new observations are rejected with
discovery_closed. An explicit close also blocks new observations, without
claiming saturation. Retries of an earlier valid observation remain harmless.
All changes to labels, streak, and respondent records commit atomically.
The key computation uses ordinary primitives:
reason,
sets the streak to choose(new, 0, field("quiet_streak") + 1), and records the
respondent’s key. All these expressions see the same pre-command snapshot, so
appending first does not accidentally make the subsequent streak calculation
believe the reason was already known.
Wire the adaptive survey
The complete adapter is available as:respondent_id, a study_id, and an integer
turn. Its simulation-only true_reason trait supplies scripted answers; the
Machine never reads that trait. Studies have separate scopes and separate catalogs.
At the start of each interview, an explicit state read supplies the current
catalog. A computed gate ends an interview if discovery is closed. The reason
question gets its actual options from that read:
options is reasons followed by Other. It excludes the
respondent ledger. The survey writes a select command after the multiple-choice
answer, asks other_reason only when the answer was Other, and writes suggest
after that text answer. Its skip rule is:
build_survey(); do not
add it a second time to the returned survey. A skipped free-text question does
not execute its attached write. An Other routing no-op does not increment the
streak or mark the respondent seen before their suggestion arrives.
The schedule runs one whole interview at a time per study:
build_survey() when running its survey. In the
local Runner:
len(results). The Machine’s own closed guard also
prevents late direct submissions from changing a saturated catalog.
Replay, completion, and interpretation
Transportingsurvey.to_dict() and schedule.to_dict() preserves the dynamic
options, state identity, command bindings, stop rule, and sequential order. Saving
and restoring both resumes the same catalog and streak. A fresh state map starts
a new study. SQLite operation retries replay the original decision, while the
respondent ledger also prevents recounting when a retry has a new operation ID.
The simulated pool is finite. If it runs out before the streak reaches 10, the
report says population_exhausted, not saturation. The random seed is explicit,
so population assignments and the sequential run can be reproduced.
Ten quiet agents is a stopping rule, not proof that every reason is known.
A test puts ten agents with a seeded reason before an agent with an unseen reason:
the survey correctly stops after ten and never discovers the later reason. In a
real study the unknown ground truth cannot be included in the report; it is an
evaluation aid available only in this simulation.
The tests
check the ninth/tenth-observation boundary, resets on discoveries, duplicate and
invalid suggestions, same-ID retries, concurrent submissions, SQLite restart,
and independent reference simulations. End-to-end tests inspect each agent’s
rendered option list, verify that only Other triggers free text, transport the
survey and schedule through JSON, resume across jobs, and confirm that later
agents are not asked after saturation. A fresh process replays the exported
Machine without importing its Python authoring helper.
Design findings
This application needs no new interpreter primitive. It combines a growing choice list, conditional questions, atomic updates, and a shared completion condition. Its Machine exports under the corpus’s empty algorithm registry and generic-reducer policy. The current primitive definition is 12,924 JSON bytes, with 184 expression occurrences and 41 distinct trees. Its checked-in Machine demo uses at most 4,078 host budget steps per transition. Reproduce those measurements withpython -m edsl.examples.shared_state.machine_primitives.profile_corpus. The two command bodies
share a Python authoring helper, but that helper expands into repeated serialized
expressions. Reusable serialized computation remains a useful design target.
The larger application-specific gap is semantic deduplication. An LLM may
write “it cost less” when “Lower price” is already listed. This example treats
those as different reasons. Merging them requires an explicit classification or
review policy, with an auditable decision about whether the streak should reset;
string normalization does not solve that problem.
The local tests do not establish support in a deployed human-survey service.
Such a deployment must preserve dynamic option snapshots, trusted IDs, shared
state, conditional questions, and the intended interview ordering. The running
design checklist tracks the
remaining composition and deployment boundaries.