Skip to content

Fix inconsistent model state when add_agent! is called with out-of-bounds position#1197

Open
Vyshnavi0702 wants to merge 4 commits intoJuliaDynamics:mainfrom
Vyshnavi0702:fix-out-of-bounds-add-agent
Open

Fix inconsistent model state when add_agent! is called with out-of-bounds position#1197
Vyshnavi0702 wants to merge 4 commits intoJuliaDynamics:mainfrom
Vyshnavi0702:fix-out-of-bounds-add-agent

Conversation

@Vyshnavi0702
Copy link
Contributor

Summary

Fixes an inconsistency where add_agent! throws a BoundsError for
out-of-bounds coordinates but still leaves the agent inside the model
container, resulting in an invalid model state.

Fix

Wrap the call to add_agent_own_pos! in a try/catch block and remove
the partially added agent from the container if space insertion fails,
then rethrow the error.

Result

  • Prevents corrupted model state
  • Makes behavior consistent with thrown error
  • Existing tests now pass for the out-of-bounds case

Notes

One unrelated visualization test error remains due to upstream Makie
changes and is not caused by this fix.

@Vyshnavi0702
Copy link
Contributor Author

Hi! This PR fixes the inconsistent model state when add_agent!
is called with an out-of-bounds position.

Please let me know if any changes or additional tests are needed.
Thanks for your time and for maintaining Agents.jl!

Copy link
Member

@Datseris Datseris left a comment

Choose a reason for hiding this comment

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

Hi, thanks for the PR! try-catch must be avoided in any performance-relevant code. Is also unecessary here. You can check manually whether the agent is within the allowed and if not then return the alternative clause.

@Datseris
Copy link
Member

Datseris commented Feb 8, 2026

the condition should be checked before add_agent_own_pos! is called at all...

@Datseris
Copy link
Member

Bump here.

@Vyshnavi0702
Copy link
Contributor Author

Hi! I have updated the implementation to avoid try/catch as suggested and resolved the merge conflicts. All tests pass locally. Please let me know if further changes are needed.

@codecov-commenter
Copy link

codecov-commenter commented Mar 2, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 84.84%. Comparing base (8b5b456) to head (69bd85c).
⚠️ Report is 208 commits behind head on main.

Additional details and impacted files
@@             Coverage Diff             @@
##             main    #1197       +/-   ##
===========================================
+ Coverage   70.12%   84.84%   +14.71%     
===========================================
  Files          42       42               
  Lines        2718     2936      +218     
===========================================
+ Hits         1906     2491      +585     
+ Misses        812      445      -367     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Copy link
Member

@Datseris Datseris left a comment

Choose a reason for hiding this comment

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

This PR adds a lot of unecessary white spaces in various files unrelated to the PR, and as such the Runic tests fail. Please remove these.

@Vyshnavi0702
Copy link
Contributor Author

I cleaned the PR and removed unrelated file changes.
Now only src/core/space_interaction_API.jl is modified as intended.
The fix ensures the agent is inserted into the space before updating the container to avoid inconsistent model state.

Please let me know if any further changes are needed.

Copy link
Member

@Datseris Datseris left a comment

Choose a reason for hiding this comment

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

This PR doesn't achieve anything. It certainly does not change any code functionality. Here you have replaced add_agent_own_pos! with the source code of the function. Naturally this doesn't change anything, it's exactly the same code.

The fact that tests are missing from this PR reflects this: you need to add tests that actually test that the issue is solved, by copying the code from the original issue.

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.

3 participants