Skip to content

Add regression tests for the token rate-limit admission race #74

Description

@neooriginal

This issue is AI generated.

Problem

The token rate-limit admission check in server/services/ai/engine.js:1789–1804 reads a SUM(total_tokens) snapshot and then proceeds without holding a lock. Two concurrent runs started within milliseconds of each other both pass the check before either has written usage tokens to the database. The existing unit tests do not simulate this scenario, so the race goes untested and any fix introduced for issue #42 has no regression coverage.

Evidence

  • server/services/ai/engine.js:1789–1804 — read-then-proceed admission check.
  • No test in test/backend/unit/ or test/integration/ submits two concurrent runs and asserts the second is rejected when the limit would be exceeded.

Required change

Add an integration or unit test that:

  1. Sets a low token limit (e.g. 100 tokens) for a test user.
  2. Simultaneously initiates two runs (via Promise.all or concurrent await) before either completes.
  3. Asserts that exactly one run succeeds and the second is rejected with a rate-limit error.
  4. Verifies the total committed tokens do not exceed the limit after both settle.

This test should fail on the current code and pass only after #42 is fixed.

Acceptance criteria

Metadata

Metadata

Assignees

No one assigned

    Labels

    autonomousGenerated by AIenhancementNew feature or requestpriority: mediumAddress in upcoming sprint — functional bug or important enhancementreliabilityReliability, resilience, or concurrency concerntestingTest coverage or test infrastructure work

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions