Skip to content

fix(signals): preserve cycle tracking across unrelated observable not… - #4237

Merged
quaquel merged 1 commit into
mesa:mainfrom
savinduhasintha2005:fix/4234-preserve-cycle-tracking
Nov 25, 2026
Merged

quaquel merged 1 commit into
mesa:mainfrom
savinduhasintha2005:fix/4234-preserve-cycle-tracking

Conversation

@savinduhasintha2005

Copy link
Copy Markdown
Contributor

Description

This PR fixes an issue in the mesa_signals cycle-tracking mechanism where unrelated observable notifications could prematurely clear the active dependency-tracking state during computed-property evaluation.

Previously, PROCESSING_SIGNALS.clear() was called inside Observable.__set__ whenever a changed notification was emitted. If an unrelated observable was modified during a computed-property evaluation, this cleared the active tracking information and prevented the cycle detector from identifying cyclical dependencies.

Changes

  1. mesa/experimental/mesa_signals/core.py

    • Removed PROCESSING_SIGNALS.clear() from Observable.__set__.
    • Scoped the clearing of PROCESSING_SIGNALS to the completion of the outermost ComputedState.evaluate().
    • Preserved active dependency-tracking information across unrelated observable notifications.
  2. tests/experimental/test_mesa_signals.py

    • Added a regression test covering unrelated observable notifications during computed-property evaluation.
    • Verified that cyclical dependencies correctly raise ValueError("cyclical dependency").

Testing

  • ruff check
  • ruff format --check
  • pytest tests/experimental/test_mesa_signals.py -v
  • pytest tests/experimental/test_states.py -v

Related Issue

Fixes #4234

@github-actions github-actions Bot added experimental Release notes label testing Release notes label labels Nov 25, 2026
@github-actions

Copy link
Copy Markdown

Performance benchmarks:

Model Size Init time [95% CI] Run time [95% CI]
BoltzmannWealth small 🔵 -3.0% [-4.8%, -1.0%] 🔵 +1.1% [+0.9%, +1.3%]
BoltzmannWealth large 🔵 -0.9% [-1.3%, -0.5%] 🔵 -0.5% [-1.0%, +0.0%]
Schelling small 🔵 +0.1% [-0.4%, +0.7%] 🔵 -0.3% [-1.1%, +0.5%]
Schelling large 🔵 +2.3% [+1.7%, +3.0%] 🔴 +8.3% [+7.3%, +9.3%]
WolfSheep small 🔵 +1.4% [+1.1%, +1.8%] 🔵 -0.6% [-2.4%, +1.2%]
WolfSheep large 🔵 -0.2% [-0.6%, +0.2%] 🔵 +2.2% [+1.0%, +3.5%]
SugarscapeG1mt small 🔴 +3.7% [+3.2%, +4.2%] 🔵 +0.3% [-1.7%, +2.2%]
SugarscapeG1mt large 🔵 +4.6% [+2.6%, +6.6%] 🔵 -0.4% [-1.6%, +0.8%]
BoidFlockers small 🔴 +5.0% [+3.9%, +6.1%] 🔵 +1.0% [+0.6%, +1.4%]
BoidFlockers large 🔵 -1.3% [-2.3%, -0.4%] 🔵 -0.2% [-0.4%, -0.0%]
MultiLevelAllianceModel small 🟢 -15.0% [-17.7%, -12.2%] 🔵 -0.9% [-1.3%, -0.6%]
MultiLevelAllianceModel large 🟢 -5.6% [-7.1%, -4.5%] 🔵 -0.8% [-1.2%, -0.4%]

@sangitabera

Copy link
Copy Markdown
Contributor

Hi @savinduhasintha2005 , I originally reported this issue in #4234 and have been actively working on it. You shouldn't take over and open a PR for an issue reported by someone else without asking or checking in on the thread first. I am submitting my PR now—please close this duplicate so we don't overlap on the same task. Thanks.

Comment on lines +273 to +274
if old_computed is None:
PROCESSING_SIGNALS.clear()

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

old_computed is None doesn't always mean this is the outermost evaluation: peek() also sets CURRENT_COMPUTED to None. If a computed peeks at another computed property, that inner evaluation clears the set and wipes the outer computed's tracking, so this cycle is no longer caught, while main raises for it:

@computed_property
def result(self):
    value = self.a
    self.peek("helper")   # helper is another computed property
    self.a = value + 1    # writes what it read: a cycle
    return value

Counting how deeply evaluate() is nested and clearing only when it returns to 0 fixes this. The case from #4234 still raises with that change, and the signals and states tests pass.

@codebreaker32 codebreaker32 left a comment •

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Posted on the wrong PR by mistake, please disregard this approval.

@quaquel
quaquel merged commit ddd573f into mesa:main Nov 25, 2026
17 of 18 checks passed
@codebreaker32

Copy link
Copy Markdown
Collaborator

@quaquel Sorry for the noise, my approval here was meant for #4164 and posted here by mistake.

Did you get a chance to look at my inline comment below? With this merged, a computed that peek()s at another computed property and then writes what it read is no longer caught as a cycle, while it was before.

@quaquel

quaquel commented Nov 25, 2026

Copy link
Copy Markdown
Member

I opened a revert PR, which hopefully will be approved asap. I did not check but just trusted your judgement. No worries about the mistake.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

experimental Release notes label testing Release notes label

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Unrelated observable notifications clear active cycle tracking and hide cyclical dependencies

4 participants