Computed properties ignore explicit dependencies when dynamic dependencies are also present
Describe the bug Computed properties do not correctly recompute when they combine explicitly registered dependencies with dynamically tracked dependencies.
When a computed property declares an explicit dependency using dependencies and also accesses another observable property dynamically, changes to the explicitly declared dependency may not trigger recomputation correctly.
The computed property is marked dirty when the explicit dependency changes, but the recomputation logic appears to rely on dynamically recorded parent dependencies when checking whether a recalculation is necessary.
As a result, the computed property can return a stale cached value instead of reflecting the updated observable state.
Expected behavior A computed property should correctly track and respond to both explicitly declared and dynamically accessed dependencies.
Whenever either dependency changes, the computed property should recompute and return the updated result.
To Reproduce
def test_explicit_dependency_recomputes_with_dynamic_dependency():
class Model(HasEmitters):
a = Observable()
b = Observable()
@computed_property(
dependencies=[("a", ObservableSignals.CHANGED)]
)
def total(self):
return self.peek("a") + self.b
model = Model()
model.a = 1
model.b = 2
assert model.total == 3
model.a = 2
assert model.total == 4
Actual result: Error - assert 3 == 4 Expected result: 4
Additional context The regression test was executed against the current implementation. Test results:
FAILED tests/experimental/test_mesa_signals.py::test_explicit_dependency_recomputes_with_dynamic_dependency - assert 3 == 4
1 failed, 86 passed in 1.70s
The suspected issue is in the interaction between explicit dependency registration and dynamically tracked parent dependencies in the computed-property invalidation and recomputation logic.
Relevant implementation areas:
- ComputedState dependency tracking.
- Computed-property wrapper and its recomputation logic.
- Explicit dependency handling and dynamic parent comparison.
A regression test covering the combination of explicit and dynamic dependencies should help prevent this behavior from recurring.
Hi @sangitabera ,
I looked into the ComputedState and computed_property implementation, and the issue seems to originate from the interaction between explicit dependency invalidation and dynamic parent comparison.
_set_dirty() marks the state as dirty when an explicit dependency changes, but the recomputation logic subsequently checks only the dynamically recorded state.parents before calling evaluate().
In the provided example, peek("a") bypasses dynamic tracking, while self.b is recorded as a dynamic dependency. This could explain why a change to a marks the state dirty but still results in the cached value being returned.
One possible direction is to distinguish invalidation from explicit dependencies from dynamic dependency changes, ensuring that an explicit dependency signal triggers recomputation without unnecessarily changing the existing dynamic dependency checks.
I'd like to investigate this further, add regression tests, and work on a fix.
Would it be okay if I picked this issue up?
Thanks!
Hi @samanvay-kumar , Thank you so much for taking the time to look into this and share such a detailed analysis—your breakdown of _set_dirty() and state.parents is spot on.
I actually already have the fix and regression tests ready on my end, and I'm about to open a PR for it shortly.
Thanks for the update @sangitabera ! I was looking forward to working on a fix for this issue, but no worries since you already have one ready.
Once your PR is up, I'd be happy to take a look, review the changes, and help test the fix.
I'm also interested in contributing to Mesa and would really appreciate it if you could point me towards any other unresolved issues where I could potentially help. I'd be happy to explore them and contribute wherever I can!