Skip to content

Claim the action slot when the action becomes active - #4111

Merged
EwoutH merged 1 commit into
mainfrom
action-slot-on-activation
Nov 15, 2026
Merged

EwoutH merged 1 commit into
mainfrom
action-slot-on-activation

Conversation

@EwoutH

@EwoutH EwoutH commented Nov 15, 2026 •

Copy link
Copy Markdown
Member

Pre-PR Checklist

  • This PR is a bug fix, not a new feature or enhancement.

Summary

An action that start() rejects (invalid state, negative duration, or a duration callable that raises) no longer leaves the agent busy for good, and calling Action.start() directly can no longer run a second action next to the one in the slot.

Bug / Issue

Part of #4062, the first step of the approach discussed there. start_action() filled the agent's slot before Action.start() validated the action, so a rejected action stayed in the slot as PENDING and every later start_action() was refused. In the other direction, Action.start() called directly ran the action outside the slot, so the agent could start a second action alongside it.

Implementation

  • Action.start() checks that the agent is idle and claims the slot when it sets the action ACTIVE, so the slot holds an action exactly while that action is active.
  • A failed start requirement never claims the slot, so start() schedules the idle wake itself; on_idle still fires after a failed start.
  • duration=0 still completes synchronously inside start().
  • start_action() keeps only the ownership check.

Testing

Three tests in TestErrorHandling: a raising duration callable and a completed action both leave the agent usable, and a direct start() claims the slot and refuses a second action. All three fail on main. The existing wake test for a failed start still passes. tests/experimental/test_actions.py and tests/test_agent.py: 140 passed.

Additional Notes

@github-actions

Copy link
Copy Markdown

Performance benchmarks:

Model Size Init time [95% CI] Run time [95% CI]
BoltzmannWealth small 🟢 -4.4% [-5.4%, -3.5%] 🔵 -2.4% [-2.5%, -2.3%]
BoltzmannWealth large 🔵 -0.8% [-1.2%, -0.5%] 🔵 -1.6% [-2.2%, -1.1%]
Schelling small 🔵 -0.0% [-0.5%, +0.4%] 🔵 -0.4% [-1.2%, +0.4%]
Schelling large 🔵 -0.0% [-0.7%, +0.6%] 🔵 -3.3% [-4.1%, -2.3%]
WolfSheep small 🔵 -1.4% [-1.8%, -1.0%] 🔵 +4.2% [-0.1%, +8.7%]
WolfSheep large 🔵 -1.8% [-2.2%, -1.5%] 🔵 -2.8% [-3.5%, -2.1%]
SugarscapeG1mt small 🔵 -1.9% [-2.1%, -1.6%] 🔵 -1.3% [-3.1%, +0.4%]
SugarscapeG1mt large 🔵 -3.4% [-4.2%, -2.8%] 🔵 +0.6% [-1.5%, +2.7%]
BoidFlockers small 🟢 -6.1% [-6.8%, -5.4%] 🔵 -0.2% [-0.5%, +0.2%]
BoidFlockers large 🔵 -2.9% [-3.4%, -2.5%] 🔵 -0.4% [-0.7%, -0.2%]
MultiLevelAllianceModel small 🟢 -10.2% [-12.1%, -8.0%] 🔵 -0.2% [-0.4%, +0.1%]
MultiLevelAllianceModel large 🟢 -6.5% [-8.1%, -4.7%] 🔵 +0.3% [+0.1%, +0.6%]

@atul-upadhyay-7

Copy link
Copy Markdown
Contributor

ran this locally: 143 tests pass and a direct start() on a busy agent is now refused. the failing "examples" job looks unrelated: test_model_steps[WarehouseModel] in mesa-examples raises CellFullException with no seed, about 3% of runs on main too (17/500 on main, 16/500 on this branch in my runs). one thing not covered here: if on_start() cancels its own action, start() still schedules a completion event afterwards, so a resume at t=3 of a duration-10 action completes at t=10 instead of 13. a state check right after the hook fixes it. happy to add a test for that.

@EwoutH
EwoutH marked this pull request as ready for review November 15, 2026 18:20
@github-actions

Copy link
Copy Markdown

Performance benchmarks:

Model Size Init time [95% CI] Run time [95% CI]
BoltzmannWealth small 🔵 -0.2% [-0.6%, +0.1%] 🔵 +0.6% [+0.5%, +0.7%]
BoltzmannWealth large 🔵 +0.4% [+0.1%, +0.7%] 🔵 +2.3% [+0.7%, +3.9%]
Schelling small 🔵 -0.2% [-0.5%, +0.2%] 🔵 -0.4% [-1.2%, +0.3%]
Schelling large 🔵 +0.7% [+0.3%, +1.1%] 🔵 +1.4% [+0.5%, +2.4%]
WolfSheep small 🔵 -0.7% [-1.0%, -0.3%] 🔵 +1.0% [-2.7%, +5.1%]
WolfSheep large 🔵 -0.1% [-0.5%, +0.3%] 🔵 +0.3% [-0.4%, +1.0%]
SugarscapeG1mt small 🔵 -0.7% [-0.9%, -0.5%] 🔵 +0.3% [-1.0%, +1.7%]
SugarscapeG1mt large 🔵 -0.3% [-0.9%, +0.3%] 🔵 +0.4% [-0.6%, +1.5%]
BoidFlockers small 🔵 +0.3% [-0.1%, +0.8%] 🔵 -0.2% [-0.6%, +0.2%]
BoidFlockers large 🔵 +0.3% [-0.2%, +0.8%] 🔵 -0.1% [-0.4%, +0.1%]
MultiLevelAllianceModel small 🔵 +0.3% [-0.7%, +1.3%] 🔵 +0.1% [-0.1%, +0.3%]
MultiLevelAllianceModel large 🔵 -0.3% [-0.7%, +0.3%] 🔵 -0.9% [-1.7%, +0.1%]

@EwoutH

EwoutH commented Nov 15, 2026

Copy link
Copy Markdown
Member Author

Thanks for testing @atul-upadhyay-7! I reproduced the on_start()-cancels-itself case: the action completes at t=10 instead of 13. It does the same on main though, so it's an existing bug rather than one introduced here. It's a different invariant too (a completion event existing only while the action is ACTIVE), so I'd like to keep this PR focused on the slot.

Would you like to open a follow-up PR with your state check after the hook and the test? It would help to mention how a model would run into it, since start_requirements is the intended way to refuse a start.

start_action() filled the agent's slot before Action.start() validated the action, so an action rejected for its state or duration, or whose duration callable raised, stayed in the slot and left the agent busy for good. Conversely, calling Action.start() directly ran the action outside the slot, so the agent could run a second action alongside it.

Action.start() now checks that the agent is idle, and claims the slot only when it sets the action ACTIVE. The slot then holds an action exactly while it is active. A failed start requirement never claims the slot, so start() schedules the idle wake itself. start_action() keeps only the ownership check.

Part of #4062.

Co-authored-by: Atul Upadhyay <[email protected]>
Assisted-by: Claude Opus 5.5
@EwoutH
EwoutH force-pushed the action-slot-on-activation branch from 78a9475 to 46a3da6 Compare November 15, 2026 20:56
@EwoutH EwoutH added bug Release notes label and removed testing Release notes label experimental Release notes label labels Nov 15, 2026
@EwoutH
EwoutH merged commit f66b5cf into main Nov 15, 2026
18 checks passed
@github-actions github-actions Bot added experimental Release notes label testing Release notes label labels Nov 15, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Release notes label experimental Release notes label testing Release notes label

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants