fix(llm_agent): initialize self.recorder so @record_model can attach it#232
fix(llm_agent): initialize self.recorder so @record_model can attach it#232
Conversation
LLMAgent.__init__ never set self.recorder, so the hasattr() check in _attach_recorder_to_agents always returned False and the recorder was silently never attached to any LLMAgent instance. Add self.recorder = None with a TYPE_CHECKING import to avoid circular imports, and add tests verifying the attribute exists and that @record_model successfully propagates the recorder. Closes mesa#218
|
Important Review skippedAuto reviews are disabled on this repository. Please check the settings in the CodeRabbit UI or the ⚙️ Run configurationConfiguration used: Organization UI Review profile: CHILL Plan: Pro Run ID: You can disable this status message by setting the Use the checkbox below for a quick retry:
✨ Finishing Touches🧪 Generate unit tests (beta)
📝 Coding Plan
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. Comment Tip CodeRabbit can use your project's `ruff` configuration to improve the quality of Python code reviews.Add a Ruff configuration file to your project to customize how CodeRabbit runs |
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #232 +/- ##
==========================================
- Coverage 90.67% 90.64% -0.04%
==========================================
Files 19 19
Lines 1555 1560 +5
==========================================
+ Hits 1410 1414 +4
- Misses 145 146 +1 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
|
hey @yashhzd! initializing self.recorder makes sense and fixes the issue of not attaching the recorder. I had a small question regarding this part: if TYPE_CHECKING:
from mesa_llm.recording.simulation_recorder import SimulationRecorderSince the file already uses future import annotations, the type hint should work without needing this import at all. Given that SimulationRecorder isn’t directly used at runtime in this file, do we still need the TYPE_CHECKING block? Removing it might simplify things and also resolve the Codecov uncovered line. |
|
Hey @BhoomiAgrawal12, great observation! I looked into this. While So both
Regarding the Codecov patch failure — |
Summary
LLMAgent.__init__never initializedself.recorder, so thehasattr(agent, "recorder")check in_attach_recorder_to_agents()always returnedFalse. This meant the@record_modeldecorator silently never attached theSimulationRecorderto anyLLMAgentinstance.Changes
mesa_llm/llm_agent.py: Addself.recorder: SimulationRecorder | None = Nonein__init__, with aTYPE_CHECKINGimport to avoid circular imports.tests/test_llm_agent.py: Add unit test verifyinghasattr(agent, "recorder")returnsTrueand defaults toNone.tests/test_recording/test_record_model.py: Add integration test verifying@record_modelsuccessfully propagates the recorder toLLMAgentinstances created during model init.Test plan
pytest tests/test_llm_agent.py::test_llm_agent_has_recorder_attribute -xvspassespytest tests/test_recording/test_record_model.py::TestRecordModelDecorator::test_recorder_attached_to_llm_agents -xvspassesruff checkand all pre-commit hooks passCloses #218