Skip to content

fix(repository): writes through a loaded instance never reach another item - #61

Merged
kalaspuff merged 2 commits into
mainfrom
fixes-stack/01-instance-keys
Oct 30, 2026
Merged

kalaspuff merged 2 commits into
mainfrom
fixes-stack/01-instance-keys

Conversation

@fctl-sh

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

Copy link
Copy Markdown
Contributor

What and why

Two ways of writing through a loaded instance changed an item other than the one it was loaded from.

  • A changed key field. update(), delete(), transactions (tx.update, tx.delete) and batch deletes of a loaded instance used the key its fields render now, where save(), put() and batch puts already raise ValueError for a changed key. After order.customer_id = "c2", order.update(Order.total.set(0)) updated customer c2's order with the same order id, and order.delete() deleted it; a versioned model was no safer when both items had the same version. These now raise the same ValueError before any I/O. refresh() reloads the item the instance was loaded from, so it undoes such a change instead of loading another item into the instance.
  • An update of a field that a key template uses. update(Customer.customer_id.set("c2")) with pk="CUSTOMER#{customer_id}" was accepted, on instances, by key and in transactions. The item kept pk=CUSTOMER#c1 but held customer_id=c2, so every decode of it rendered the key of c2, and the instance's later delete() deleted c2's item. Such fields are part of the key, so updates now refuse them with a ValueError, as they refuse the key attributes themselves. The guides already described these fields as immutable on loaded instances.

Verified with a new integration test on the in-memory backend and MiniStack that covers each of these writes, and with the whole suite, ruff, pyrefly and the consistency checks.

Checklist

  • Tests cover the change (in-memory backend and MiniStack)
  • Docs updated (writing and constraints guides, AGENT_GUIDE.md, changelog)
  • make check passes locally (lint, types, consistency, tests; no Rust change)
  • Breaking change? No: only writes that corrupted data are refused

Worth confirming

  • Decision: refresh() of a loaded instance whose key field changed reloads the item it was loaded from (discarding the change, like any other local change), rather than raising like the writes do.
  • Decision: updates by key (users.update(User.key(...), ...)) refuse fields that a key template uses too, not only updates of instances, since they leave the stored item just as inconsistent.

fctl session


Note

Medium risk. It makes update(), delete(), transactions and batch deletes raise ValueError in cases they used to accept, so code that changed a key field of a loaded instance, or updated a field a key template uses, now fails before any I/O. The check sits in the shared _target_key helper that every write path goes through.

What it does. Writes through a loaded instance used the key its fields render now, so after order.customer_id = "c2" an update() or delete() hit another customer's order. Those writes now compare the key with the one the instance was loaded with and raise the same ValueError as save() and put(), and refresh() reloads the item the instance came from. Updates of a field that a key template uses are refused too, on instances, by key and in transactions, since they left an item whose key and fields disagreed.

Stricter updates. update() actions on a field that a key template uses (for example Customer.customer_id.set("b") with pk="CUSTOMER#{customer_id}") now raise ValueError, also for updates by key and upserts, where they used to be accepted.

fctl review's first look at b359228 · 8 files, +113 −24 · Review agent settings

… item

update(), delete(), transactions and batch deletes of a loaded instance
used the key its fields render now. After `order.customer_id = "c2"`,
`order.update(...)` updated another customer's order and
`order.delete()` deleted it, while save() and put() refused the changed
key. They now raise the same ValueError before any I/O, and refresh()
reloads the item the instance was loaded from.

Updates of a field that a key template uses (`customer_id` in
`pk="CUSTOMER#{customer_id}"`) were accepted. The item then kept its
key but held another customer_id, so every decode of it rendered
another key, and the instance's later writes went to that item. Such
updates now raise ValueError, like updates of the key itself.

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

This comment has been minimized.

Comment thread python/dynamoxy/repository.py Resolved
@kalaspuff
kalaspuff merged commit c192597 into main Oct 30, 2026
22 checks passed
@kalaspuff
kalaspuff deleted the fixes-stack/01-instance-keys branch October 30, 2026 21:03
This was referenced Oct 30, 2026
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.

1 participant