Source: https://frameworkrecipes.dev/problems/concurrent-writes/

[Agent repair workflows by failure](https://frameworkrecipes.dev/problems/)

# Reject stale edits and serialize competing writes

Put the availability decision inside a lock or conditional update, and reject edits based on an obsolete version. Choose the mechanism for the conflict: competing reservations and a customer submitting an old screen need different checks.

## How do I keep two requests from changing the same stock or order incorrectly?

Choose the implementation for your framework. Each linked guide includes the source, integration prerequisites, failure-path tests, saved requirement and recorded check.

Existing captured source checks; these are individual examples

| Implementation | Saved requirement | Broken source | Corrected source | Capture date |
| --- | --- | --- | --- | --- |
| [Serialize Rails stock reservations with an Active Record row lock](https://frameworkrecipes.dev/rails/locked-stock/) | locked-stock-availability | Rule finding | No rule finding | [2026-10-14](https://frameworkrecipes.dev/rails/locked-stock/capture.json) |
| [Reject stale Rails cancellation decisions with optimistic locking](https://frameworkrecipes.dev/rails/optimistic-cancellation/) | submitted-order-version | Rule finding | No rule finding | [2026-10-14](https://frameworkrecipes.dev/rails/optimistic-cancellation/capture.json) |
| [Reserve stock in Express with one conditional database update](https://frameworkrecipes.dev/express/transactional-reservation/) | stock-never-negative | Rule finding | No rule finding | [2026-10-14](https://frameworkrecipes.dev/express/transactional-reservation/capture.json) |
| [Return a conflict when EF Core detects a stale order edit](https://frameworkrecipes.dev/aspnet-core/optimistic-concurrency/) | stale-update-is-not-success | Rule finding | No rule finding | [2026-10-14](https://frameworkrecipes.dev/aspnet-core/optimistic-concurrency/capture.json) |

## Inspect the requirement before installing it

The [Serialize Rails stock reservations with an Active Record row lock](https://frameworkrecipes.dev/rails/locked-stock/) example uses this requirement on `app/services/reserve_stock.rb`:

```
- name: locked-stock-availability
  where: "app/services/reserve_stock.rb"
  ensure: >-
    Inside the row lock, available stock must be checked against quantity before it is decremented. Reject reservations that exceed the locked stock.
```

[Exact corrected source and broken edit](https://frameworkrecipes.dev/rails/locked-stock/source.json) · [Recorded Perch output](https://frameworkrecipes.dev/rails/locked-stock/capture.json) · [Download this requirement](https://reporules.fyi/rails/locked-stock/rule-pack.zip)

## Make the agent check the requirement after its edit

Keep your existing perch.yaml. The [scoped rule pack](https://reporules.fyi/rails/locked-stock/) installs the saved requirement under .perch/rules/. Review its file selector and use the implementation’s integration prerequisites before adapting it to your application.

### Install Perch and configure your agent

```
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/rails/locked-stock/rule-pack.zip' -o requirement.zip
unzip -n requirement.zip
perch check app/services/reserve_stock.rb --rules locked-stock-availability --json
# Read the finding, repair the implementation, then repeat:
perch check app/services/reserve_stock.rb --rules locked-stock-availability --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.

## Compare the implementations before choosing a rule

- **[Serialize Rails stock reservations with an Active Record row lock](https://frameworkrecipes.dev/rails/locked-stock/)**

  The lock reloads the record and serializes competing reservations. An availability check made before taking the lock can become stale. Keeping the check, decrement, and save inside with_lock makes the inventory decision atomic for that row. [Inspect its rule pack](https://reporules.fyi/rails/locked-stock/).
- **[Reject stale Rails cancellation decisions with optimistic locking](https://frameworkrecipes.dev/rails/optimistic-cancellation/)**

  Active Record detects a write conflict after the record is loaded, but it cannot infer that a customer made a decision using an older screen. Compare the submitted version first, then let lock_version protect the save from a race that occurs during this request. [Inspect its rule pack](https://reporules.fyi/rails/optimistic-cancellation/).
- **[Reserve stock in Express with one conditional database update](https://frameworkrecipes.dev/express/transactional-reservation/)**

  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. [Inspect its rule pack](https://reporules.fyi/express/transactional-reservation/).
- **[Return a conflict when EF Core detects a stale order edit](https://frameworkrecipes.dev/aspnet-core/optimistic-concurrency/)**

  A concurrency token lets EF put the expected row version into the UPDATE predicate. When another edit won, the application must return a conflict or explicitly resolve it, rather than tell the caller their old decision succeeded. [Inspect its rule pack](https://reporules.fyi/aspnet-core/optimistic-concurrency/).

## Keep the check tied to its source

The table reports saved checks on the exact linked source versions. Adapt the paths, adapters and regression tests to your repository. The example source may differ from the released framework or your service; the recorded outcome belongs to that example.

[Other agent-edit failure paths](https://frameworkrecipes.dev/problems/) · [All framework recipes](https://frameworkrecipes.dev/)

**Framework Recipes**

Implementation, failure path, check.

Published by Perch · © 2026 Verglas LLC. Framework Recipes is a Perch publication.

Perch’s MIT license covers the CLI; hosted model analysis is a paid service. Relevant source is sent to the configured model service.

[Perch CLI MIT license](https://github.com/lakeday-org/perch/blob/main/LICENSE) · [Article text](https://frameworkrecipes.dev/problems/concurrent-writes/index.md) · [Resource index](https://frameworkrecipes.dev/llms.txt)

[Privacy](https://frameworkrecipes.dev/privacy/) ·
