Skip to content

Check the new action's agent and state before interrupting the current one - #4112

Open
EwoutH wants to merge 1 commit into
mainfrom
interrupt-for-validate-first
Open

EwoutH wants to merge 1 commit into
mainfrom
interrupt-for-validate-first

Conversation

@EwoutH

@EwoutH EwoutH commented Nov 15, 2026 •

Copy link
Copy Markdown
Member

Pre-PR Checklist

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

Summary

interrupt_for() no longer interrupts the current action for an incoming action that is then rejected.

Bug / Issue

Closes #4062, together with #4111 (merged). interrupt_for() interrupted the current action first and only then let start_action() check the incoming one. An incoming action belonging to another agent, or in a finished state, raised after the current action had already been interrupted and its completion event cancelled. The agent was left idle with the old action INTERRUPTED.

Implementation

interrupt_for() checks ownership and state before it interrupts. It only checks conditions that on_interrupt() can't change, so every call that raises now also raised on main, and every call that worked on main still does. Small _check_owner() and _check_startable() helpers keep these checks in one place for start_action()/start() and interrupt_for().

The duration is still resolved at start, after the interruption: a duration callable may read state that on_interrupt() changes, or rely on a start requirement holding. So a negative duration, like a failed start requirement, still leaves the old action interrupted, as documented. Passing the current action itself still interrupts and restarts it, so the state check skips it.

Testing

  • A parametrized test checks that an incoming action from another agent and a completed one both raise while the current action keeps running to completion. Both cases fail on main.
  • A second test checks that interrupt_for(current_action) still restarts it, as on main.
  • tests/experimental/test_actions.py and tests/test_agent.py: 146 passed.

Additional Notes

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

@github-actions github-actions Bot added experimental Release notes label testing Release notes label labels Nov 15, 2026
@EwoutH
EwoutH added this pull request to stack #4113 November 15, 2026 09:24
@atul-upadhyay-7

Copy link
Copy Markdown
Contributor

ran this locally: 147 tests pass. one regression for state-dependent durations: if should_interrupt() refuses the incoming action, interrupt_for() has already called its duration callable and cached the result, so a later start uses the old value. e.g. speed is 5 at the refused interrupt_for and 1 at the later start_action: duration comes out as 5 here, 1 on #4111. resolving the duration after should_interrupt() returns True keeps the check before the interruption. i tried that locally: 147 pass and the stale value is gone.

@EwoutH
EwoutH marked this pull request as ready for review November 15, 2026 18:20
@EwoutH
EwoutH requested a review from codebreaker32 November 15, 2026 18:20
@github-actions

Copy link
Copy Markdown

Performance benchmarks:

Model Size Init time [95% CI] Run time [95% CI]
BoltzmannWealth small 🔵 -2.0% [-2.2%, -1.7%] 🔵 -0.3% [-0.5%, -0.1%]
BoltzmannWealth large 🔵 -0.5% [-1.0%, -0.2%] 🔵 -0.4% [-1.3%, +0.5%]
Schelling small 🔵 +0.0% [-0.4%, +0.5%] 🔵 +0.2% [-0.6%, +0.9%]
Schelling large 🔵 +0.2% [-0.5%, +0.8%] 🔵 +1.2% [+0.0%, +2.3%]
WolfSheep small 🔵 +0.3% [-0.2%, +0.8%] 🔵 -2.5% [-6.1%, +1.3%]
WolfSheep large 🔵 -1.1% [-1.7%, -0.6%] 🔵 -1.2% [-2.6%, +0.0%]
SugarscapeG1mt small 🔵 +0.3% [-0.0%, +0.6%] 🔵 -0.7% [-2.1%, +0.9%]
SugarscapeG1mt large 🔵 +0.9% [+0.1%, +1.7%] 🔵 -0.2% [-1.9%, +1.7%]
BoidFlockers small 🔵 -0.6% [-1.0%, -0.2%] 🔵 -0.1% [-0.6%, +0.4%]
BoidFlockers large 🔵 -1.5% [-1.9%, -1.2%] 🔵 +0.0% [-0.2%, +0.2%]
MultiLevelAllianceModel small 🔵 -0.4% [-1.1%, +0.5%] 🔵 -0.4% [-1.4%, +0.2%]
MultiLevelAllianceModel large 🔵 +0.0% [-0.4%, +0.4%] 🔵 +0.4% [-0.5%, +1.3%]

@EwoutH
EwoutH force-pushed the interrupt-for-validate-first branch from e75d6a6 to fff8d36 Compare November 15, 2026 20:56
Base automatically changed from action-slot-on-activation to main November 15, 2026 20:59
@EwoutH
EwoutH force-pushed the interrupt-for-validate-first branch 2 times, most recently from 980a94a to 487308a Compare November 15, 2026 21:11
@EwoutH

EwoutH commented Nov 15, 2026

Copy link
Copy Markdown
Member Author

Reworked this into one commit. @atul-upadhyay-7 your ordering fixes the stale duration, but a duration callable can also rely on a start requirement (lambda a: a.target * 2 guarded by a.target is not None), and resolving it before start() checks requirements turns a clean failure into a TypeError. So interrupt_for() now only checks what on_interrupt() can't change (ownership and finished states) and leaves the duration where it was.

@codebreaker32 that drops the resolve-once helper you suggested; the cost is that a negative duration still leaves the old action interrupted, like a failed requirement.

interrupt_for() interrupted the current action first and only then let start_action() check the incoming one. An incoming action from another agent, or one that is COMPLETED or FAILED, raised after the current action had already been interrupted and its completion cancelled.

interrupt_for() now runs those checks before it interrupts. They are limited to what on_interrupt() can't change, so every call that raises now also raised before, and every call that worked still does: the duration is still resolved at start, after the interruption, and passing the current action still restarts it.

Closes #4062.

Assisted-by: Claude Opus 5.5
@EwoutH
EwoutH force-pushed the interrupt-for-validate-first branch from 487308a to 979bbe3 Compare November 15, 2026 21:13
@EwoutH EwoutH changed the title Validate the incoming action before interrupt_for() interrupts Don't interrupt the current action for another agent's action or a finished one Nov 15, 2026
@EwoutH EwoutH changed the title Don't interrupt the current action for another agent's action or a finished one Check the new action's agent and state before interrupting the current one Nov 15, 2026
@atul-upadhyay-7

Copy link
Copy Markdown
Contributor

checked this against main and the pr head. one behaviour change the description doesn't mention: when the current action is non-interruptible (or should_interrupt refuses), interrupt_for() used to return False for an incoming action from another agent or a finished one. now it raises before should_interrupt runs.

cur = a.start_action(Action(a, duration=5, interruptible=False))
a.interrupt_for(Action(b, duration=1))  # b is another agent
# main: False, cur stays ACTIVE
# this branch: ValueError

same for a completed incoming action. the docstring still says a refusal returns False.

moving the owner and state checks to after the should_interrupt check, and before current_action.interrupt(), keeps the point of the pr (nothing is interrupted before the raise) and keeps a refusal as False. 146 tests pass with that order and those cases match main again. if raising there is intended, a line in the description and in Returns would cover it.

EwoutH added a commit that referenced this pull request Nov 24, 2026
cancel() now ends an action for good, in the new CANCELED state, and
fires on_cancel(). The old resumable cancel is interrupt(force=True).
An INTERRUPTED action continues with resume(); start() is private and
agents start PENDING actions with start_action(). state and
current_action are read-only.

interrupt_for() validates the new action before interrupting, takes a
PENDING or INTERRUPTED action, returns True without changes for the
current action, and asks whether the slot is free afterwards rather
than what interrupt() returned. Resolved durations must be finite and
>= 0, priorities not NaN.

Supersedes #4112. Refs #4120

Assisted-by: Claude Opus 5.5
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.

rejected actions can leave an agent busy or interrupt its current action

2 participants