Fix disk state manager debounced writes

Closed
HA
3mo ago
+1786-102

Fixes #6806

Summary

Fix debounced StateManagerDisk.set_state() writes so repeated updates for the same token persist the latest value instead of the first queued value.

Problem

When disk write debouncing was enabled, only the first write for a token was retained in _write_queue. A later write before the debounce flush was silently discarded, so background processing or close() could persist stale state.

Changes

  • Replace an existing queued payload with the latest state while preserving the original queue timestamp.
  • Keep the in-memory cache aligned when set_state() receives an arbitrary instance instead of the one returned by get_state().
  • Explicitly persist supplied BaseState instances, including untouched instances, so the cache never points to a state that was not written to disk.
  • Preserve the existing touched-state optimization for the normal same-instance path.

Testing

  • uv run pytest tests/units/test_state.py::test_state_manager_disk_debounced_set_state_flushes_latest_non_base_state -q
  • uv run pytest tests/units/test_state.py::test_state_manager_disk_set_state_updates_cache_for_arbitrary_instance -q
  • uv run pytest tests/units/test_state.py::test_state_manager_disk_set_state_persists_untouched_base_state -q
  • uv run ruff check reflex/istate/manager/disk.py tests/units/test_state.py
  • uv run ruff format --check reflex/istate/manager/disk.py tests/units/test_state.py
  • git diff --check