Extract A/B verbal-cluster splitting into a testable helper#1260
Merged
Conversation
Pull the verbal-cluster building and condition-balancing logic out of make_pairs into a pure split_ab_conditions() function and add unit tests. The helper takes the answerers and recorded verbal groups and returns the (in_person, chat) partition. Randomness is injectable via an rng parameter so tests can pass a seeded random.Random for deterministic results. No behavior change in make_pairs. Tests cover: singletons go to chat, non-voting partners stay in their partner's condition, verbal groups are never split, overlapping recorded groups stay disjoint (the grp -= clustered guard), both conditions get seeded when possible, balanced splitting, and determinism. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01ALwXHQ2E7VCS2DN2spq1Y3
Contributor
There was a problem hiding this comment.
Pull request overview
This PR extracts the A/B verbal-cluster construction and condition-balancing logic from the make_pairs request handler into a pure helper (split_ab_conditions) so it can be unit tested without DB/auth/request context.
Changes:
- Added
split_ab_conditions(answerers, in_person_groups, rng=...)to encapsulate clustering + balancing + “seed each condition when possible” behavior. - Updated
make_pairsto callsplit_ab_conditionsinstead of inlining the logic. - Added a new unit test module covering key clustering, balancing, and determinism cases.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.
| File | Description |
|---|---|
bases/rsptx/assignment_server_api/routers/peer.py |
Introduces split_ab_conditions and replaces inlined A/B clustering/balancing logic in make_pairs with a helper call. |
test/bases/rsptx/assignment_server_api/test_make_pairs.py |
Adds unit tests for the extracted helper, focusing on invariants, edge cases, and deterministic behavior with a seeded RNG. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Comment on lines
+630
to
+634
| def split_ab_conditions( | ||
| answerers: list[str], | ||
| in_person_groups: list[set[str]], | ||
| rng: random.Random = random, | ||
| ) -> tuple[list[str], list[str]]: |
Member
Author
Contributor
|
Looks good to me. I can take a look at at fixing #1261. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Follow-up to the
ab-fixesmerge. The A/B verbal-cluster building andcondition-balancing logic was inline inside the
make_pairsrequest handler,which made it impossible to unit test (it needs a DB, auth, and a live request).
This PR extracts that logic into a pure
split_ab_conditions()function andadds unit tests.
No behavior change in
make_pairs— the inline block is replaced by a callto the new helper.
What changed
split_ab_conditions(answerers, in_person_groups, rng=random)returns the(peeps_in_person, peeps_in_chat)partition. It contains the cluster-building(
find_set_containing_string+grp -= clustered), greedy ~50/50 balancing,and the "seed each condition when possible" adjustment.
rngparameter so tests can pass a seededrandom.Randomfor deterministic results.make_pairsnow calls the helper instead of inlining ~55 lines.Tests
New
test/bases/rsptx/assignment_server_api/test_make_pairs.py(9 tests,pure/sync, no DB) covering:
grp -= clusteredguard)All 9 pass;
black/flake8clean on the changed files; assignment servicerebuild (
uv run build -s assignment dev) passes including the full suite.🤖 Generated with Claude Code