# State Guidelines This module owns materialized document state, container stores, diff application, checkout/replay behavior, deep/shallow values, and mergeable container visibility. Read [../../../../context/mergeable-containers.md](../../../../context/mergeable-containers.md) before changing mergeable child behavior. ## Local Entry Points - `value_decode.rs`: iterative postcard map/list value reader. Its layout, destruction and WASM conversion contracts are in [../../../../context/deep-value-stack-safety.md](../../../../context/deep-value-stack-safety.md). - `../state.rs`: `DocState`, checkout/path/deep-value traversal, state replay, lifecycle, and alive-container discovery. - `container_store/`: persisted KV-backed container snapshots and `ContainerWrapper` encoding. The decoded-value cache in `InnerStore` is bounded and evicted wrappers must stay re-creatable from KV; read [../../../../context/container-value-cache.md](../../../../context/container-value-cache.md) before changing read/caching paths there. - `map_state.rs`, `list_state.rs`, `richtext_state.rs`, `tree_state.rs`, `movable_list_state.rs`, `counter_state.rs`: per-container state and snapshot codecs. `richtext_state.rs` also hosts `redact_dead_style_values`, used by shallow-snapshot export; read [../../../../context/shallow-snapshot-style-redaction.md](../../../../context/shallow-snapshot-style-redaction.md) before changing the richtext snapshot codec or that pass. `tree_state.rs` builds tree events two ways depending on the diff mode: [../../../../context/tree-events.md](../../../../context/tree-events.md). Its depth-independent traversals, snapshot order, and nested result disposal: [../../../../context/tree-depth.md](../../../../context/tree-depth.md). `movable_list_state.rs` import validation and the Checkout-mode fallback for moves of deleted elements, plus the List/Text `validate_diff` bounds checks: [../../../../context/movable-list-op-validation.md](../../../../context/movable-list-op-validation.md). - `mergeable.rs`: logical child edge resolution for mergeable containers. - `dead_containers_cache.rs`: dead/alive tracking. Map/list deletions are final; tree and movable-list ones are dropped whenever a move could revive them; mergeable ones are never cached: [../../../../context/dead-container-cache.md](../../../../context/dead-container-cache.md). A failed import keeps its arena indices, so store entries and caches keyed by `ContainerIdx` stay valid; it clears this cache: [../../../../context/failed-import-arena-indices.md](../../../../context/failed-import-arena-indices.md). - `unknown_state.rs` and `../diff_calc/unknown.rs`: forward compatibility for unknown container types. They are never created locally; the `apply_diff` pre-check and undo rules are in [../../../../context/unknown-containers.md](../../../../context/unknown-containers.md). ## Mergeable Rules - `MapHandler::ensure_mergeable_*` writes a compact marker into the parent map and returns a handler for a deterministic `ContainerID`. - The parent map marker, not "child has ops", decides whether a mergeable child is visible. - Non-mergeable occupants must block `ensure_mergeable_*`; same-kind marker writes are idempotent; different-kind marker writes are deliberate kind changes. - Snapshot and shallow snapshot retention must preserve hidden losing-kind mergeable state. ## Validation - `cargo test -p loro-internal --test mergeable_cid_encoding` - `cargo test -p loro-internal --test mergeable_container` - `cargo test -p loro-internal import_atomicity` if import or rollback is involved. `container_tree.rs` traverses ephemeral shallow values into a sink. Container identity comes from CRDT edges, including mergeable markers and Tree metadata; ordinary values remain opaque. See [bulk reads](../../../../context/wasm-container-tree.md).