Skip to content

Add Agent.on_idle wake dispatched as a deferred low-priority event - #3833

Merged
jackiekazil merged 2 commits into
mesa:mainfrom
codebreaker32:feat/actions/on-idle
Sep 7, 2026
Merged

jackiekazil merged 2 commits into
mesa:mainfrom
codebreaker32:feat/actions/on-idle

Conversation

@codebreaker32

@codebreaker32 codebreaker32 commented Sep 4, 2026 •

Copy link
Copy Markdown
Collaborator

Summary

Adds Agent.on_idle(previous): called whenever the agent's action has ended — completion, interruption, cancellation, or failure — and nothing has replaced it. Dispatched as a zero-delay Priority.LOW event. Completes section 3 of #3798.

Motive

After #3801 and #3805 an action can fail or be preempted, but nothing tells the agent afterwards; models must poll or schedule around it. on_idle is the "body became free" half of the wake contract: one place to chain the next action or resume an interrupted one. The default is a no-op, and nothing is resumed automatically.

Implementation

  • Every ending funnels through a new Action._release_agent(), which clears the agent's slot and schedules the wake. Agent.remove() bypasses it and cancels a pending wake — a removed agent never wakes.
  • Dispatch is deferred rather than synchronous from _do_complete: a zero-duration action started from on_idle cannot recurse, and every agent finishing at time t decides after all completions at t have run.
  • At most one wake per agent per model time; endings that pile up coalesce, with previous reporting the latest. A wake that finds the agent busy again (interrupt_for refilled the slot synchronously) is skipped.
  • Eleven new lifecycle tests (TestOnIdle); test_start_failure_schedules_nothing sharpened into test_start_failure_schedules_no_completion.

Usage Examples

class Worker(Agent):
    def on_idle(self, previous):
        self.start_action(Task(self, duration=1.0))

Each completion wakes the worker once, after all other completions at that time, and the next task begins, no external driver.

Additional Notes

Docs and the task-driven example are section 5 of #3798 and follow separately. on_idle sits on Agent next to the existing action cluster, pending the mixin question raised in #3805.

@github-actions github-actions Bot added experimental Release notes label testing Release notes label labels Sep 4, 2026
@codebreaker32 codebreaker32 added feature Release notes label and removed testing Release notes label labels Sep 4, 2026
@codebreaker32

Copy link
Copy Markdown
Collaborator Author

tests are failing, see widgetti/solara#1199

@github-actions

github-actions Bot commented Sep 4, 2026

Copy link
Copy Markdown

Performance benchmarks:

Model Size Init time [95% CI] Run time [95% CI]
BoltzmannWealth small 🔵 -0.3% [-0.7%, +0.1%] 🔵 -1.8% [-2.1%, -1.6%]
BoltzmannWealth large 🔵 +1.7% [+1.3%, +2.3%] 🔵 +2.1% [+0.9%, +3.4%]
Schelling small 🔵 +2.5% [+1.9%, +3.1%] 🔵 +2.6% [+1.8%, +3.4%]
Schelling large 🔵 +2.3% [+1.3%, +3.3%] 🔵 +2.5% [+0.3%, +4.8%]
WolfSheep small 🔵 +2.6% [+2.1%, +3.1%] 🔵 +2.0% [-2.0%, +6.3%]
WolfSheep large 🔵 +0.7% [+0.2%, +1.1%] 🔵 +2.5% [+0.4%, +4.5%]
SugarscapeG1mt small 🔵 +0.3% [-0.5%, +1.0%] 🔵 -0.3% [-2.3%, +1.5%]
SugarscapeG1mt large 🔵 -1.8% [-3.1%, -0.6%] 🔵 +2.0% [+0.8%, +3.3%]
BoidFlockers small 🔵 +0.5% [+0.1%, +0.9%] 🔵 +0.5% [+0.1%, +1.0%]
BoidFlockers large 🔵 +1.1% [+0.7%, +1.5%] 🔵 +0.1% [-0.1%, +0.2%]
MultiLevelAllianceModel small 🔵 +1.7% [+0.4%, +2.9%] 🔵 +0.2% [-0.1%, +0.6%]
MultiLevelAllianceModel large 🔵 +3.2% [+2.4%, +4.1%] 🔴 +5.6% [+3.6%, +7.4%]

@github-actions github-actions Bot added docs Release notes label testing Release notes label labels Sep 6, 2026
@github-actions github-actions Bot removed the docs Release notes label label Sep 6, 2026

@quaquel quaquel left a comment

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.

This looks fine to me. Just one clarifying question: why schedule the wake instead of calling it synchronously?

@codebreaker32

Copy link
Copy Markdown
Collaborator Author

Three failure modes pushed me to defer it:

  1. Zero-duration actions recurse. A duration-0 action completes inside start(). If _do_complete called on_idle synchronously, an agent that chains another duration-0 action from on_idle would nest start() → _do_complete() → on_idle() → start() → … without bound. Deferred, the once-per-time guard collapses the chain to a single wake per model.time (test_zero_duration_chain_wakes_once_per_time covers this — it hangs rather than fails if the guard breaks).

  2. interrupt_for() would break. Interruption releases the slot mid-call: synchronous dispatch would run on_idle between current.interrupt() and start_action(incoming), and if on_idle starts anything there, the incoming action then raises "already performing". Deferring, plus skipping the wake when the slot is already refilled by the time it fires, keeps interrupt_for atomic (test_no_wake_when_interrupt_for_refills_the_slot).

  3. Ordering at a timestamp. As a Priority.LOW event, every agent finishing at time t decides after all completions at t. Synchronously, decisions would interleave with completions in heap-insertion order, so agents would decide against a half-updated world (test_wake_runs_after_all_completions_at_that_time).

@jackiekazil

Copy link
Copy Markdown
Member

Agents won't get a second wake at the same time step with a "once per time" guard on. This makes sense, but will this fail silently?

Maybe a docstring for hint to users?
Add it to the debugger?
Thoughts?

@jackiekazil jackiekazil left a comment

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.

Looks good to me.

@codebreaker32

codebreaker32 commented Sep 7, 2026 •

Copy link
Copy Markdown
Collaborator Author

The on_idle docstring already carries the hint:

At most one wake fires per agent per model time, so a zero-duration action started here cannot re-trigger the hook in the same instant.

The suppression does have to be quiet: the only agent that hits it is one chaining zero-duration actions within a single instant, and the alternative to stopping quietly is spinning forever without time advancing.

A debug aid is cheap, though, I can emit a DEBUG-level log line whenever the guard suppresses a wake, either in this PR or with the docs one. Preference?

@jackiekazil

Copy link
Copy Markdown
Member

Ty for the docstring. I missed it.
I think in the PR is better.

@jackiekazil

Copy link
Copy Markdown
Member

LGTM. Thank you!

@jackiekazil
jackiekazil merged commit e31bb84 into mesa:main Sep 7, 2026
15 of 16 checks passed
@EwoutH EwoutH mentioned this pull request Nov 15, 2026
53 of 65 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

experimental Release notes label feature Release notes label testing Release notes label

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants