Skip to content

fixing Parallel Stepping Conflict#163

Open
hillhack wants to merge 1 commit intomesa:mainfrom
hillhack:jyoti_paralllel_steping
Open

fixing Parallel Stepping Conflict#163
hillhack wants to merge 1 commit intomesa:mainfrom
hillhack:jyoti_paralllel_steping

Conversation

@hillhack
Copy link
Contributor

@hillhack hillhack commented Mar 6, 2026

Fix: #162

Summary of Changes

Core Update

mesa_llm/parallel_stepping.py

  • Added logic to patch _HardKeyAgentSet to manage agents. Without patching this specific class, parallel stepping would stop working entirely in the new Mesa.
    facing : Version mismatch #164

Summary by CodeRabbit

Release Notes

  • Bug Fixes

    • Fixed an issue where Meta Agent constituent agents could be stepped multiple times during parallel stepping operations.
    • Improved handling of agents in asynchronous and threaded stepping modes.
  • Tests

    • Added comprehensive tests verifying correct behavior of Meta Agent parallel stepping across asyncio and threading modes.

@coderabbitai
Copy link
Contributor

coderabbitai bot commented Mar 6, 2026

Walkthrough

The changes implement a conflict resolution mechanism for MetaAgent parallel stepping by introducing an is_component flag to exclude constituent agents from direct parallel scheduling. The fix handles multiple stepping paths (async, sync, threading) and integrates with Mesa's _HardKeyAgentSet when available, with comprehensive test coverage validating behavior in asyncio and threading modes.

Changes

Cohort / File(s) Summary
Parallel Stepping Core
mesa_llm/parallel_stepping.py
Adds early filtering of agents with is_component=True across all parallel stepping paths (async, sync, multithreaded). Extends monkey-patching to handle Mesa's _HardKeyAgentSet and its shuffle_do implementation. Updates enable/disable mechanisms to manage enhanced shuffle_do on both AgentSet types.
Parallel Stepping Tests
tests/test_parallel_stepping.py
Introduces ConflictWorker, ConflictManager, and ConflictBusinessModel test fixtures to verify constituent agents are stepped exactly once when parallel stepping is enabled. Adds tests for both asyncio and threading modes. Includes autouse fixture for state reset and updates DummyModel to use rng parameter.

Sequence Diagram

sequenceDiagram
    participant Client as Test/Model
    participant Scheduler as Parallel Scheduler
    participant MetaA as MetaAgent
    participant CompA as Constituent Agent
    
    Client->>Scheduler: model.agents.step()
    Scheduler->>Scheduler: Filter agents by is_component flag
    Scheduler->>Scheduler: Exclude agents where is_component==True
    Scheduler->>CompA: ❌ Skip (is_component==True)
    Scheduler->>MetaA: ✓ Schedule (is_component==False)
    par Parallel Execution
        Scheduler->>MetaA: step()
        MetaA->>MetaA: Internal logic
        MetaA->>CompA: step()
    end
    CompA-->>Client: State updated once per tick
Loading

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

Poem

🐰 A badge for components, both wise and true,
No more double-stepping in the parallel zoo!
MetaAgents and kin now dance in the light,
Each agent stepped once—perfectly right.

🚥 Pre-merge checks | ✅ 3 | ❌ 2

❌ Failed checks (1 warning, 1 inconclusive)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 50.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
Description check ❓ Inconclusive The description mentions the core change and linked issues but lacks detail on implementation approach, testing strategy, and specific API changes. Expand the description to explain how is_component flag is implemented, how it integrates with existing code, and provide a brief testing summary.
✅ Passed checks (3 passed)
Check name Status Explanation
Title check ✅ Passed The title accurately summarizes the main change—fixing parallel stepping conflicts when MetaAgents and component agents are both scheduled.
Linked Issues check ✅ Passed The code changes implement the proposed fix by introducing is_component handling to prevent component agents from being scheduled independently alongside their parent MetaAgents.
Out of Scope Changes check ✅ Passed All changes directly address the parallel stepping conflict; modifications to test suite and patching logic align with the fix objectives.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment

Tip

Try Coding Plans. Let us write the prompt for your AI agent so you can ship faster (with fewer bugs).
Share your feedback on Discord.


Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@hillhack hillhack force-pushed the jyoti_paralllel_steping branch 5 times, most recently from e83ed4a to e131699 Compare March 6, 2026 05:18
@hillhack hillhack closed this Mar 6, 2026
@hillhack hillhack force-pushed the jyoti_paralllel_steping branch from 8dd6ba9 to 4ea91ed Compare March 6, 2026 05:36
@hillhack hillhack reopened this Mar 6, 2026
@hillhack hillhack changed the title Fix: issues/162 fixing Parallel Stepping Conflict Mar 6, 2026
@colinfrisch
Copy link
Member

I like the idea, but I'm going to take a bit more time to review, what do you think @wang-boyu ?

@colinfrisch
Copy link
Member

@CodeRabbit review

@colinfrisch colinfrisch self-assigned this Mar 10, 2026
@coderabbitai
Copy link
Contributor

coderabbitai bot commented Mar 10, 2026

✅ Actions performed

Review triggered.

Note: CodeRabbit is an incremental review system and does not re-review already reviewed commits. This command is applicable only when automatic reviews are paused.

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 1

Caution

Some comments are outside the diff and can’t be posted inline due to platform limitations.

⚠️ Outside diff range comments (1)
mesa_llm/parallel_stepping.py (1)

95-106: ⚠️ Potential issue | 🟠 Major

Return values missing in fallback paths and parallel stepping case.

The fallback calls to _original_hardkey_shuffle_do and _original_shuffle_do, as well as the early return in the parallel stepping case, don't return their results. Mesa's shuffle_do returns self to support method chaining (e.g., model.agents.shuffle_do("step").do("advance")); omitting the return breaks this contract.

♻️ Proposed fix
 def _enhanced_shuffle_do(self, method: str, *args, **kwargs):
     """Enhanced shuffle_do with automatic parallel stepping."""
     if method == "step" and self:
         agent = next(iter(self))
         if hasattr(agent, "model") and getattr(agent.model, "parallel_stepping", False):
             step_agents_parallel_sync(list(self))
-            return
+            return self

     if _HardKeyAgentSet and isinstance(self, _HardKeyAgentSet):
-        _original_hardkey_shuffle_do(self, method, *args, **kwargs)
+        return _original_hardkey_shuffle_do(self, method, *args, **kwargs)
     else:
-        _original_shuffle_do(self, method, *args, **kwargs)
+        return _original_shuffle_do(self, method, *args, **kwargs)
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@mesa_llm/parallel_stepping.py` around lines 95 - 106, The
_enhanced_shuffle_do wrapper fails to return values, breaking Mesa's chaining
contract; update _enhanced_shuffle_do so that when parallel stepping is used it
returns the agent container (e.g., return self) after calling
step_agents_parallel_sync(list(self)), and in the fallback branches return the
results of _original_hardkey_shuffle_do(self, method, *args, **kwargs) and
_original_shuffle_do(self, method, *args, **kwargs) respectively; locate these
symbols (_enhanced_shuffle_do, step_agents_parallel_sync,
_original_hardkey_shuffle_do, _original_shuffle_do, _HardKeyAgentSet) to
implement the necessary return statements.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Inline comments:
In `@tests/test_parallel_stepping.py`:
- Line 5: Tests currently import the experimental MetaAgent API (MetaAgent, and
rely on its is_component attribute and remove_constituting_agents method) which
may move or change in future Mesa releases; update the test to either (a) add a
guarded import with a clear fallback/skip (try importing MetaAgent from
mesa.experimental.meta_agents.meta_agent and skip the tests with a helpful
message if ImportError/AttributeError occurs), or (b) pin a minimum Mesa version
in the test requirements/CI config once the API is required/stable so the import
path remains valid and features (is_component, remove_constituting_agents)
exist.

---

Outside diff comments:
In `@mesa_llm/parallel_stepping.py`:
- Around line 95-106: The _enhanced_shuffle_do wrapper fails to return values,
breaking Mesa's chaining contract; update _enhanced_shuffle_do so that when
parallel stepping is used it returns the agent container (e.g., return self)
after calling step_agents_parallel_sync(list(self)), and in the fallback
branches return the results of _original_hardkey_shuffle_do(self, method, *args,
**kwargs) and _original_shuffle_do(self, method, *args, **kwargs) respectively;
locate these symbols (_enhanced_shuffle_do, step_agents_parallel_sync,
_original_hardkey_shuffle_do, _original_shuffle_do, _HardKeyAgentSet) to
implement the necessary return statements.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: f3a8088b-eb8a-435b-a62d-63fa9fc817c5

📥 Commits

Reviewing files that changed from the base of the PR and between 4ea91ed and be35fe5.

📒 Files selected for processing (2)
  • mesa_llm/parallel_stepping.py
  • tests/test_parallel_stepping.py


import pytest
from mesa.agent import Agent, AgentSet
from mesa.experimental.meta_agents.meta_agent import MetaAgent
Copy link
Contributor

Choose a reason for hiding this comment

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

⚠️ Potential issue | 🟡 Minor

🧩 Analysis chain

🏁 Script executed:

#!/bin/bash
# Check if MetaAgent is documented or if there's a more stable import path
rg -n "MetaAgent" --type py -g '!tests/*' | head -20

Repository: mesa/mesa-llm

Length of output: 39


🏁 Script executed:

# Find where MetaAgent is defined
rg -n "class MetaAgent" --type py

# Check directory structure of meta_agents module
fd -t d "meta_agents" 

# Look for __init__ files that might expose MetaAgent
fd -t f "__init__.py" | xargs rg -l "MetaAgent"

Repository: mesa/mesa-llm

Length of output: 41


🏁 Script executed:

# Simple search for MetaAgent class definition
rg "class MetaAgent" --type py

# Search in the entire codebase for the file
fd "meta_agent.py"

# List the experimental directory if it exists
fd -t d "experimental"

Repository: mesa/mesa-llm

Length of output: 39


🏁 Script executed:

# List main directories
git ls-files | head -50

# Check if experimental directory exists at all
git ls-files | grep -i experimental

# Read the test file to understand its context
head -30 tests/test_parallel_stepping.py

Repository: mesa/mesa-llm

Length of output: 2033


🏁 Script executed:

# Read the full test file to see how MetaAgent is used
cat -n tests/test_parallel_stepping.py | head -100

# Check for mesa version requirements or documentation
fd -t f "requirements.txt|setup.py|pyproject.toml|setup.cfg" -x cat

# Look for any documentation about MetaAgent or experimental APIs
fd -t f "*.md" -x grep -l "MetaAgent\|experimental" 2>/dev/null

Repository: mesa/mesa-llm

Length of output: 6625


🏁 Script executed:

# Read the rest of the test file
tail -n +100 tests/test_parallel_stepping.py | head -100

# Search for any Mesa-related documentation about experimental APIs
find . -name "*.md" -type f -exec grep -l "experimental\|MetaAgent" {} \;

# Check if there are any comments in the codebase about API stability
rg "experimental\|deprecated\|unstable" --type py

Repository: mesa/mesa-llm

Length of output: 2288


Using experimental API in tests—consider version pinning if MetaAgent becomes required.

The import path mesa.experimental.meta_agents.meta_agent.MetaAgent indicates this uses Mesa's experimental namespace. Tests depend on specific MetaAgent features (is_component attribute, remove_constituting_agents() method). With the flexible version constraint mesa[rec]>=3.0, future Mesa releases could relocate or modify this API, breaking these tests. If MetaAgent becomes essential, consider pinning a minimum Mesa version once its API stabilizes, or switch to a stable import path if one becomes available.

🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@tests/test_parallel_stepping.py` at line 5, Tests currently import the
experimental MetaAgent API (MetaAgent, and rely on its is_component attribute
and remove_constituting_agents method) which may move or change in future Mesa
releases; update the test to either (a) add a guarded import with a clear
fallback/skip (try importing MetaAgent from
mesa.experimental.meta_agents.meta_agent and skip the tests with a helpful
message if ImportError/AttributeError occurs), or (b) pin a minimum Mesa version
in the test requirements/CI config once the API is required/stable so the import
path remains valid and features (is_component, remove_constituting_agents)
exist.

@hillhack
Copy link
Contributor Author

hillhack commented Mar 10, 2026

mesa/mesa#3452 so to make more sense u can take a look in it . I did it for easy working of multi-agent later.

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Parallel Stepping Conflict

3 participants