Skip to content

Warn when the on_idle guard leaves an agent idle - #4171

Merged
EwoutH merged 3 commits into
mainfrom
lifecycle-wake-warning
Nov 25, 2026
Merged

EwoutH merged 3 commits into
mainfrom
lifecycle-wake-warning

Conversation

@EwoutH

@EwoutH EwoutH commented Nov 22, 2026 •

Copy link
Copy Markdown
Member

Pre-PR Checklist

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

Summary

A chain of instant actions started from on_idle stops after one step without any sign. The once-per-time guard drops the second wake and logs it at DEBUG, which Mesa's NullHandler hides. This turns the drop into a RuntimeWarning, but only when the agent is actually left idle.

Bug / Issue

Reproduced by @atul-upadhyay-7 in #4120: five chained Action(duration=0) from on_idle run once, and the agent stays idle with nothing in the log. Agreed in the discussion to keep the guard and make the drop visible with warnings.warn rather than the logger.

Implementation

The guard moved from _schedule_wake (when the action ends) to _fire_wake (when the deferred wake event runs). A release after on_idle already ran at this time now queues the wake event as usual; when it runs, it skips silently if the agent is busy again, and warns if the agent is still idle.

That placement avoids a false alarm the proposal didn't cover. In the fleeing example of #4120, on_idle tries unfinished.resume(), which can fail its start requirement (FAILED, wake dropped), and then starts a new Forage. Warning at drop time would flag that, although the agent is busy. Warning at fire time only flags an agent that will really stay idle.

The message says which agent class missed on_idle after which action. It leaves out the agent id and time, so Python shows it once per action type rather than once per agent and step. It is reported from the actions module (stacklevel=1), so a module filter matches it. The advice to chain instant actions directly is in the on_idle docstring. The guard itself and its once-per-time policy are unchanged.

Testing

  • The chain test now expects the warning.
  • New: the warning names the agent class and the action, and is reported from the actions module; and an on_idle whose first action fails its start requirement and whose second one starts doesn't warn.
  • pytest tests/ -Werror -Wdefault::PendingDeprecationWarning passes, apart from test_import_namespace.py::test_simulator_classes_removed, which fails on main too in my environment.

Additional Notes

This is the bottom of the actions lifecycle stack from #4120 and stands alone. It is not a backport candidate: 3.5.x-maintenance has no actions module. @atul-upadhyay-7 proposed and prepared this change in the discussion; I picked it up here so the stack could build on it, with thanks.

Written with help from Claude Code (Claude Opus 5.5).

An on_idle wake dropped by the once-per-time guard was logged at DEBUG,
which Mesa's NullHandler hides by default. A chain of instant actions
started from on_idle therefore stopped after one step without any sign.

The guard now applies when the wake event runs, and emits a
RuntimeWarning only if the agent is still idle then. A wake whose slot
was refilled in the meantime is skipped silently, as before.

Refs #4120

Assisted-by: Claude Opus 5.5
@EwoutH EwoutH added bug Release notes label backport-candidate PRs we might want to backport to an earlier branch labels Nov 22, 2026
@github-actions github-actions Bot added experimental Release notes label testing Release notes label labels Nov 22, 2026
@EwoutH
EwoutH added this pull request to stack #4178 November 22, 2026 09:40
@EwoutH
EwoutH removed this pull request from stack #4178 November 22, 2026 09:43
@EwoutH
EwoutH added this pull request to stack #4179 November 22, 2026 09:44

@atul-upadhyay-7 atul-upadhyay-7 left a comment •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

@EwoutH ran this on the PR head (3b92fcf). the actions tests pass (107, -Werror), 1184 tests pass with the viz and import-namespace tests excluded, and my reproducer from the discussion now shows the RuntimeWarning once with the agent, action and time named, while the guard itself is unchanged (the chain still stops after one step). also merged this with #4121 locally to check the stack - clean merge, all 109 tests pass. the busy-refill and failing-start cases are covered well by the new test.

one small thing in the description: the actions module does not exist on 3.5.x-maintenance at all, so there is nothing to backport this to. that note can just be dropped.

lgtm

@codebreaker32 codebreaker32 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Thanks, LGTM otherwise!

Comment thread mesa/experimental/actions/actions.py Outdated
Comment on lines +715 to +719
f"Agent {self.unique_id} stays idle: {previous!r} ended at time "
f"{self.model.time}, after on_idle already ran at that time, "
f"and on_idle runs at most once per agent per model time. To "
f"chain actions that take no time, start the next one directly "
f"instead of from on_idle.",

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

This fires whenever on_idle ends with an instant action, not just for chains. Say on_idle starts an instant Eat after Forage, and step() gives idle agents new work: the model works fine, but 20 agents print 180 warnings in 10 steps. It also fires when another agent interrupts a normal action in the same instant, where the advice about instant actions doesn't fit.

The id and time make every message unique, so Python never collapses them. What about just saying what happened?

f"{type(self).__name__}.on_idle was not called after {previous.name} ended, "
f"because it already ran for that agent at that time."

That gives one warning for the same model, and the advice could move to the docstring.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Agreed, switched to your wording in f948f28 and moved the advice to the on_idle docstring. Your 20-agent example now prints one warning instead of 200.

Comment thread mesa/experimental/actions/actions.py Outdated
f"chain actions that take no time, start the next one directly "
f"instead of from on_idle.",
RuntimeWarning,
stacklevel=2,

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Nit: _fire_wake is called by the event loop, so stacklevel=2 points at mesa/time/events.py, and a filter on module="mesa.experimental.actions" doesn't catch it. stacklevel=1 would.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Done, stacklevel=1 in f948f28. The test now also checks the warning is reported from actions.py.

from mesa.model import Model
from mesa.time import Event

_mesa_logger = create_module_logger()

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

why is this dropped?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

its only use was the debug log for the suppressed wake, which this pr replaces with the RuntimeWarning (agreed in #4120, since the NullHandler hides debug records so the drop had to become a warning to be visible). nothing else in the file uses _mesa_logger, so the import went with it.

if you'd rather keep the drop traceable at debug too, i have a small patch ready: the guard logs the drop at debug and then warns, and the removed log test comes back adapted to fire time. 108 actions tests pass with -Werror, ruff clean.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

As @atul-upadhyay-7 says: its only use was the debug log for the dropped wake, which the debug level hid behind Mesa's NullHandler. The warning replaces it, so the logger had no remaining use in this file.

@EwoutH EwoutH removed the backport-candidate PRs we might want to backport to an earlier branch label Nov 24, 2026
Naming the agent and the time made every message unique, so a model where
on_idle ends in an instant action printed one warning per agent per step.
Say what happened instead, move the advice to the on_idle docstring, and
report the warning from the actions module so a module filter matches it.

Assisted-by: Claude Code
@EwoutH

EwoutH commented Nov 25, 2026

Copy link
Copy Markdown
Member Author

@mesa/maintainers could I get a green checkmark?

@quaquel

quaquel commented Nov 25, 2026 •

Copy link
Copy Markdown
Member

@EwoutH, here you go. Can you return the favor on #4153, #4164, #4189, #4190, #4126, and #4239? These are largely semi maintenance related.

@EwoutH
EwoutH merged commit d525322 into main Nov 25, 2026
17 checks passed
EwoutH added a commit that referenced this pull request Nov 26, 2026
…ss/on_stop, removal (#4172)

Implements the actions part of #4120 (section 3) as one contract: a final `cancel()`, separate `interrupt()`/`resume()`, completion wins on time, `on_progress(delta)` and `on_stop()`, re-entry guards, and removal that cancels an agent's actions with their hooks. It sits between #4171 (dropped-wake warning, below) and #4176 (randomized invariant test, above).

The four commits follow the spec's sections and can be reviewed one at a time: vocabulary and state, completion wins and scheduling, hooks and guards, removal. Or read the final `actions.py` against the spec.
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.

4 participants