Skip to content

Track mutations made through dict.values() and dict.items() on state vars - #7500

Merged
FarhanAliRaza merged 2 commits into
mainfrom
claude/pensive-hawking-e8qxi1
Oct 23, 2026
Merged

FarhanAliRaza merged 2 commits into
mainfrom
claude/pensive-hawking-e8qxi1

Conversation

@masenf

@masenf masenf commented Oct 7, 2026 •

Copy link
Copy Markdown
Collaborator

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?

Description

closes #7477

MutableProxy only re-wrapped nested mutables reached through __getitem__, __iter__, get() and setdefault(). The native dict.values() / dict.items() views handed out the nested dicts unwrapped, so a loop like

for item in self.inventory.values():
    item["stock"] -= 1

changed the backend value without marking the var dirty: the rendered value and any cached var over it stayed stale. The gap dates to the MutableProxy rewrite in #1748 (v0.2.8); before that, the eager ReflexDict conversion did mark the var dirty but rebuilt the container on each write, so only the first mutation in such a loop landed.

Fix. A proxied dict now serves values() and items() as collections.abc views that read each value back through the proxy. Mutable values come out wrapped under their key, so they mark the field dirty and refresh by that key in an async with context. The views keep native dict-view semantics: live, sized, reversible, searchable, usable in set operations, and isinstance of ValuesView / ItemsView. Pure-Python mappings already worked because their Mapping mixin methods get rebound to the proxy; this closes the gap for the C-level dict methods, guarded so a non-dict's values / items attribute is left alone.

Both views iterate through one shared generator that wraps straight from the wrapped dict, so they run at the existing per-element proxy fast-path cost rather than the ABC default of proxy[key] per element:

Path (10k-entry dict, per element) scalar values nested dicts
native view, before 19 ns 19 ns
proxied view, after 54 ns ~1.5 µs (proxy construction, same as d[k])

Tests. Three new tests in tests/units/istate/test_proxy.py fail before the fix and pass after it: dirty marking through both views (plus the live / reversed / membership semantics), the issue's cached-var scenario, and key-path refresh for proxies read through views in an async context.


🤖 Generated with Claude Code

https://claude.ai/code/session_01MzM34QeM3FSHoTgvC7hs1x


Generated by Claude Code

View guided diff Turn on auto-fix

…vars

MutableProxy only re-wrapped nested mutables reached through __getitem__,
__iter__, get() and setdefault(). The native dict views handed out the
nested dicts unwrapped, so mutating them in a `for item in d.values()`
loop changed the backend value without marking the var dirty: the
rendered value and any cached var over it went stale. The gap dates to
the MutableProxy rewrite in #1748 (v0.2.8).

A proxied dict now serves `values()` and `items()` as collections.abc
views that read each value back through the proxy, wrapping mutable
values under their key so they mark the field dirty and refresh by that
key in an async context. The views keep dict-view semantics: live, sized,
reversible, searchable, and `isinstance` of ValuesView/ItemsView. Both
views iterate through one shared generator that wraps straight from the
wrapped dict, at the cost of the existing per-element proxy fast path.

Fixes #7477

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01MzM34QeM3FSHoTgvC7hs1x
@masenf
masenf requested a review from a team as a code owner October 7, 2026 18:14

@cubic-dev-ai cubic-dev-ai Bot left a comment •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All reported issues were addressed across 3 files

Reply with feedback, questions, or to request a fix.

View guided diff | Turn on auto-fix | Re-trigger cubic

Comment thread reflex/istate/proxy.py Outdated
@greptile-apps

greptile-apps Bot commented Oct 7, 2026 •

Copy link
Copy Markdown
Contributor

RetriggerView in GreptileConfidence Score: 5/5

[Medium impact] The PR appears safe to merge, with the previous compatibility issue addressed and no new actionable issues found.

Summary

The PR makes nested writes through state dictionaries’ values() and items() mark the owning field dirty.

  • Nested values from dictionary views now update state when they change.

Reviews (2) · Last reviewed commit: "Give the proxied dict views the native m..." · Reviewed by Greptile

Comment thread reflex/istate/proxy.py Outdated
@masenf masenf added the bug Something isn't working label Oct 19, 2026 — with ChatGPT Codex Connector
@FarhanAliRaza FarhanAliRaza added the on deck PRs lined up to review / merge next label Oct 22, 2026
@codspeed

codspeed Bot commented Oct 22, 2026 •

Copy link
Copy Markdown

Merging this PR will degrade performance by 4.27%

1 warning

⚠️ 19 benchmarks are dominated by system calls

System calls cannot be simulated consistently and are left out of the measure. The reported value understates the real cost. Switch to the Walltime instrument to measure them.

Learn more about system calls

See affected benchmarks

❌ 1 regressed benchmark
✅ 147 untouched benchmarks

Warning

Please fix the performance issues or acknowledge them on CodSpeed.

Performance Changes

Benchmark BASE HEAD Efficiency
❌ test_set_state[memory] 2.9 ms 3 ms -4.27%

Tip

Investigate this regression by commenting @codspeedbot fix this regression on this PR, or directly use the CodSpeed MCP with your agent.


Comparing claude/pensive-hawking-e8qxi1 (9d67d19) with main (c43ffc6)

Open in CodSpeed

Comment thread reflex/istate/proxy.py Outdated
The views replacing `dict.values()` / `dict.items()` on a proxied dict
dropped two parts of the native view API, and carried no type
information about the dict they view.

`mapping` returns a read-only mapping proxy over the dict's proxy, so the
nested values it hands out keep tracking mutations. `repr` delegates to
the native view of the wrapped dict, printing exactly what it printed
before the views were introduced.

The view base is now generic in the dict's key and value types, with
`__iter__` and `__reversed__` overloaded on the view's own type: the
items view yields `tuple[K, V]` and the values view yields `V`. A test
pins the element types with `assert_type`, so pyright checks them in CI.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01MzM34QeM3FSHoTgvC7hs1x
@FarhanAliRaza
FarhanAliRaza merged commit d74ed89 into main Oct 23, 2026
168 of 169 checks passed
@FarhanAliRaza
FarhanAliRaza deleted the claude/pensive-hawking-e8qxi1 branch October 23, 2026 13:21
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working on deck PRs lined up to review / merge next

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Mutating nested dict entries through dict.values() / dict.items() bypasses dirty tracking: rendered values and cached vars go stale

3 participants