0
Fork 0
mirror of https://github.com/mastra-ai/mastra.git synced 2026-11-17 08:44:46 +00:00

fix(factory): delete an intake rule without renumbering the rules after it (#30613)

## Problem

On a deployment with a remote Postgres (e.g. Shipyard), `DELETE
/web/factory/projects/:id/intake-routes/:routeId` took very long, and
deleting a rule broke other admins' open rule edits.

The delete closed the position gap it left: it rewrote every rule after
the deleted one with two sequential UPDATEs each (park at a negative
position, then the final position — ~2N round trips inside one
transaction). Each rewrite also bumped the rule's revision, so an edit
another admin had open failed with a stale-revision 409 when saved.
Deleting a board compacted its surviving project's rules the same way.

## Fix

Stop compacting positions on delete. Gaps are safe: every reader sorts
by `position`, new rules insert at `max + 1`, and reorder / insert-ahead
renumber the whole list anyway. The single-rule delete and the board
delete now only remove the deleted rows — O(1) route-table writes, and
the surviving rules keep their positions, revisions and order. No schema
change; the UI already renders rules by list order.

## Tests

- New storage scenario (LibSQL + Postgres): rules over two boards,
delete one near the top → zero writes to the route table (spied), the
other rules keep order/revision/fields exactly, an edit loaded before
the delete still saves, and later insert + reorder still work over the
gap.
- New HTTP scenario (LibSQL + Postgres): delete a rule through the API →
remaining rules keep revision 1 and a PATCH holding a pre-delete
revision answers 200 instead of 409.
- Updated the board-delete scenario to pin that surviving rules keep
their positions/revisions, and removed assertions that pinned the old
contiguous renumbering.
- Both new scenarios were verified red on the previous code (8 route
writes / revision bumps / 409) and green on this change.
- Net test lines: +45 (81 insertions, 36 deletions across 4 test/source
files).

Gates: `build:lib`, `tsc --noEmit`, `oxlint --type-aware`, `oxfmt`,
focused intake/boards/dispatcher suites on LibSQL and a dedicated
postgres:16 container, CLI route metadata regenerated with no drift.


<!-- This is an auto-generated comment: release notes by coderabbit.ai
-->
<!-- coderabbit-ai-generated:v1 -->

**ELI5:** When someone deletes an intake rule, the app used to renumber
every rule below it. That caused slow writes on remote databases and
conflicts for editors who had a rule open. Now deletion removes only the
selected rule, and the other rules keep their positions and revisions.

## Summary

This change stops intake-rule deletion from renumbering surviving rules.
It applies to single-rule deletion and to board deletion. The change
also prevents board-scoped reorder and insert-ahead from renumbering or
revision-bumping rules that do not move.

Before this change, deletion closed the position gap by updating every
later rule. On remote Postgres, this added many route-table writes. It
also changed each survivor's revision. An editor who loaded a rule
before the delete then received a stale-revision conflict on save.

After this change:

- Deletion removes only the selected rows.
- Surviving rules keep their positions, revisions, and order.
- Position gaps are allowed. Readers sort by position.
- New rules use `max + 1`.
- Project-wide reorder still compacts positions.
- Board-scoped reorder assigns the board's rules only to position slots
that the board already holds. Other boards' rules keep their positions
and revisions.
- Insert-ahead uses a free gap before the catch-all when one exists.
Otherwise, it moves only the catch-all and the rules after it.
- Default seeding is unchanged. It places new defaults at the start of
the list.
- No schema change is included.

## Changes by file

- **`.changeset/purple-buckets-spend.md`**: Adds a patch changeset for
`@mastra/factory`. It describes the delete fix. (+5/−0)
- **`mastracode/factory/src/storage/domains/boards/base.ts`**: Removes
renumbering from intake-route and board deletion. Adds the gap-aware
insert-ahead and board-scoped reorder behavior. `placeIntakeRoutes`
delegates to `moveIntakeRoutes`, which leaves routes already in place
unchanged. (+45/−14)
-
**`mastracode/factory/src/storage/domains/boards/intake-routes.test.ts`**:
- The route-order helper accepts strictly increasing, distinct positions
with gaps.
  - `emptyProject` deletes seeded routes in listed order.
- A new test covers deletion of an early route. It checks that no
route-table updates run. It checks that other routes keep their order,
fields, and revisions. It checks that an edit using a previously read
revision succeeds, and that later insert and reorder work across the
gap.
- New tests cover board-scoped reorder across a gap and insert-ahead
across a gap.
- The concurrency test title and comments describe adding, deleting, and
editing routes. (+94/−22)
-
**`mastracode/factory/src/storage/domains/boards/intake-order.test.ts`**:
The board-deletion test expects surviving routes to keep their positions
and revisions. The title states that other boards' rules are unchanged.
(+5/−4)
- **`mastracode/factory/src/routes/intake-routes.test.ts`**: Test setup
deletes unseeded routes in listed order. A new regression test checks
that deleting one route leaves other revisions unchanged. It also checks
that an editor holding a pre-delete revision can update a later route
without a conflict. (+24/−2)

Totals: 5 files changed, +173 lines, −42 lines.

## Verification

The PR author reports these results. This review did not run them.

- New LibSQL and Postgres storage and HTTP scenarios fail on the
previous code and pass with this change. This includes the board-scoped
reorder and insert-ahead regression scenarios.
- Reported gates: `build:lib`, `tsc --noEmit`, `oxlint --type-aware`,
`oxfmt`, and focused intake, boards, and dispatcher suites on LibSQL and
a Postgres 16 container.
- CLI route metadata was regenerated with no drift.
- The author reports that all gates were rerun after the follow-up
changes and passed.

## Review notes

- Behavior change: deleted rules leave position gaps. Any reader that
assumes contiguous positions starting at zero will fail. The test helper
accepts gaps. Other readers were not checked in this review.
- Review severity counts: not available.
<!-- end of auto-generated comment: release notes by coderabbit.ai -->

---------

Co-authored-by: mastracode <284800079+mastra-platform[bot]@users.noreply.github.com>
This commit is contained in:
Ward Peeters 2026-11-17 01:45:21 +01:00 • committed by GitHub
commit 2d53b0462d
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
5 changed files with 173 additions and 42 deletions