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:
- Sets a low token limit (e.g. 100 tokens) for a test user.
- Simultaneously initiates two runs (via
Promise.all or concurrent await) before either completes.
- Asserts that exactly one run succeeds and the second is rejected with a rate-limit error.
- 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
This issue is AI generated.
Problem
The token rate-limit admission check in
server/services/ai/engine.js:1789–1804reads aSUM(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.test/backend/unit/ortest/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:
Promise.allor concurrentawait) before either completes.This test should fail on the current code and pass only after #42 is fixed.
Acceptance criteria