Skip to content

Don't schedule a completion for an action its own hook ended - #4121

Closed
atul-upadhyay-7 wants to merge 3 commits into
mesa:mainfrom
atul-upadhyay-7:followup-on-start-cancel
Closed

atul-upadhyay-7 wants to merge 3 commits into
mesa:mainfrom
atul-upadhyay-7:followup-on-start-cancel

Conversation

@atul-upadhyay-7

Copy link
Copy Markdown
Contributor

Pre-PR Checklist

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

Summary

if on_start() or on_resume() cancels its own action, start() still schedules the completion event. the action stays INTERRUPTED, but if it is resumed later the old event completes it early.

Bug / Issue

raised in the discussion on lifecycle conventions (#4120), where it was reproduced. on main, an action with duration 5 whose on_start() calls self.cancel() is INTERRUPTED right after start(). resumed at t=1 it should finish at t=6, but the leftover event from the first start fires at t=5 and completes it early. same with on_resume(): cancel in the hook, resume again later, and it ends at the first resume's time.

Implementation

start() returns right after the on_start()/on_resume() hook if the action is no longer ACTIVE, before it works out the remaining time and schedules the completion. the action keeps whatever state the hook gave it. i also added two lines to the docstring.

Testing

two tests in TestHookEndsAction (cancel in on_start, cancel in on_resume). both fail on main (5.0 instead of 6.0, 6.0 instead of 7.0) and pass with the change. tests/experimental/test_actions.py: 109 passed. ruff check and ruff format --check are clean.

If on_start() or on_resume() cancels the action, start() still scheduled
the completion event. The action stayed INTERRUPTED, but resuming it later
completed it at the old time instead of after its remaining duration.
@github-actions github-actions Bot added experimental Release notes label testing Release notes label labels Nov 16, 2026
@atul-upadhyay-7

Copy link
Copy Markdown
Contributor Author

@EwoutH this is the follow-up you asked for on #4111. you're right that start_requirements is the way to refuse a start, so i'm not claiming a model should do this on purpose. the case is any on_start() or on_resume() that ends its own action (cancel(), or interrupt() through code it calls): main still schedules the completion, so a later resume ends early (t=5 instead of t=6 in the test). i don't have a real model that hits it, so it's about keeping the "completion event only while ACTIVE" invariant you described.

@github-actions

Copy link
Copy Markdown

Performance benchmarks:

Model Size Init time [95% CI] Run time [95% CI]
BoltzmannWealth small 🔵 +0.4% [-0.4%, +1.3%] 🔵 +3.0% [+2.6%, +3.4%]
BoltzmannWealth large 🔵 +0.7% [+0.2%, +1.3%] 🔵 -0.2% [-0.8%, +0.2%]
Schelling small 🔵 -0.5% [-1.0%, +0.1%] 🔵 -1.0% [-1.6%, -0.2%]
Schelling large 🔵 +0.2% [-0.5%, +0.9%] 🔵 -1.7% [-2.7%, -0.7%]
WolfSheep small 🔵 -0.5% [-1.1%, +0.0%] 🔵 +3.1% [-0.5%, +6.9%]
WolfSheep large 🔵 +0.3% [-0.2%, +0.8%] 🔵 +0.2% [-0.9%, +1.2%]
SugarscapeG1mt small 🔵 +1.0% [+0.5%, +1.6%] 🔵 -0.0% [-1.6%, +1.4%]
SugarscapeG1mt large 🔵 -0.0% [-1.2%, +1.1%] 🔵 +1.7% [+0.5%, +2.9%]
BoidFlockers small 🔵 -1.0% [-2.1%, +0.2%] 🔵 -1.4% [-1.9%, -0.9%]
BoidFlockers large 🔵 -1.1% [-2.0%, -0.1%] 🔵 -1.8% [-2.1%, -1.6%]
MultiLevelAllianceModel small 🔵 -3.1% [-4.8%, -1.1%] 🔵 -0.3% [-0.7%, +0.0%]
MultiLevelAllianceModel large 🔵 -0.9% [-6.7%, +3.8%] 🔵 +0.3% [-0.0%, +0.6%]

@mesa-triage-bot

Copy link
Copy Markdown

Thanks @atul-upadhyay-7 for the two timing regressions. I reproduced completion at t=5/t=6 on main and t=6/t=7 with the guard.

@EwoutH, should #4120 support hooks ending their own action, or require hooks to avoid lifecycle transitions and use start_requirements to refuse entry? I recommend supporting this guard: refusing entry remains separate, while an action that has already ended should leave no completion event that can finish a later resume early.

Automated note by an AI model; it can be wrong, and maintainers make the decisions. About this bot.

EwoutH added a commit that referenced this pull request Nov 24, 2026
Each attempt now has a deadline. interrupt(), cancel() and
interrupt_for() complete an action that has reached it instead of
interrupting it, so an interrupt at the completion time no longer
leaves a resumable action that pays twice. progress is exactly 1.0
from the deadline on.

The completion event is scheduled before on_start()/on_resume() run,
so a hook that interrupts, cancels or resumes its own action goes
through the normal methods and leaves no stale completion behind.

Supersedes #4121; its tests are carried over. Refs #4120

Co-authored-by: atul-upadhyay-7 <[email protected]>
Assisted-by: Claude Opus 5.5
@atul-upadhyay-7

Copy link
Copy Markdown
Contributor Author

Hi, I merged current main locally and the only conflicts were in actions.py and test_actions.py. Taking main for both leaves no diff, since #4172 already covers this. Happy to close this one as superseded, or leave it for you to close. Thanks for the redesign.

@EwoutH

EwoutH commented Nov 29, 2026

Copy link
Copy Markdown
Member

Thanks for your input, it was valuable!

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.

2 participants