Skip to content

feat: limit tasks per user (opt-in) - #456

Merged
uniqueg merged 6 commits into
devfrom
feat/task-limits
Nov 7, 2026
Merged

uniqueg merged 6 commits into
devfrom
feat/task-limits

Conversation

@uniqueg

@uniqueg uniqueg commented Nov 7, 2026 •

Copy link
Copy Markdown
Member

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 also UNKNOWN and CANCELING);
  • max_stored: maximum stored tasks per user;
  • exempt_roles: roles whose users are not limited (empty by default).

POST /tasks counts the tasks of the owner (get_owner(), user_id) before the plugin pipeline runs. A task over a limit gets 429 Too Many Requests with the limit named in the problem detail (and a problem_type pointing 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 model TaskLimits, check_task_limits(), TaskLimitReached), with its tests in tests/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:

  1. A call in 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 a request-phase check that can count stored tasks.
  2. A config field (PostTask.limits) and its defaults in config.yaml.
  3. The index {user_id, task.state} in db of config.yaml.
  4. The 429 response declared in pro_tes/api/additions.openapi.yaml.

TaskLimitReached is a FOCA GatewayError, so the existing error path of CreateTask serves its problem details without a new entry in the exception mapping.

Docs

  • docs/configuration.md: post_task.limits in 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 passes config.custom through with the app's snake_case keys).

Release notes are written in the release-prep PR (docs/releases/), as for earlier releases.

Verification

  • Test first: the new tests in tests/limits/ failed before the hook point existed (unknown config key, no 429, no index) and pass now.
  • coverage run -m pytest (mongomock, with helm dependency build deployment for the chart tests): 1746 passed, 11 skipped. coverage report: 100%.
  • ruff check / ruff format --check (0.17.3), mypy, ast-grep scan and semgrep --config=.semgrep: clean.
  • mkdocs build --strict: OK.
  • Against MongoDB 7.0 locally (MONGODB_TEST_URI, private mongod): 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: all checks passed (CI passed), including Test (MongoDB 7.0), Smoke test, Chart and Docs.
  • Review: adversarial-reviewer and quality-reviewer. Fixed: the 429 declared in the spec and the API docs, all limit tests moved into tests/limits/, more edge-case tests, the docs wording, and the docs note on opt-in roles.

Declined review findings

  • Move the index creation to the top of the upgrade notes (low): the feature's note already says that the index is built on the first start and takes a moment for large collections; the notes stay ordered by topic.

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:

  • Add opt-in per-user limits for unfinished and stored tasks, with configurable role exemptions.

Enhancements:

  • Reject task creation with documented 429 problem details when a configured per-user limit is reached, before tasks are stored or forwarded.
  • Add the database index and configuration support required to efficiently count tasks by owner and state.

Deployment:

  • Provide deployment configuration examples for enabling task limits.

Documentation:

  • Document task-limit settings, soft-limit behavior, API errors, operations guidance, and upgrade requirements.

Tests:

  • Add unit and endpoint coverage for limit enforcement, exemptions, error responses, defaults, and the owner/state index.

uniqueg and others added 4 commits November 7, 2026 14:16
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]>
@uniqueg uniqueg added this to the v0.6.0 milestone Nov 7, 2026
@sourcery-ai

sourcery-ai Bot commented Nov 7, 2026 •

Copy link
Copy Markdown

Reviewer's Guide

Schema and migrations

Add the owner-and-state database index that supports per-user task counts.

  • pro_tes/config.yaml L121-125

Data structures

Define the task-limit settings and attach their opt-in defaults to the post-task configuration.

  • pro_tes/config.yaml L275-283
  • pro_tes/config_models.py L18-156 (4 small edits)
  • pro_tes/limits/config.py L1-31

API

Declare the 429 response for task creation in the served API specification.

  • pro_tes/api/additions.openapi.yaml L25-33

Uses of the API

Enforce task limits in task creation before the plugin pipeline, using the request owner, roles, and configured limits.

  • pro_tes/ga4gh/tes/task_runs.py L81, L368-369, L395-402

Implementation

Implement per-user unfinished and stored task counting, role exemptions, and the 429 gateway error.

  • pro_tes/limits/__init__.py L1-19
  • pro_tes/limits/check.py L1-90

Configuration

Add a commented deployment example for enabling task limits.

  • deployment/values.yaml L53-58

Documentation

Document the task-limit settings, behavior, API error, operations guidance, and upgrade impact.

  • docs/api.md L174-175, L189-191, L204-205
  • docs/configuration.md L582-626
  • docs/operations.md L146-148
  • docs/upgrading.md L116-122

Tests

Test limit configuration and counting behavior, endpoint rejection and exemptions, index creation, and the served OpenAPI response.

  • tests/limits/conftest.py L1-10
  • tests/limits/test_check.py L1-181
  • tests/limits/test_endpoint.py L1-107
  • tests/test_app.py L135, L147

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

@read-the-docs-community

read-the-docs-community Bot commented Nov 7, 2026 •

Copy link
Copy Markdown

Documentation build overview

📚 proTES | 🛠️ Build #35720140 | 📁 Comparing e3c360b against latest (92e24a1)

  🔍 Preview build  

5 files changed · ± 5 modified

± Modified

@codecov

codecov Bot commented Nov 7, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 100.00%. Comparing base (92e24a1) to head (e3c360b).
⚠️ Report is 1 commits behind head on dev.

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.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

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]>
@uniqueg
uniqueg marked this pull request as ready for review November 7, 2026 14:30

@sourcery-ai sourcery-ai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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


Sourcery is free for open source - if you like our reviews please consider sharing them ✨

Comment thread tests/limits/test_check.py Resolved
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]>

@sourcery-ai sourcery-ai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sourcery assessment

Approved.

@uniqueg
uniqueg merged commit 53ce92b into dev Nov 7, 2026
36 checks passed
@uniqueg
uniqueg deleted the feat/task-limits branch November 7, 2026 14:46
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

feat: limit tasks per user (opt-in)

1 participant