Skip to content

fix(versions)!: version() fields hold a random token, so a stale instance never matches again - #150

Merged
kalaspuff merged 5 commits into
mainfrom
parity-stack/4-version-tokens
Oct 31, 2026
Merged

kalaspuff merged 5 commits into
mainfrom
parity-stack/4-version-tokens

Conversation

@fctl-sh

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

Copy link
Copy Markdown
Contributor

Summary

A version() field held a counter, which started again at 1 on a put() of an instance that was not loaded and on creating an item again after it was deleted, so an instance loaded earlier at version 1 passed its check again and overwrote both writes. As Carl Oscar decided on #84, versions are now random tokens: 16 hex digits from the operating system's random source, new on every write, so such an instance raises VersionConflictError (ADR 0022). Updates set the new token instead of adding 1, so templates that render the version (revision = "v{version}") need no read of the item. The operations of a transaction with an idempotency token derive their tokens from the token and their position, so the same transaction planned again with the token is the same request; random tokens broke the retry in examples/transactions.py with IdempotentParameterMismatch.

Verified with make check (both backends, the boto3 interop tests, the examples and the smoke test) and new tests: a stale instance fails after a put() of an instance that was not loaded and after delete-and-create, Attr[int] version fields are refused, and a transaction with a token planned twice applies once.

Breaking change

version() fields must be declared Attr[str] (Annotated[str | None, Version()] in pydantic models); other types raise TypeError when the model is used. Stored numeric versions are not read (ItemDecodeError), and versions compare for equality only: keep a counter field to count writes.

Worth confirming

  • Decision: tokens are 64 bits as 16 hex digits, like the write ids of ADR 0016.
  • Decision: items stored with numeric versions are not migrated or read, since dynamoxy has no users yet.
  • Decision: with an idempotency token, version tokens derive from the token and the operation's position in the transaction, so reusing a token for another transaction after DynamoDB's 10-minute window repeats them; tokens are meant to be unique per operation.

Fixes #84

fctl session


Warning

High risk. It changes the stored type of every version() field from a number to a string token, so items already stored with numeric versions stop loading, and it reworks how optimistic locking, updates and idempotent transactions plan their writes.

What it does. Makes version() fields hold a random 16-hex-digit token that is new on every write instead of a counter, so an instance loaded before the item was overwritten, or deleted and created again, can no longer match the stored version and raises VersionConflictError (fixes #84). Updates now SET the new token rather than ADD 1, and the operations of a transaction with an idempotency token derive their tokens from the token and their position, so a retried transaction sends the same request. Docs, ADR 0022, examples and tests are updated, including tests that refused field types fail and that a stored numeric version raises ItemDecodeError.

Breaking stored format. version: Attr[int] fields now raise TypeError when the model is used, and items already stored with numeric versions raise ItemDecodeError when read; there is no migration.

fctl review's first look at f152197 · 37 files, +335 −116 · Review agent settings

@fctl-review

This comment has been minimized.

Comment thread python/dynamoxy/repository.py Resolved
@fctl-review

This comment has been minimized.

@fctl-sh

fctl-sh Bot commented Oct 31, 2026 •

Copy link
Copy Markdown
Contributor Author

Stack of 8 pull requests, merged in this order with "Squash and merge":

  1. docs(parity): keep DynamoDB's measured answers and the probes that ask them #147 docs(parity): keep DynamoDB's measured answers and the probes that ask them (merged)
  2. fix(indexes): reads refuse filters and projections of attributes an index does not hold #148 fix(indexes): reads refuse filters and projections of attributes an index does not hold (merged)
  3. fix(tables): the in-memory backend refuses table changes DynamoDB refuses #149 fix(tables): the in-memory backend refuses table changes DynamoDB refuses
  4. fix(versions)!: version() fields hold a random token, so a stale instance never matches again #150 fix(versions)!: version() fields hold a random token, so a stale instance never matches again ← this pull request
  5. fix(expressions): the in-memory backend refuses placeholders, names and operands DynamoDB refuses #151 fix(expressions): the in-memory backend refuses placeholders, names and operands DynamoDB refuses
  6. fix(limits): local index entries count toward the 400 KB of an item, and transactions keep to 4 MB #152 fix(limits): local index entries count toward the 400 KB of an item, and transactions keep to 4 MB
  7. fix(errors): LocalStack's DuplicateItem code raises DuplicateItemError, and the e2e tests pass on LocalStack #153 fix(errors): LocalStack's DuplicateItem code raises DuplicateItemError, and the e2e tests pass on LocalStack
  8. test: probe dynamoxy itself on DynamoDB, MiniStack, LocalStack and the in-memory backend #154 test: probe dynamoxy itself on DynamoDB, MiniStack, LocalStack and the in-memory backend

@fctl-review

This comment has been minimized.

@fctl-sh

fctl-sh Bot commented Oct 31, 2026

Copy link
Copy Markdown
Contributor Author

Fixed in a1f9508: the older [Unreleased] entry about items stored before the model had a version says save() stores its first version, which is a token now.

@kalaspuff
kalaspuff added this pull request to stack #155 October 31, 2026 18:18
Base automatically changed from parity-stack/3-table-updates to main October 31, 2026 18:28
fctl-sh Bot and others added 5 commits October 31, 2026 19:28
…ance never matches again

A counter started again at 1 on a put() of an instance that was not
loaded and on delete-and-create, so an instance loaded before passed its
version check again and overwrote both writes (#84). Versions are now
random tokens (Attr[str]), set on every write; transactions with an
idempotency token derive theirs from it, so a retry is the same request
(ADR 0022).

BREAKING CHANGE: version() fields must be declared Attr[str]
(Annotated[str | None, Version()] in pydantic models); stored numeric
versions are not read.

Co-authored-by: Carl Oscar Aaro <[email protected]>
@kalaspuff
kalaspuff force-pushed the parity-stack/4-version-tokens branch from f152197 to e981f72 Compare October 31, 2026 18:28
@kalaspuff
kalaspuff merged commit e9c5a15 into main Oct 31, 2026
19 checks passed
@kalaspuff
kalaspuff deleted the parity-stack/4-version-tokens branch October 31, 2026 18:28
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.

Instances that were not loaded: put() resets the version, and updates render derived attributes from them

1 participant