fix: only record an uncached var as sent once its delta is delivered

Merged
MA
24d ago
+407-77

All Submissions:

  • Have you followed the guidelines stated in CONTRIBUTING.md file?
  • Have you checked to ensure there aren't any other open Pull Requests for the desired changed?

Type of change

  • Bug fix (non-breaking change which fixes an issue)

Changes To Core Features:

  • Have you added an explanation of what your changes do and why you'd like us to include them?
  • Have you written new tests for your core changes, as applicable?
  • Have you successfully ran tests with your changes locally?

Summary

closes #7212

#6946 dedupes unchanged @rx.var(cache=False) values by remembering the last value sent per client. The memo was written while the delta was built (BaseState.get_delta → ComputedVar._record_delta_value), so a downstream get_delta override that withholds an entry — the pattern the get_delta docstring documents, and what reflex-enterprise 0.9.5's auth filtering does — left the memo claiming the client had a value it never received. Once the filter stopped withholding, the identical value was deduped away and the client stayed stale until the value changed again. Regression vs 0.9.11.post1.

What changed

  • get_delta gathers candidate _DeltaRecords (state, key, value, instance, memo attribute, stored key) into a ContextVar while building the delta; ComputedVar._record_delta_value became _pending_delta_record and no longer writes the memo.
  • _get_resolved_delta — the single chokepoint every delivery path uses (app.py, istate/proxy.py, reflex-base base_state_processor.py, istate/shared.py), and the one place that sees the delta after the override chain with coroutines resolved — commits a record only where delta[state][key] is value, i.e. the very object that was computed survived. Dropped or replaced entries are owed to the client again.
  • _suppress_delta_recording() is still honoured, and a suppressed traversal nested inside a recording one records nothing.
  • Behaviour change to be aware of: a bare get_delta() call no longer dedupes (only delivered deltas record). Ten existing #6946 unit tests moved onto await state._get_resolved_delta() with their assertions unchanged.

Tests

  • New: test_uncached_var_withheld_by_delta_override_is_resent (dropped / replaced-with-placeholder × sync / async) and test_uncached_computed_var_recorded_only_once_delivered. The sync cases and the latter fail on main and pass here; the async cases document that a filter closes the coroutine before it can reach the memo.
  • Mutation checks: making _delta_value_key return a bare value, or a constant instead of _UNKEYABLE_VALUE, fails the corresponding #6946 tests again (a first version of this change had left two of them on the non-recording path; fixed during review).
  • uv run pyright reflex tests: 0 errors. ruff / format clean. uv run pytest tests/units --ignore=tests/units/reflex_cli: 9137 passed together with the other three campaign fixes.

Verification against the real breakage

  • Browser-free probe from the pre-release campaign: on the published 0.9.12a1 the withheld var is never re-sent (steps 3/4 False); on this branch it is delivered (True), matching 0.9.11.post1.
  • Playwright repro (elapp page /filtered), dev in-memory and dev + redis: the published alpha shows the stale secret-0 after the filter is lifted and secret-2 after the next round; this branch shows secret-1 and secret-3, with zero console/page/network errors. #6946's savings are intact: the campaign's /uncached probe sends the same 24 delta frames with the same keys (15.3 KB inbound vs 26.1 KB on 0.9.11.post1).

Notes for review

  • The commit still happens before emit_update, so a delta the socket never delivers is recorded as sent (pre-existing; a reconnect re-hydrates via dict()). Moving it past the emit is a separate decision.
  • A downstream package that filters after _get_resolved_delta would still get values recorded; reflex-enterprise 0.9.5 wraps only get_delta and dict (verified from the wheel), passing surviving values through by reference.
  • Survival is tested by identity; a filter that returns a copy of a value loses the dedupe for that var (chattier, never stale).
  • Withholding an async uncached var leaves the inner coroutine unawaited (RuntimeWarning; pre-existing, reproduced on main, filtered in the new test).
  • Independent adversarial review blocked the first version (two pyright errors, two vacuous tests); both fixed in the amended commit. Report with the review and follow-up: fixes/f003/REPORT.md.

🤖 Generated with Claude Code

https://claude.ai/code/session_01NeyxWsuC9hqSKq8YcEwZyY


Generated by Claude Code