This repository was archived by the owner on Oct 24, 2026. It is now read-only.
Repository navigation
fix(trust): close all twelve remaining audit findings — no money write from a stale read #305
Merged
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
c4724be
fix(permits): decide every budget write in the database, not from a s…
claude 8d56a97
fix(billing,wallets): decide every wallet money move in the database
claude 2e1c11b
fix(idempotency,billing): surface the orphaned local debit; cap the c…
claude 3fca6b3
Merge origin/main (#304) into the trust-integrity branch
claude 0b31bd9
fix(review): restore the release state check; make the velocity rever…
claude 55f03d4
fix(billing,awi): stop a mid-charge freeze debiting; don't freeze con…
claude 684c103
Merge origin/main (#306) into the trust-integrity branch
claude 5525051
fix(billing): a freeze is not a balance deficit, in the race path too
claude aa75f5b
fix(permits,stripe): surface skipped budget repairs; timestamp the fi…
claude 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
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,33 @@ | ||
| """Reusable SQL expressions for writes that must not lose a concurrent update. | ||
|
|
||
| The money paths in this application mutate counters that several requests can | ||
| touch at once. The unsafe form is always the same: read a column into Python, | ||
| compute a new total, write the total back. That is a read-modify-write, and it | ||
| is serialized only by whatever row lock happens to be held — which is nothing | ||
| at all on SQLite, where ``SELECT ... FOR UPDATE`` parses and does nothing. | ||
|
|
||
| Expressing the arithmetic against the *column* instead of against a value this | ||
| process read keeps the whole operation relative, so the database applies it to | ||
| whatever is stored at write time. | ||
| """ | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| from decimal import Decimal | ||
| from typing import Any, cast | ||
|
|
||
| from sqlalchemy import case | ||
| from sqlalchemy.sql.elements import ColumnElement | ||
|
|
||
|
|
||
| def clamped_decrement(column: Any, amount: Decimal) -> Any: | ||
| """``max(0, column - amount)``, evaluated by the database. | ||
|
|
||
| The Python spelling of this clamp (``max(Decimal("0"), col - amount)``) | ||
| has to read the column first, which reintroduces the lost update the | ||
| relative form exists to prevent. | ||
| """ | ||
| return case( | ||
| (cast(ColumnElement[bool], column - amount < Decimal("0")), Decimal("0")), | ||
| else_=column - amount, | ||
| ) |
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.
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.
Uh oh!
There was an error while loading. Please reload this page.