Skip to content

docs: add agent actions tutorial and API reference - #3960

Merged
codebreaker32 merged 7 commits into
mesa:mainfrom
codebreaker32:docs/actions-states
Nov 30, 2026
Merged

codebreaker32 merged 7 commits into
mesa:mainfrom
codebreaker32:docs/actions-states

Conversation

@codebreaker32

@codebreaker32 codebreaker32 commented Oct 21, 2026 •

Copy link
Copy Markdown
Collaborator

Summary

Adds a tutorial for mesa.experimental.actions, documents the module in the API reference, and adds a runnable HasActions example to it.

Motive

Actions arrived across #3801 (requirements and a FAILED state), #3805 (priority driven preemption) and #3833 (the on_idle wake), and none of it has been documented. The wake contract needs it most. An agent wakes on exactly two triggers, its action ending or a Threshold crossing, and two consequences are not obvious from the API: on_idle(previous) receives the action that just ended, which after an interruption is the interrupting one, and nothing is resumed automatically, so the model has to remember the interrupted action itself.

Implementation

Usage Examples

Both wake sources together. Thirst rises continuously, a Threshold interrupts grazing when the sheep gets too thirsty, and on_idle resumes the graze once drinking is done:

class ThirstySheep(HasActions, Agent, HasEmitters):
    thirst = ContinuousState(fallback_value=0.0, rate=1.0)
    _thirsty = Threshold(
        state=thirst, limit=8.0, callback="got_thirsty", direction="rising"
    )

    # wake source 2: the world crossed a line declared in advance
    def got_thirsty(self):
        self.unfinished = self.current_action  # capture before interrupting
        self.interrupt_for(Drink(self))

    # wake source 1: the body became free
    def on_idle(self, previous):
        if self.unfinished is not None and self.unfinished.is_resumable:
            resuming, self.unfinished = self.unfinished, None
            resuming.resume()  # runs only the remainder
        else:
            self.start_action(Graze(self))

In the tutorial this logs:

t=3.0: starting a fresh Graze
t=6.0: starting a fresh Graze
t=8.0: thirsty, interrupting
t=10.0: resuming Graze at 67%
t=11.0: starting a fresh Graze

@github-actions github-actions Bot added the docs Release notes label label Oct 21, 2026
@codebreaker32
codebreaker32 marked this pull request as draft October 21, 2026 14:45
@github-actions github-actions Bot added the experimental Release notes label label Oct 23, 2026
@EwoutH EwoutH mentioned this pull request Nov 15, 2026
53 of 65 tasks
@EwoutH

EwoutH commented Nov 16, 2026

Copy link
Copy Markdown
Member

Thanks for the PR! Would be nice to have Actions in the tutorial, can I help in any way?

@mesa-triage-bot

Copy link
Copy Markdown

Thanks for documenting timed actions and resumption, @codebreaker32. The failing examples check is stale: its October 23 run fails on the warehouse example's mesa.meta_agents import; the check now passes on main.

Please rebase onto main to refresh CI. Related work to keep in view while finishing the draft: #4120 discusses Event and Action lifecycle conventions.

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

@codebreaker32

Copy link
Copy Markdown
Collaborator Author

Thanks for the PR! Would be nice to have Actions in the tutorial, can I help in any way?

Thanks! It's nearly there. While finishing it I checked it against #4120, since that will change how actions pay for partial work and it turned out fine.
I'd like to land it now and we can add on_progress and on_cancel examples alongside the PRs that introduce them. A read for flow would help most, especially anything that's confusing if you haven't used actions before.

@codebreaker32
codebreaker32 marked this pull request as ready for review November 21, 2026 16:49
@github-actions

Copy link
Copy Markdown

Performance benchmarks:

Model Size Init time [95% CI] Run time [95% CI]
BoltzmannWealth small 🔵 -3.8% [-4.6%, -2.9%] 🔵 +2.5% [+2.1%, +2.8%]
BoltzmannWealth large 🔵 -1.1% [-2.0%, -0.2%] 🟢 -7.7% [-9.7%, -5.8%]
Schelling small 🔵 -1.7% [-2.6%, -0.7%] 🔵 -1.3% [-2.0%, -0.5%]
Schelling large 🔵 +0.2% [-0.8%, +1.1%] 🔵 -1.7% [-2.9%, -0.6%]
WolfSheep small 🔵 +0.7% [+0.3%, +1.2%] 🔵 +3.8% [-0.1%, +8.1%]
WolfSheep large 🔵 +3.5% [+1.6%, +5.5%] 🔴 +5.9% [+3.5%, +8.3%]
SugarscapeG1mt small 🔵 +1.5% [+0.7%, +2.4%] 🔵 +1.1% [-0.5%, +3.0%]
SugarscapeG1mt large 🔵 -0.3% [-2.3%, +2.2%] 🔵 +0.4% [-1.0%, +1.7%]
BoidFlockers small 🔵 +0.7% [+0.4%, +1.0%] 🔵 +1.1% [+0.8%, +1.6%]
BoidFlockers large 🔵 +0.7% [+0.3%, +1.1%] 🔵 +0.8% [+0.6%, +1.1%]
MultiLevelAllianceModel small 🔵 +0.6% [-0.3%, +1.6%] 🔵 +1.1% [+0.4%, +1.6%]
MultiLevelAllianceModel large 🔵 +2.0% [+1.0%, +3.0%] 🔵 +1.8% [+0.2%, +3.6%]

@mesa-triage-bot

Copy link
Copy Markdown

Thanks for refreshing the branch and fixing the interrupted-forage example, @codebreaker32. All seven tutorial code cells run against current Mesa, and the earlier stale check is resolved.

@EwoutH, the author answered your offer with a request for a read for flow. Please review that next; I recommend landing this documentation of the current API and adding examples for #4120’s future lifecycle changes alongside their implementation PRs.

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

@EwoutH

EwoutH commented Nov 24, 2026

Copy link
Copy Markdown
Member

@codebreaker32 agreed, let's land this first. I'll rebase #4172 on it afterwards and update the Drink example to resuming.resume().

@EwoutH EwoutH 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.

Thanks @codebreaker32, this reads really well. It builds up step by step, and the thirsty sheep example ties the two wake sources together nicely. I ran all cells on the PR head and the outputs match the text.

Three small corrections inline (the current_action reason, the 20% comment, the CellAgent sentence), plus a few optional suggestions. Happy for this to merge once those three are fixed; I'll update the tutorial for the #4172 renames afterwards.

Comment thread docs/tutorials/13_agent_actions.ipynb Resolved
Comment thread docs/tutorials/13_agent_actions.ipynb Resolved
Comment thread docs/tutorials/13_agent_actions.ipynb Resolved
Comment thread docs/tutorials/13_agent_actions.ipynb Resolved
Comment thread docs/tutorials/13_agent_actions.ipynb Resolved
Comment thread docs/tutorials/13_agent_actions.ipynb Resolved
@quaquel

quaquel commented Nov 30, 2026

Copy link
Copy Markdown
Member

@codebreaker32, whenever you have time, can you resolve the conflict and then go ahead and merge this.

@codebreaker32

Copy link
Copy Markdown
Collaborator Author

Sure, doing.

Merge main, which brings the action lifecycle rework (mesa#4172). The
module docstring conflict resolves to main's version, which already
drops the double-paying on_interrupt example; the HasActions example
from this branch is kept.

Tutorial updates for mesa#4172:
- Resume with resuming.resume(); start_action() now only starts a
  PENDING action and raised ValueError in the thirsty-sheep example.
- is_resumable means INTERRUPTED, and resume() on a final action raises.
- List the current hooks, including on_progress, on_stop and on_cancel.

Review fixes:
- interrupt_for() leaves the new action in current_action, it doesn't
  clear it.
- The recipe resumes the remaining third, not 20%.
- Drop the CellAgent sentence, which has Agent first.
- INTERRUPTED isn't final, and on_idle doesn't fire when interrupt_for()
  refills the slot.
- Rename "Interruption, Priority and Resumption" and point to the recipe.
- State that the tutorial needs Mesa 4.0, or main until it's released.
@codebreaker32
codebreaker32 merged commit af7d853 into mesa:main Nov 30, 2026
17 of 18 checks passed
EwoutH added a commit to EwoutH/mesa that referenced this pull request Nov 30, 2026
Seven PRs were merged after the notes were drafted: mesa#4284, mesa#3960,
mesa#4261, mesa#3979, mesa#4286, mesa#4216 and mesa#4301. Also moves the release date to
2026-11-30.

Assisted-by: Claude Code
EwoutH added a commit that referenced this pull request Nov 30, 2026
* Update release notes and version for 4.0.0a1

Add the 4.0.0a1 release notes to HISTORY.md, leaving out changes
already released in Mesa 3.5.1 to 3.5.4, and bump the version to
4.0.0a1. Move the #4182 replication id fix, which was added under
4.0.0a0, into the 4.0.0a1 notes where it was released.

Assisted-by: Claude Opus 5.5

* Leave dependabot and pre-commit.ci bumps out of the 4.0.0a1 notes

Assisted-by: Claude Opus 5.5

* Add PRs merged on 2026-11-30 to the 4.0.0a1 notes

Seven PRs were merged after the notes were drafted: #4284, #3960,
#4261, #3979, #4286, #4216 and #4301. Also moves the release date to
2026-11-30.

Assisted-by: Claude Code
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

docs Release notes label experimental Release notes label

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants