This repository was archived by the owner on Aug 12, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
feat(subscriptions): live transfer of subscriptions from non-deleted accounts #377
Open
lourou
wants to merge
14
commits into
feature/delete-account-impl
Choose a base branch
from
feature/delete-account-live-transfer
base: feature/delete-account-impl
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
ba34b3c
feat(subscriptions): restore live transfer, contest window, undo, and…
lourou 5f62616
test(deletion): port the live-transfer suite onto the shared reclaim …
lourou 91b97f1
feat(deletion): snapshot the pre-escrow wallet balance on the deletio…
lourou fca7248
fix(deletion): complete the notification purge when the installation …
lourou aa66721
fix(subscriptions): fail loudly when escrow runs after the wallet tea…
lourou 5d9e7a4
docs(delete-account): record agreed design decisions
lourou 0dbb1d3
Merge branch 'feature/delete-account-impl' into feature/delete-accoun…
lourou a70330c
docs(delete-account): correct retention wording — two clocks, no 30-d…
lourou 28c81f1
test(deletion): pin re-verify dedup across the 30-day record expiry
lourou 03d6ba2
Merge branch 'feature/delete-account-impl' into feature/delete-accoun…
lourou 94a3501
Merge branch 'feature/delete-account-impl' into feature/delete-accoun…
lourou 003c3de
Merge branch 'feature/delete-account-impl' into feature/delete-accoun…
lourou b28b16a
Merge branch 'feature/delete-account-impl' into feature/delete-accoun…
lourou 02ea5c7
Merge branch 'feature/delete-account-impl' into feature/delete-accoun…
lourou File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
3 changes: 3 additions & 0 deletions
3
prisma/migrations/20260722120000_add_deletion_final_balance/migration.sql
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| -- Display-only snapshot of the wallet balance at deletion time (pre-escrow). | ||
| -- Nullable: records written before this field existed stay null. | ||
| ALTER TABLE "DeletionRecord" ADD COLUMN "finalBalanceCredits" BIGINT; |
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,56 @@ | ||
| import { prisma } from "@/utils/prisma"; | ||
|
|
||
| /** | ||
| * Record "any authenticated act" on the account. The live-transfer contest | ||
| * window uses lastAuthAt strictly as a veto - an old owner who touches any | ||
| * authenticated route during the window cancels the pending transfer - so | ||
| * the stamp must be reliable exactly when it matters: | ||
| * | ||
| * - The write is awaited before the request proceeds (a fire-and-forget | ||
| * stamp could land after settlement locked and read the row). | ||
| * - The timestamp is database now(), the same clock that stamps the pending | ||
| * row's createdAt, so app/DB clock skew can never make a later act | ||
| * compare as older. | ||
| * - Throttling applies only while the account has no pending outgoing | ||
| * transfer. With one pending, every authenticated act is stamped | ||
| * unconditionally - a suppressed write inside the throttle window would | ||
| * otherwise leave lastAuthAt before the pending row and the transfer | ||
| * would settle despite real victim activity. | ||
| * - Failures propagate (fail closed): a failed stamp must never silently | ||
| * cost a veto. Callers fail the request with a 5xx so the client retries; | ||
| * the alternative - swallowing the error and proceeding - lets a | ||
| * transient DB blip during a contest window hand the subscription to the | ||
| * claimant despite real owner activity. An UPDATE matching zero rows | ||
| * (account deleted mid-request) is not a failure: there is no veto left | ||
| * to preserve. | ||
| */ | ||
| const STAMP_INTERVAL_MS = 5 * 60 * 1000; | ||
|
|
||
| let stampFailureForTests: Error | null = null; | ||
|
|
||
| /** Test seam: make stamp writes fail with the given error (null clears). */ | ||
| export const __setAuthActivityStampFailureForTests = ( | ||
| err: Error | null, | ||
| ): void => { | ||
| stampFailureForTests = err; | ||
| }; | ||
|
|
||
| export const stampAuthActivity = async ( | ||
| accountId: string, | ||
| knownLastAuthAt: Date | null, | ||
| ): Promise<void> => { | ||
| const withinThrottle = | ||
| knownLastAuthAt !== null && | ||
| Date.now() - knownLastAuthAt.getTime() < STAMP_INTERVAL_MS; | ||
| if (withinThrottle) { | ||
| const pending = await prisma.subscriptionTransfer.findFirst({ | ||
| where: { status: "pending", fromAccountId: accountId }, | ||
| select: { id: true }, | ||
| }); | ||
| if (!pending) return; | ||
| } | ||
| if (stampFailureForTests) throw stampFailureForTests; | ||
| await prisma.$executeRaw` | ||
| UPDATE "Account" SET "lastAuthAt" = now() WHERE id = ${accountId}::uuid | ||
| `; | ||
| }; | ||
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
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
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
Oops, something went wrong.
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🟠 High
accounts/auth-activity.ts:38stampAuthActivityissues a bareUPDATE "Account" SET "lastAuthAt" = now()without locking the row first, so a concurrent transfer settlement that runsSELECT ... FOR UPDATEon the account can acquire the lock before this UPDATE, read the stalelastAuthAt, and commit the transfer while this authenticated request is still waiting on the UPDATE. The stamp commits only after settlement has already made its veto decision, so a real owner act during the contest window fails to cancel the pending transfer. Awaiting the UPDATE does not close the read-to-write race. The stamp needs to acquire the account row lock (e.g.,SELECT id FROM "Account" WHERE id = ... FOR UPDATE) before thenow()write so it serializes before settlement's read — or settlement must re-checklastAuthAtafter any competing stamp.🚀 Reply "fix it for me" or copy this AI Prompt for your agent: