feat(budgets): add Redis-backed distributed mutex guard for spending limits - #136
Open
lewistemple22 wants to merge 4 commits into
Open
feat(budgets): add Redis-backed distributed mutex guard for spending limits#136lewistemple22 wants to merge 4 commits into
lewistemple22 wants to merge 4 commits into
Conversation
…limits Implements SpendingLimitGuardService that wraps the budget check+consume cycle in a single Redis distributed lock (SETNX with TTL) to eliminate the TOCTOU race condition where concurrent agent transactions could both pass assertWithinBudget before either records spend. Changes: - Add SpendingLimitGuardService with atomic guardAndConsume method - Wire RedisLock and guard into BudgetModule - Integrate guard into TransactionService for auto-executable transactions - Add 12 Vitest unit tests covering lock acquisition, headroom validation, concurrent attempts, and error handling Closes ASTROIDX556#86
|
@lewistemple22 Great news! 🎉 Based on an automated assessment of this PR, the linked Wave issue(s) no longer count against your application limits. You can now already apply to more issues while waiting for a review of this PR. Keep up the great work! 🚀 |
|
MergeKeeper review unavailable AI provider review response did not contain valid JSON No approval or merge action was taken. |
|
MergeKeeper review Scope: in scope for linked issue The PR successfully implements a Redis-backed distributed mutex guard for spending limit evaluation with appropriate tests and transaction pipeline integration. Reviewed commit: |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Overview
This PR adds a distributed locking mechanism using Redis to prevent race conditions when multiple concurrent AI agent transactions attempt to evaluate and deduct from the same spending budget simultaneously.
Related Issue
Closes #86
Changes
🛡️ Distributed Mutex Guard
src/modules/budgets/services/spending-limit-guard.service.ts—SpendingLimitGuardServicewraps the budget check + consume cycle in a single Redis distributed lock (SETNXwith TTL) to eliminate the TOCTOU race conditionsrc/modules/budgets/budget.module.ts— WireRedisLockandSpendingLimitGuardServiceinto the module providers and exports🔗 Transaction Pipeline Integration
src/modules/transactions/transaction.service.ts— For auto-executable transactions, useSpendingLimitGuardService.guardAndConsume()instead of separateassertWithinBudget()+consume()calls. Approval-required transactions still use the check-only path with consume deferred toexecute().🧪 Unit Tests
src/modules/budgets/services/spending-limit-guard.service.spec.ts— 12 Vitest tests covering:Verification Results
Acceptance Criteria
SpendingLimitGuardServicewith configurable TTL (default 5s)lock:budget:{id}PX+NXflags with Lua-based safe releasetsc --noEmit+vitest runall green