Run the example
The demo compares each agent’s actual survey answers against the Machine’s
ledger, so accidentally asking extra questions fails verification. With an
insufficient population, such as
run_demo(count=1), the report instead says
population_exhausted and reports the coverage achieved.
This is a correctness demonstration. The local Runner now reuses validated
survey templates; the performance probe below measures that improvement without
changing the survey’s navigation or the Machine’s execution checks.
Assignments and answer counts
coverage_
namespace. The target and per-agent limit must be positive integers. The sample
uses 1–5 integer ratings; another answer type requires changing both the survey
question and the Machine’s answer schema.
The fields are counts per question, assignments per respondent, an answers
ledger keyed by respondent and question, and an explicit closed flag.
assign(respondent_id=...) filters out questions already at target, sorts the
rest by (answer count, question ID), and saves up to per_agent IDs. Ties use
lexical question-ID order. A repeated assignment request retains the original
list, even if coverage has changed. The survey asks the selected subset in its
normal question order.
An assignment does not count as an answer and does not reserve capacity.
If an agent leaves a question unanswered, its count stays unchanged and a later
agent can receive it. The original assignment remains available for resumption;
already answered or subsequently filled questions are omitted from the
respondent’s pending view.
record_answer(respondent_id=..., question=..., answer=...) validates the rating
and checks that the question belongs to that respondent’s saved assignment.
It atomically records a new answer and increments that question’s count. Each
respondent/question pair counts once. Retrying the same answer is a no-op;
changing it is rejected with answer_changed. An unassigned question is rejected
with question_not_assigned; a new answer to a full question is rejected with
question_full. Invalid answers do not advance coverage.
Completion requires every count to reach its target. An explicit close blocks
new assignments and answers but does not claim full coverage. Harmless retries
of recorded answers remain allowed. Stable respondent IDs and trusted study
scope are host responsibilities.
Generate the survey routing
- Before the first question, request the respondent’s fixed assignment.
- Read the Machine view, including that respondent’s pending question IDs.
- Compute three internal scalar slots containing those IDs.
- Ask each real question only if its ID appears in a slot.
- After a validated answer, execute its attached
record_answercommand.
coverage_none, a value excluded from actual question
IDs. An empty string is unsuitable: computed-question rendering currently
validates the rendered text as a question and rejects empty text.
The rule for q01 is equivalent to:
build_survey(); do not add
them again. Membership compares exact IDs, so q1 cannot accidentally match
q10. If the first slot is coverage_none, a stop rule ends that interview.
A skipped question does not execute its attached answer write.
Ordering and resumption
Each agent hasstudy_id, a stable respondent_id, and a numeric turn. The
returned schedule runs whole interviews one at a time within each study:
grouped_round_robin("study_id", "turn", stop_when=coverage.is_complete())
ordering ensures the next agent’s assignment sees previous answers. Preserve
the survey, its state identity, and the schedule when resuming. JSON transport
preserves the generated routing and command bindings. SQLite retains assignments
and answers; the pending view prevents reasking previously recorded answers.
Concurrent interviews are outside this adapter’s allocation contract. The
Machine still prevents counts exceeding their targets, but simultaneous agents
can receive the same remaining capacity and one answer may be rejected. A
concurrent version needs explicit reservations and an abandonment/release policy.
This sample makes neither capacity reservations nor replacement assignments.
Tests and design findings
The tests cover independent reference streams with abandoned answers, fixed assignments, invalid answers, exact membership, retries, concurrent count bounds, SQLite restart, fresh-process Machine replay, and survey/schedule JSON transport. End-to-end tests check actual asked questions, suppressed writes for skipped questions, partial interview resumption, the final two-question assignment, and population exhaustion. Three limitations are especially useful for future designs:- Typed state-to-routing bindings. The current adapter expands a list into scalar computed slots and generated rules. Direct typed membership against a shared-state view would remove this plumbing and the nonempty-slot marker.
- Repeated definition work. The original Runner repeatedly reconstructed the survey and validated its embedded Machines. A bounded local template cache now removes that repetition; measurements and isolation rules follow.
- Concurrent allocation policy. Answer counting alone cannot prevent two active respondents receiving the same last place. Reservations require clear expiry, abandonment, and resume semantics.
python -m edsl.examples.shared_state.machine_primitives.profile_corpus. They describe that replay,
not the larger completed survey, and are not portable runtime-cost guarantees.
Local tests do not establish deployment support in a hosted human-survey
service. Such a host must preserve trusted IDs, shared state, computed routing,
validated-answer writes, and the required interview ordering. Remaining work is
tracked in the design checklist.
Runner definition reuse
The Runner keeps at most 16 decoded survey templates perJobService. Each
lookup identifies the complete stored payload, including its Machine definitions
and rules. A changed payload goes through normal deserialization and validation.
Failed definitions never enter the cache. Eviction or a new service instance
simply causes the definition to be reconstructed again.
Every caller receives a deep copy. Attaching a respondent’s answers, rendering
personal options, changing question objects, or editing returned Results cannot
change the cached template or another caller’s copy. Incoming dictionaries are
also detached from templates. State views, answers, SQLite state, capability
admission, and execution budgets are not cached by this optimization.
Use the reproducible local probe to compare cached execution with the previous
per-use decoding path:
