Skip to main content
Suppose a survey has 20 questions, each needing 10 valid answers. Each agent answers at most 3 questions in a single interview. Before that interview starts, a Machine assigns the questions with the fewest recorded answers. Later agents see a different subset as coverage changes. Once all questions reach their targets, the study stops. The primitive Machine uses existing map/filter, sorting, assertions, and atomic effects. The survey adapter generates the skip rules and runs interviews sequentially. No new interpreter operation or registered algorithm is needed.

Run the example

The local simulation creates 100 scripted agents and uses a test model, making no model API calls. Its actual survey run produces: 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

Question IDs must be distinct identifiers outside the reserved 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

The adapter installs these steps:
  1. Before the first question, request the respondent’s fixed assignment.
  2. Read the Machine view, including that respondent’s pending question IDs.
  3. Compute three internal scalar slots containing those IDs.
  4. Ask each real question only if its ID appears in a slot.
  5. After a validated answer, execute its attached record_answer command.
The default survey therefore contains 20 substantive questions, three computed routing slots, 20 generated skip rules, and one stop rule. The computed slots do not call a model or request respondent input. They translate the Machine’s list view into ordinary answer references supported by the survey rule engine. For example, the first computed slot contains:
An unused slot contains 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:
These fragments explain rules already generated by 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 has study_id, a stable respondent_id, and a numeric turn. The returned schedule runs whole interviews one at a time within each study:
Its 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.
The Machine definition is 12,831 JSON bytes, with 159 expression occurrences and 62 distinct trees. Its short corpus replay uses at most 12,514 host budget steps per transition. Reproduce these measurements with 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 per JobService. 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:
In the measured five-agent run, elapsed time fell from 34.8 seconds to 4.8 seconds, with identical answer counts and stop reason. Survey decoding fell from 186 calls to one, and Machine validation from 4,094 calls to 24. The full 100-agent coverage regression fell from roughly 635 seconds to 112 seconds, retaining the 67-answering-agent, 200-answer result. Timings are local observations, not cross-host guarantees; the probe deliberately uses no model calls. Initial definition validation and actual state execution still have costs. This change preserves ordinary skip/stop rules and the existing survey API. Direct typed state-to-routing bindings and state-machine navigation remain separate, deferred design work.