Repository navigation
feat: limit tasks per user (opt-in) - #456
Conversation
Add `custom.controllers.post_task.limits` with `max_unfinished`, `max_stored` and `exempt_roles`, all off by default. `POST /tasks` counts the tasks of the owner before the plugin pipeline runs and rejects tasks over a limit with `429 Too Many Requests`, naming the limit in the problem details. The limits are soft: concurrent requests of one user may exceed them. The feature lives in `pro_tes.limits`, with its own config model, and is called from one hook point in `create_task`; a new index on `user_id` and `task.state` serves the counts. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Describe `custom.controllers.post_task.limits` in the configuration reference (Task limits), with the soft limits and the new index, add the upgrade note for 0.6.0, point to the limits in Abusive tasks, and add an example to the chart's values. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Declare `429 Too Many Requests` of `POST /tasks` in the served spec and the API docs, keep all tests of the limits in `tests/limits/`, test both limits at once, users without roles, the log of rejections, the base config and the documented problem type, and reword the pointer to the limits in Abusive tasks. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Also state in the docs that exempt roles are those that access control applies to the request, so opt-in roles count only if activated. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Reviewer's GuideSchema and migrationsAdd the owner-and-state database index that supports per-user task counts.
Data structuresDefine the task-limit settings and attach their opt-in defaults to the post-task configuration.
APIDeclare the 429 response for task creation in the served API specification.
Uses of the APIEnforce task limits in task creation before the plugin pipeline, using the request owner, roles, and configured limits.
ImplementationImplement per-user unfinished and stored task counting, role exemptions, and the 429 gateway error.
ConfigurationAdd a commented deployment example for enabling task limits.
DocumentationDocument the task-limit settings, behavior, API error, operations guidance, and upgrade impact.
TestsTest limit configuration and counting behavior, endpoint rejection and exemptions, index creation, and the served OpenAPI response.
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 #456 +/- ##
=========================================
Coverage 100.00% 100.00%
=========================================
Files 24 27 +3
Lines 2832 2875 +43
=========================================
+ Hits 2832 2875 +43 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
Against MongoDB, the driver logs the stored tasks at debug level, so the test of the log of rejections checks the records of `pro_tes.limits` only. Co-Authored-By: Claude Opus 5.5 <[email protected]>
There was a problem hiding this comment.
Hey - I've found 1 issue
Prompt for AI Agents
Please address the comments from this code review:
## Individual Comments
### Comment 1
<location path="tests/limits/test_check.py" line_range="89" />
<code_context>
+
+def test_problem_type_documented() -> None:
+ """The problem type of the errors points to a section of the docs."""
+ assert PROBLEM_TYPE.endswith("/configuration/#task-limits")
+ text = (ROOT / "docs" / "configuration.md").read_text()
+ assert "\n### Task limits\n" in text
</code_context>
<issue_to_address>
**Wrong problem-type host passes**
When `PROBLEM_TYPE` is changed to an unrelated host while retaining `/configuration/#task-limits`, `test_problem_type_documented` checks only the URL suffix and that the heading exists; `test_max_unfinished` compares the emitted value to the same constant, so both pass even though clients are sent to a host that does not serve the documentation.
Assert the complete expected documentation URL, or verify that the problem type resolves to the intended documentation site.
</issue_to_address>Linked issues
#285 feat: limit tasks per user (opt-in) · 4 of 4 met
| # | Requirement | Status | Notes |
|---|---|---|---|
| 1 | The limit is a configuration option and is off by default. | ✅ Met | TaskLimits defaults both limits to null, and the check returns without limiting tasks when neither is configured. |
| 2 | When the limit is reached, new tasks are rejected with a clear error that names the limit. | ✅ Met | The check raises TaskLimitReached with status 429 and names the setting in the problem detail. |
| 3 | Unfinished and stored tasks can be limited separately; admins may be exempt (configurable). | ✅ Met | The configuration provides independent max_unfinished and max_stored limits and configurable exempt_roles. |
| 4 | Document the feature in docs/configuration.md, with the trade-offs. | ✅ Met | The Task limits section describes the settings, stored-task retention implications, and soft-limit behavior under concurrent requests. |
Sourcery assessment
Needs a human reviewer. 1 finding to address first, and if the limits reject more submissions than intended, affected clients receive 429 responses and can retry; reverting the code restores the previous behavior. The new MongoDB index remains after a revert, though it is bounded and can be dropped.
Blocking findings: tests/limits/test_check.py:89
Compare the problem type with the default docs site of `mkdocs.yml`, not only the path of its section. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Summary
Opt-in limits of the tasks per user (
custom.controllers.post_task.limits), all off by default:max_unfinished: maximum unfinished tasks per user (any state but the finished ones, so alsoUNKNOWNandCANCELING);max_stored: maximum stored tasks per user;exempt_roles: roles whose users are not limited (empty by default).POST /taskscounts the tasks of the owner (get_owner(),user_id) before the plugin pipeline runs. A task over a limit gets429 Too Many Requestswith the limit named in the problem detail (and aproblem_typepointing to the docs), and is neither stored nor forwarded. The limit is soft: count, then insert, so concurrent requests of one user can exceed it by up to one task per concurrent request (documented). A new index{user_id: 1, task.state: 1}serves the counts; each count stops at the limit.Closes #285
Containment and plugin hook points
The feature lives in
pro_tes/limits/(config modelTaskLimits,check_task_limits(),TaskLimitReached), with its tests intests/limits/and its own docs section (Configuration, Task limits). It doesn't import the retention work (#286). Core touches only these hook points, which a plugin would need:TaskRuns.create_task, before the pipeline, with the tasks collection, the owner, the caller's roles (Principal.groups) and the config. In a plugin this is arequest-phase check that can count stored tasks.PostTask.limits) and its defaults inconfig.yaml.{user_id, task.state}indbofconfig.yaml.429response declared inpro_tes/api/additions.openapi.yaml.TaskLimitReachedis a FOCAGatewayError, so the existing error path ofCreateTaskserves its problem details without a new entry in the exception mapping.Docs
docs/configuration.md:post_task.limitsin Tasks, plus a new section Task limits (settings, 429, soft limits, owners, index).docs/api.md: 429 in Errors and in the declared spec extensions.docs/upgrading.md(To version 0.6.0): opt-in note, and the new index created on the first start.docs/operations.md(Abusive tasks): pointer to the limits.deployment/values.yaml: commented example (the chart passesconfig.customthrough with the app's snake_case keys).Release notes are written in the release-prep PR (
docs/releases/), as for earlier releases.Verification
tests/limits/failed before the hook point existed (unknown config key, no 429, no index) and pass now.coverage run -m pytest(mongomock, withhelm dependency build deploymentfor the chart tests): 1746 passed, 11 skipped.coverage report: 100%.ruff check/ruff format --check(0.17.3),mypy,ast-grep scanandsemgrep --config=.semgrep: clean.mkdocs build --strict: OK.MONGODB_TEST_URI, privatemongod):tests/limits,tests/test_app.py,tests/ga4gh/tes/test_server.py: 200 passed (after fixing a log assertion that the driver's debug logs broke in CI's first run).CI passed), including Test (MongoDB 7.0), Smoke test, Chart and Docs.adversarial-reviewerandquality-reviewer. Fixed: the 429 declared in the spec and the API docs, all limit tests moved intotests/limits/, more edge-case tests, the docs wording, and the docs note on opt-in roles.Declined review findings
Open questions
None.
🤖 Generated with Claude Code
Summary by Sourcery
Enable deployments to optionally constrain per-user task usage while preserving the existing unlimited behavior by default.
New Features:
Enhancements:
Deployment:
Documentation:
Tests: