Separating a document swap from an in-place edit makes the failure surface completely clear. Mutating the text field in place while keeping the same document ID leaves an ID assertion totally blind, which is why taking a deep-copied MappingProxyType snapshot right at the retrieval boundary closes a hole that post-hoc ID matching cannot see.
One practical Python wrinkle with MappingProxyType is that it is shallow on nested containers: if a retrieved document carries nested metadata dicts or chunk lists, freezing those inner structures recursively (or serializing into frozen dataclasses/tuples) ensures downstream formatting helpers cannot mutate nested fields either.
The shallow bit is the one that would've bitten me — freezing the top level and calling it done, then some formatting helper mutates a nested metadata dict and nothing complains. I'm going to run that one, shallow freeze against recursive, and see whether the nested mutation actually survives the snapshot in practice. Do you freeze recursively in your own code, or go the frozen-dataclass route?
Ahmet Özel
Separating a document swap from an in-place edit makes the failure surface completely clear. Mutating the text field in place while keeping the same document ID leaves an ID assertion totally blind, which is why taking a deep-copied MappingProxyType snapshot right at the retrieval boundary closes a hole that post-hoc ID matching cannot see.
One practical Python wrinkle with MappingProxyType is that it is shallow on nested containers: if a retrieved document carries nested metadata dicts or chunk lists, freezing those inner structures recursively (or serializing into frozen dataclasses/tuples) ensures downstream formatting helpers cannot mutate nested fields either.