Repository navigation
Warn when the on_idle guard leaves an agent idle - #4171
Conversation
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
Assisted-by: Claude Opus 5.5
There was a problem hiding this comment.
@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
left a comment
There was a problem hiding this comment.
Thanks, LGTM otherwise!
| 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.", |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
| f"chain actions that take no time, start the next one directly " | ||
| f"instead of from on_idle.", | ||
| RuntimeWarning, | ||
| stacklevel=2, |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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() |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
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
|
@mesa/maintainers could I get a green checkmark? |
…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.
Pre-PR Checklist
Summary
A chain of instant actions started from
on_idlestops after one step without any sign. The once-per-time guard drops the second wake and logs it at DEBUG, which Mesa'sNullHandlerhides. This turns the drop into aRuntimeWarning, but only when the agent is actually left idle.Bug / Issue
Reproduced by @atul-upadhyay-7 in #4120: five chained
Action(duration=0)fromon_idlerun once, and the agent stays idle with nothing in the log. Agreed in the discussion to keep the guard and make the drop visible withwarnings.warnrather 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 afteron_idlealready 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_idletriesunfinished.resume(), which can fail its start requirement (FAILED, wake dropped), and then starts a newForage. 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_idleafter 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 theon_idledocstring. The guard itself and its once-per-time policy are unchanged.Testing
on_idlewhose first action fails its start requirement and whose second one starts doesn't warn.pytest tests/ -Werror -Wdefault::PendingDeprecationWarningpasses, apart fromtest_import_namespace.py::test_simulator_classes_removed, which fails onmaintoo 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-maintenancehas 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).