fix: improve commitment API route validation and recovery - #1872
Open
Olayiwola2904 wants to merge 34 commits into
Open
Olayiwola2904 wants to merge 34 commits into
Olayiwola2904 wants to merge 34 commits into
Conversation
|
@Olayiwola2904 is attempting to deploy a commit to the 1nonly's projects Team on Vercel. A member of the Team first needs to authorize it. |
|
@Olayiwola2904 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! 🚀 |
Contributor
Author
|
@Commitlabs-Org Hi! This PR is open and ready for review — happy to address any feedback. Thanks! |
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 improves the commitment API route validation by making state transitions deterministic, atomic, and recoverable. It introduces an explicit commitment state machine, request/response schema validation, authorization scoping, idempotent submission, and retry-safe failure recovery for
src/app/api/commitments/route.tsandsrc/app/api/commitments/search/route.ts. The change prevents duplicate submissions and stale responses from creating contradictory client state, and adds focused tests for success, failure, boundary, retry, and permission behavior.Related Issue
Refs #
Changes
🔐 Commitment State Machine & Invariants
[ADD]
src/lib/commitments/state-machine.tspending,confirmed,rejected,cancelled,recoverable.pending -> confirmed,pending -> rejected,pending -> cancelled,confirmed -> cancelled,recoverable -> pendingonly via explicit retry.409 Conflictand a reason; no silent state mutation.[MODIFY]
src/types/commitment.tsstatus,version,idempotencyKey,lastTransitionAt,failureReason, andrecoveryTokento the commitment domain type.🛡️ Validation & Authorization
[ADD]
src/lib/commitments/validation.tsPOST /api/commitmentsandGET /api/commitments/search.[MODIFY]
src/app/api/commitments/route.ts400invalid input,401/403auth,404missing commitment,409conflict/stale version,422invalid transition,500unexpected failure.202 Acceptedwith arecoveryTokenwhen an external side effect fails, preserving user intent without auto-retrying the on-chain action.🔎 Search Route Bounded Query Behavior
src/app/api/commitments/search/route.tslimitbetween 1 and 100, validates cursor, and restricts results to the authenticated user's commitments.🔁 Atomic Repository & Service Layer
[MODIFY]
src/lib/commitments/repository.tsUPDATE ... WHERE id = ? AND version = ?statements.(ownerId, idempotencyKey)constraint to prevent duplicate submissions.[MODIFY]
src/lib/commitments/service.tsrecoverableand storesrecoveryToken; explicit retry with the same token resumes the original intent exactly once.🧪 Automated Tests
[ADD]
src/lib/commitments/__tests__/state-machine.test.ts[ADD]
src/lib/commitments/__tests__/commitments-api.test.ts⚖️ Design Tradeoffs & Limitations
idempotencyKeyon retries.Verification Results
Acceptance walk-through:
✅ All state transitions are deterministic and protected by guards.
✅ Duplicate submissions return the existing result without creating a second commitment.
✅ Stale writes fail on version mismatch; clients can refresh and retry.
✅ Failed external actions return a recovery token and are not silently replayed.
pending,confirmed,rejected,cancelled,recoverablewith guarded transitionsRefs #<issue-number>Closes #1757