Skip to content

fix(repository): unique models check delete conditions on missing items, and stale deletes and updates read again - #110

Merged
kalaspuff merged 9 commits into
mainfrom
fctl/59-https-github-com-kalaspuff-dynamoxy-issu
Oct 31, 2026
Merged

kalaspuff merged 9 commits into
mainfrom
fctl/59-https-github-com-kalaspuff-dynamoxy-issu

Conversation

@fctl-sh

@fctl-sh fctl-sh Bot commented Oct 30, 2026 •

Copy link
Copy Markdown
Contributor

What and why

Two gaps in deletes and updates of models with unique attributes.

Conditional deletes of missing items. Such a delete reads the stored item for its guards, and when none was stored it returned None without sending the caller's condition; in a transaction the step was dropped, so tx.delete(User.key(...), model=User, condition=...) used as a guard let the rest of the transaction commit. Without unique attributes, DynamoDB checks the condition on the missing item. Such a delete now sends a Delete that requires the caller's condition and that no item is stored, so it fails (ConditionalCheckFailedError, or a canceled transaction) or holds as it does for other models. A delete of a missing item without a condition still sends nothing, but is now finalized like other models' deletes: after_delete hooks run and the instance is unloaded.

Stale instances of unversioned models. delete() and update(..., Profile.handle.set(...)) of a loaded instance raised an unexplained ConditionalCheckFailedError ("the condition of delete failed", with no condition given) when another writer had changed a unique value since the load, and delete() failed the same way when another writer had deleted the item, where other models return None. Like put(), they now read the stored item and plan again, once. Transactions cannot read again, and upserts are not planned again, so their error says to refresh() the instance and retry. Also, delete() of an instance with no unique values set, whose item another writer deleted, returned the item as loaded; it now returns None.

Verified with new integration tests on the in-memory backend and MiniStack, which fail without the fix: conditional deletes of missing items alone and in transactions, an item stored between the read and the delete (a middleware writes it right after the GetItem), stale deletes and updates with their hooks counted, stale upserts, and the transaction messages. make check passes.

Fixes #81

Checklist

  • Tests cover the change (in-memory backend and MiniStack)
  • Docs updated (constraints and writing guides, architecture, AGENT_GUIDE.md, changelog)
  • make check passes locally (lint, types, clippy, consistency, Rust and Python tests)
  • Breaking change? No

Worth confirming

  • Decision: when another writer stores the item between a conditional delete's read and its write, delete() reads again and deletes the item with its guards; a transaction is canceled ("another writer stored the item after it was read: retry") rather than leaving guards without their item.
  • Decision: a delete of a missing item without a condition now runs after_delete hooks and leaves the instance unloaded and bound, as for models without unique attributes. Before, only before_delete ran.
  • Decision: stale upserts (update(..., upsert=True) of a loaded instance) are not planned again: they raise ConditionalCheckFailedError with the refresh() hint, alone and in transactions. If another writer had deleted the item, planning again would create it without the unique values the upsert's actions do not set.
  • Decision: in a transaction, a stale delete of an item that another writer deleted still cancels the transaction (with the refresh() hint), where a model without unique attributes would commit. Committing instead would need a read at plan time.

fctl session


Note

Medium risk. It changes how deletes and updates of models with unique attributes are planned, retried and checked inside transactions, so conditions, hooks and error behaviour that callers rely on shift.

What it does. Deletes of models with unique attributes now send the caller's condition (or the version check) when no item is stored, so a conditional delete of a missing item fails as it does for other models, and a transaction that uses one as a guard no longer commits. Stale delete() and update() calls on loaded unversioned instances now read the stored item and plan once more, like put(), while transactions and upserts raise ConditionalCheckFailedError with a hint to refresh() the instance and retry. A delete of a missing item without a condition still writes nothing but now runs after_delete hooks and unloads the instance.

Behaviour change. A conditional delete of a missing item in a model with unique attributes now raises ConditionalCheckFailedError or cancels the transaction where it returned None or was skipped, and deletes of missing items now run after_delete hooks.

fctl review's first look at 500c168 · 8 files, +343 −54 · Review agent settings

fctl-sh Bot and others added 2 commits October 30, 2026 23:27
…ms, and stale deletes and updates read again

Deletes of models with unique attributes read the stored item for its
guards, and returned None when there was none, without sending the
caller's condition; in a transaction the step was dropped, so a delete
used as the guard of a transaction let the rest commit. Without unique
attributes, DynamoDB checks the condition on the missing item. Such a
delete now sends the condition with "still no item stored", so it holds
or fails as for other models; when an item was stored after the read,
delete() plans again from it. A delete with nothing to delete and
nothing to check runs no request but is finalized like other models'
(after_delete hooks, the instance unloaded).

Deletes and updates of loaded instances of unversioned unique models
raised an unexplained ConditionalCheckFailedError when another writer
had changed a unique value since the instance was loaded, and delete()
failed the same way after another writer deleted the item. Like put(),
they now read the stored item and plan again once; transactions, which
cannot read again, say to refresh() the instance and retry. Upserts are
left out: one planned again after a delete would create the item without
the unique values the actions do not set.

delete() of an instance without unique values set, whose item another
writer deleted, returned the item as loaded; it returns None now.

Refs #81

Co-authored-by: Carl Oscar Aaro <[email protected]>
@fctl-review

This comment has been minimized.

fctl-sh Bot and others added 7 commits October 30, 2026 23:34
…stance

Upserts of a loaded instance whose unique values another writer changed
are not written again (planned again after a delete elsewhere, one would
create the item without the unique values its actions do not set), so
they raised the unexplained ConditionalCheckFailedError, also in
transactions. Their plan is now marked stale like other updates', so the
error says to refresh() the instance and retry; update() still does not
plan them again. The guides said every unversioned update() writes
again: they now name the exception.

Co-authored-by: Carl Oscar Aaro <[email protected]>
…com-kalaspuff-dynamoxy-issu

# Conflicts:
#	CHANGELOG.md
…com-kalaspuff-dynamoxy-issu

# Conflicts:
#	CHANGELOG.md
…com-kalaspuff-dynamoxy-issu

# Conflicts:
#	CHANGELOG.md
…com-kalaspuff-dynamoxy-issu

# Conflicts:
#	CHANGELOG.md
…com-kalaspuff-dynamoxy-issu

# Conflicts:
#	CHANGELOG.md
…com-kalaspuff-dynamoxy-issu

# Conflicts:
#	CHANGELOG.md
@kalaspuff
kalaspuff merged commit 0fe5815 into main Oct 31, 2026
22 checks passed
@kalaspuff
kalaspuff deleted the fctl/59-https-github-com-kalaspuff-dynamoxy-issu branch October 31, 2026 00:19
kalaspuff added a commit that referenced this pull request Oct 31, 2026
…ack, and collections refused (#129)

## What and why

Three ways unique guards got out of step with their items (#82):

- **Upserts that race with creates:** an upsert that set a unique value
read the item and, finding none, wrote without requiring that it was
still missing (`put()` already adds `partition.not_exists()` in this
case). When another writer created the item between the read and the
write, the upsert overwrote its unique value and never deleted the other
writer's guard, which stayed taken forever. The upsert now requires that
the item is still missing. When that fails because an item appeared, it
reads the item again and writes once more, freeing the replaced value's
guard. This is the replan that conditional deletes got in #110. In a
transaction it cancels with a `ConditionalCheckFailedError` that says
another writer stored the item and to retry. Upserts of stale loaded
instances still raise as before. The same condition covers upserts that
read the item to render a derived attribute (`Player.score.add(1)` with
`rank="SCORE#{score:08d}"`): they wrote a rank rendered from the missing
item over the created one (score 6, rank `SCORE#00000001`), and now
render it from the created item.
- **Unique floats could not be written back:** `_plan_guards` compared
the stored `Decimal("0.1")` with the float `0.1` using `==`, so the plan
deleted and put the same guard item, which DynamoDB refuses. `save()`,
`put()` and `update(Place.latitude.set(0.1))` of an instance read back
failed with the value unchanged. Guards now compare stored identities,
as keys already do.
- **Collections were accepted as unique:** the guard key is the value's
text, and a set's text depends on the process's hash seed. `unique=True`
on a set, list, map or document field is now a `TypeError` when the
model's schema is built, and a collection stored in a unique `Any` field
raises `ValueError` before any write.

Verified with integration tests on the in-memory backend and MiniStack,
each failing without the fix: a middleware that creates the item right
after the upsert's `GetItem` (for a unique value, standalone and in a
transaction, and for a derived rank), a float field saved, put and
updated after a read, and the refused collection fields and values.
`make check` passes.

Fixes #82

## Breaking change

`unique=True` on a set, list, map or document field now raises
`TypeError` when the model is used. Such guards never had one text per
value, so they did not guarantee uniqueness. Drop `unique=True` from the
field, or keep a canonical string of the value
(`",".join(sorted(tags))`) in a unique field of its own.

## Checklist

- [x] Tests cover the change (in-memory backend and MiniStack)
- [x] Docs updated (constraints guide, AGENT_GUIDE, changelog)
- [x] `make check` passes locally (lint, types, clippy, consistency,
tests)
- [x] Breaking change? Marked with `!` in the title and explained above

## Worth confirming

- **Decision:** collections are refused rather than given a canonical
text, as the issue offered both. The refusal is a `TypeError` for
declared collection types and a `ValueError` for collection values of
`Any` fields; booleans stay allowed.
- **Decision:** an upsert that loses the race to a create is planned
again from the created item, so it updates that item and replaces its
unique value, instead of failing.

[fctl session](https://app.fctl.sh/s/ses_01M67V41FM3GB8WN7TS9ZMAKDD)

<!-- FCTL_REVIEW_SUMMARY -->
---

> [!NOTE]
> **Medium risk.** It changes how upserts and writes of models with
unique fields plan their conditions and guard items, which every such
write goes through, and it makes models that declare unique collection
fields fail where they were accepted before.
>
> **What it does.** Fixes three ways unique guard items could get out of
step with their items. An upsert that finds no item now requires that
the item is still missing, and when another writer created it in the
meantime, it reads the item again and writes once more, so the replaced
value's guard is freed and derived keys such as `rank` are rendered from
the created item. Guards of unique floats are compared as stored values,
so a float read back no longer deletes and puts the same guard item.
Unique set, list and map fields are refused with a `TypeError` when the
schema is built, and collection values in unique `Any` fields raise a
`ValueError` before any write.
>
> **Breaking change.** Models that declare `unique=True` on a set, list,
map or document field now raise a `TypeError` as soon as their schema is
used, so applications with such models stop working until they remove
the option or keep a canonical string in a unique field of its own.
>
> <sub>[fctl
review](https://app.fctl.sh/s/ses_01M67XAJZV10F0QA39N1RB4D8K)'s first
look at
[`3ba79de`](3ba79de)
· 8 files, +185 −12 · [Review agent
settings](https://app.fctl.sh/p/dynamoxy?tab=reviews)</sub>

<!-- /FCTL_REVIEW_SUMMARY -->

---------

Co-authored-by: fctl-sh[bot] <fctl-sh[bot]@users.noreply.github.com>
Co-authored-by: Carl Oscar Aaro <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Unique attributes: conditional deletes of missing items skip the condition, and stale instances fail unexplained

1 participant