Source: https://frameworkrecipes.dev/problems/transaction-boundaries/

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

# Keep state changes and events in one transaction

Keep the state change and its outbox or job record on the same transaction handle. A commit between the writes can leave the state changed without a durable event. Dispatch external work after the transaction commits.

## How do I preserve a transaction when an agent refactors my service?

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 |
| --- | --- | --- | --- | --- |
| [Keep a FastAPI order update and its outbox event in one transaction](https://frameworkrecipes.dev/fastapi/outbox-transaction/) | outbox-no-early-commit | Rule finding | No rule finding | [2026-10-14](https://frameworkrecipes.dev/fastapi/outbox-transaction/capture.json) |
| [Create a Django outbox event atomically with the order change](https://frameworkrecipes.dev/django/durable-outbox/) | outbox-same-atomic-block | Rule finding | No rule finding | [2026-10-14](https://frameworkrecipes.dev/django/durable-outbox/capture.json) |
| [Keep Spring order confirmation and its outbox write under one proxy transaction](https://frameworkrecipes.dev/spring-boot/transactional-outbox/) | confirmation-transaction-boundary | Rule finding | No rule finding | [2026-10-14](https://frameworkrecipes.dev/spring-boot/transactional-outbox/capture.json) |
| [Keep NestJS event persistence on the supplied TypeORM transaction manager](https://frameworkrecipes.dev/nestjs/outbox-transaction-manager/) | outbox-transaction-manager | Rule finding | No rule finding | [2026-10-14](https://frameworkrecipes.dev/nestjs/outbox-transaction-manager/capture.json) |

## Inspect the requirement before installing it

The [Keep a FastAPI order update and its outbox event in one transaction](https://frameworkrecipes.dev/fastapi/outbox-transaction/) example uses this requirement on `confirmation.py`:

```
- name: outbox-no-early-commit
  where: "confirmation.py"
  ensure: >-
    The order update and outbox insertion stay in the same transaction. The function must not explicitly commit inside the session.begin block before it finishes successfully.
```

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

## Make the agent check the requirement after its edit

Keep your existing perch.yaml. The [scoped rule pack](https://reporules.fyi/fastapi/outbox-transaction/) 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/fastapi/outbox-transaction/rule-pack.zip' -o requirement.zip
unzip -n requirement.zip
perch check confirmation.py --rules outbox-no-early-commit --json
# Read the finding, repair the implementation, then repeat:
perch check confirmation.py --rules outbox-no-early-commit --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

- **[Keep a FastAPI order update and its outbox event in one transaction](https://frameworkrecipes.dev/fastapi/outbox-transaction/)**

  Publishing a message after a database write leaves a crash window. An outbox row is written with the order state, so a worker can deliver it later. A unique event_key prevents accidental duplicate event insertion; a delivery consumer still needs its own deduplication. [Inspect its rule pack](https://reporules.fyi/fastapi/outbox-transaction/).
- **[Create a Django outbox event atomically with the order change](https://frameworkrecipes.dev/django/durable-outbox/)**

  An on_commit callback runs after commit but does not make message delivery durable. Store the event in the transaction and let a retrying worker publish it. Moving create outside the block makes a failed event insert leave a confirmed order without a delivery record. [Inspect its rule pack](https://reporules.fyi/django/durable-outbox/).
- **[Keep Spring order confirmation and its outbox write under one proxy transaction](https://frameworkrecipes.dev/spring-boot/transactional-outbox/)**

  Repository methods may each be transactional while the service action is not. Without the outer transaction, saving the order and inserting its event can commit independently. A public proxy-invoked service method supplies the required boundary. [Inspect its rule pack](https://reporules.fyi/spring-boot/transactional-outbox/).
- **[Keep NestJS event persistence on the supplied TypeORM transaction manager](https://frameworkrecipes.dev/nestjs/outbox-transaction-manager/)**

  TypeORM’s global manager can run outside the transaction callback’s connection. That can leave an event committed without its business change or a change committed without its event. Keep every related read and write on the manager supplied to the callback. [Inspect its rule pack](https://reporules.fyi/nestjs/outbox-transaction-manager/).

## 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/transaction-boundaries/index.md) · [Resource index](https://frameworkrecipes.dev/llms.txt)

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