Express conditional writes and transaction cleanup recipes
Choose the Express implementation for the database boundary you need to preserve: reserve stock with one conditional update or release the PostgreSQL client after success and failure. Each recipe links its saved requirement and captured source checks.
Download the Express rule pack ZIP · Inspect the YAML rules
JavaScript
Checking stock in JavaScript and then writing a new total permits two requests to reserve the same remaining unit. A conditional UPDATE puts the availability decision into the database statement that changes the row. Parameter binding keeps the SKU out of SQL syntax.
Broken edit flagged · corrected edit passes
JavaScript
A transaction helper often looks correct in a successful request while leaking a connection on an exception. Under load the pool exhausts and later requests wait indefinitely. finally covers the paths that normal return statements and rollback handlers miss.
Broken edit flagged · corrected edit passes
Introductory framework example
Use a correction as a saved requirement
Choose the failure you need to prevent, add the scoped rule to your repository, and check both the broken and corrected implementation. Ask your agent to inspect the finding, repair the code and recheck the same rule. Keep framework tests beside the model check.
Perch keeps the requirement beside the code. Reuse the named check during the agent’s next edit and in CI, with cached answers free on unchanged requests.
Official Express documentation · All language and framework recipes
Inspect the edit and the saved check
The Reserve stock in Express with one conditional database update example checks reserve.cjs. The source below is the same example used by its linked capture.
Compare the broken edit with the corrected implementation
Broken implementation
async function reserve(client, tenantId, sku, quantity) {
if (!Number.isSafeInteger(quantity) || quantity <= 0) {
throw new Error("Quantity must be a positive integer");
}
const result = await client.query(
`UPDATE inventory SET available = available - $1
WHERE tenant_id = $2 AND sku = $3
RETURNING available`,
[quantity, tenantId, sku],
);
if (result.rowCount !== 1) throw new Error("Stock not available");
return result.rows[0].available;
}
module.exports = { reserve };
Corrected implementation
async function reserve(client, tenantId, sku, quantity) {
if (!Number.isSafeInteger(quantity) || quantity <= 0) {
throw new Error("Quantity must be a positive integer");
}
const result = await client.query(
`UPDATE inventory SET available = available - $1
WHERE tenant_id = $2 AND sku = $3 AND available >= $1
RETURNING available`,
[quantity, tenantId, sku],
);
if (result.rowCount !== 1) throw new Error("Stock not available");
return result.rows[0].available;
}
module.exports = { reserve };
Exact source and edit · Raw captured check
Make the agent check the requirement after its edit
Keep your existing perch.yaml. The scoped rule pack installs the saved requirement under .perch/rules/. Review its file selector and use the implementation’s integration prerequisites before adapting it to your application.
npm install -g @lakeday/perch
perch login
# Choose the setup command for your agent:
perch setup claude-code
# Or: perch setup codex
# Or: perch setup cursor
Check the edit and recheck the repair
curl -fL 'https://reporules.fyi/express/transactional-reservation/rule-pack.zip' -o requirement.zip
unzip -n requirement.zip
perch check reserve.cjs --rules stock-never-negative --json
# Read the finding, repair the implementation, then repeat:
perch check reserve.cjs --rules stock-never-negative --json
Run the same check on the repaired working file before committing. Keep the application regression tests alongside it. Fresh model judgments can vary; an unchanged request can reuse its cached answer. A passing check does not prove application correctness.