Repository navigation
feat(generic/routing/random): add random routing - #12
Conversation
FOCA 0.18.0, with the plugin engine and its test kit, is on PyPI. Remove the Git source of FOCA, require foca>=0.18.0,<0.19 in the example plugin and the meta distribution, and re-lock. CI tests FOCA 0.18.0 and dev; Dependabot updates FOCA again. The template's tests use FOCA's test kit (contract checks, harness), so new plugins inherit them. Docs updated. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Template tests build a new task per use instead of sharing a mutable dict. The Codecov upload follows a coverage flag of the matrix entry instead of a hard-coded FOCA version. Comments and docs say how contract checks run, that both CI entries replace the locked FOCA, and which sample inputs plugins should pass; a stale note on the Git source of FOCA is gone. Co-Authored-By: Claude Opus 5.5 <[email protected]>
The template's tests build their candidate backends per use, like their tasks, so that hooks that change their input in place can't make comparisons pass. Rewrap a comment in release.yml; say in the CI matrix comment which entry reports coverage. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Tests that check that the template's hooks keep their input pass values of their own (a task name, backend identifiers), so that a hook returning the builders' defaults can't pass them. The template's comment on namespace packages no longer names its own subgroup, which copies kept. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Add the plugin generic.routing.random, the successor of proTES's random task distribution: its route step returns all candidate backends shuffled with random.SystemRandom. A seed gives reproducible orders for tests, refused unless the admin setting allow_seed is on. Records the number of candidates and whether a seed was used; the order itself is in FOCA's route provenance. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Accept only integer seeds, name the Python version in the promise of reproducible orders, add a seeded example, fix the namespace comment and list the plugin in the READMEs. Tests use their own candidates and assert exact exception types. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Co-Authored-By: Claude Opus 5.5 <[email protected]>
Reviewer's GuideIntroduces a packaged random-routing plugin with secure unseeded behavior, explicitly authorized deterministic test seeding, comprehensive FOCA contract and integration coverage, and repository-wide adoption of the released FOCA 0.18 plugin API and compatibility matrix. Sequence diagram for random backend routingsequenceDiagram
participant Gateway
participant Random
participant SystemRandom
participant Backends
Gateway->>Random: on_route(candidates, ctx)
Random->>SystemRandom: shuffle(candidates)
SystemRandom-->>Random: shuffled candidates
Random->>Random: record(candidates, seeded)
Random-->>Gateway: all candidates in random order
Sequence diagram for authorized seeded routingsequenceDiagram
participant Pipeline
participant Random
participant RandomParams
participant SeededRandom
Pipeline->>RandomParams: validate seed
Pipeline->>Random: check_params(params)
alt seed set and allow_seed is false
Random-->>Pipeline: InvalidParams
else seed allowed or absent
Pipeline->>Random: on_route(candidates, ctx)
Random->>SeededRandom: shuffle(candidates)
SeededRandom-->>Random: reproducible order
Random-->>Pipeline: all candidates in shuffled order
end
File-Level Changes
Assessment against linked issues
Possibly linked issues
Tips and commandsInteracting with Sourcery
Customizing Your ExperienceAccess your dashboard to:
Getting Help
|
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## dev #12 +/- ##
=========================================
Coverage 100.00% 100.00%
=========================================
Files 1 2 +1
Lines 29 66 +37
Branches 3 6 +3
=========================================
+ Hits 29 66 +37
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
Co-Authored-By: Claude Opus 5.5 <[email protected]>
There was a problem hiding this comment.
Hey - I've found 2 issues
Prompt for AI Agents
Please address the comments from this code review:
## Individual Comments
### Comment 1
<location path="plugins/generic/routing/random/tests/test_random.py" line_range="245" />
<code_context>
+ for _ in range(20)
+ }
+ # 8! orders: 20 equal draws have a probability of about 1e-88
+ assert len(orders) > 1
+
+
</code_context>
<issue_to_address>
**Correct random draws can fail**
When all 20 unseeded draws happen to produce the same permutation, when 20 correct unseeded draws happen to produce the same permutation, `test_on_route_varies_without_seed` fails at line 245 even though the implementation is correct; the test relies on an unmocked random outcome.
Replace the probabilistic assertion with a deterministic check of the unseeded randomness path.
</issue_to_address>
### Comment 2
<location path="plugins/generic/routing/random/tests/test_random.py" line_range="209" />
<code_context>
+ candidates = tuple(BACKENDS)
+ ctx = make_context()
+ result = plugin.on_route(candidates, ctx)
+ assert sorted(result, key=lambda b: b.id) == BACKENDS
+ assert candidates == tuple(BACKENDS)
+ assert ctx.annotations == {"candidates": 8, "seeded": False}
</code_context>
<issue_to_address>
**Tuple results pass permutation check**
When the route hook returns the input tuple unchanged for a multi-candidate tuple input, when `on_route` returns the input tuple unchanged for this multi-candidate input, `test_on_route_returns_permutation` sorts it at line 209 and compares only its contents, so the test passes despite the hook not returning the promised new list.
Assert that the result is a list and, using a mutable input, that it is not the input object.
</issue_to_address>Sourcery assessment
Approval pending. 1 finding to address first.
Blocking findings: plugins/generic/routing/random/tests/test_random.py:209
Check that the hook returns a new list, and replace the probabilistic check of unseeded orders with one on a fixed entropy stream (review by Sourcery). Co-Authored-By: Claude Opus 5.5 <[email protected]>
# Conflicts: # README.md # uv.lock
Summary
Adds the plugin
generic.routing.random(distributionfoca-plugin-generic-routing-random), the successor of proTES's random task distribution:routereturns all candidate backends shuffled withrandom.SystemRandom. It never drops or adds candidates and never raisesNotApplicable.candidates(count) andseeded.params.seed(strict integer), for reproducible orders in tests. It is refused when pipelines are resolved unless the admin setsconfig.allow_seed(default off), because seeded orders are predictable.Stacked on #11: this branch contains the commits of #11 (FOCA 0.18.0 from PyPI). The PR targets
devso that CI runs. Merge #11 first; after that, this PR's diff shrinks to the plugin commits.Closes #7
Verification
pytest plugins/generic/routing/random: 24 passed. Coverage of the plugin is 100% (line and branch).pytest tests: 74 passed.pre-commit run --all-files: all hooks pass (ruff, ruff format, mypy, ast-grep, semgrep and the repo's checks).python -m scripts.packages mypy plugins/generic/routing/random: clean.mkdocs build --strict: builds.check_plugin_contractpasses, both unseeded and seeded.PluginHarnessand aPluginHostload the plugin through its installed entry point.Declined review findings
allow_seedis off. FOCA itself enforces this (resolve_stepsrunscheck_paramsfor client pipelines). The plugin'scheck_paramsis tested directly and through host construction.random.Randomseeds with the absolute value of the integer (seed: -1andseed: 1give the same order). The docs only promise that the order is a function of the seed.Open questions
None.
🤖 Generated with Claude Code
Summary by Sourcery
Add a generic random-routing plugin that preserves every candidate while providing secure default shuffling and opt-in predictable ordering for tests.
New Features:
generic.routing.randomplugin, which routes all candidate backends in random order and supports optional reproducible seeded orders for testing.Enhancements:
Build:
Documentation:
Tests:
Chores: