Skip to content

Cancel an agent's actions with their hooks when it is removed - #4175

Closed
EwoutH wants to merge 1 commit into
lifecycle-actions-hooksfrom
lifecycle-actions-remove
Closed

EwoutH wants to merge 1 commit into
lifecycle-actions-hooksfrom
lifecycle-actions-remove

Conversation

@EwoutH

@EwoutH EwoutH commented Nov 22, 2026

Copy link
Copy Markdown
Member

Pre-PR Checklist

  • I linked an issue/discussion where a maintainer approved this enhancement/feature for implementation.

Approval Link

#4120, section 3.3 (remove()). Removal with hooks answers @codebreaker32's point 2: CANCELED should mean on_cancel() ran.

Summary

Removing an agent now cancels its actions with their hooks, instead of abandoning the active one in ACTIVE forever. Part 5 of 6.

Motive

On main, remove() on a busy agent cancels the completion event and leaves the action ACTIVE, with no hook (reproduced by @AshNicolus in #4120). A model that needs cleanup has to override remove(), and a reservation held through an interruption is never released if the agent is removed mid-break.

Implementation

remove():

  1. closes the agent: start_action(), resume() and interrupt_for() raise ValueError, and no wake is queued;
  2. cancels a pending wake;
  3. cancels an ACTIVE action with on_progress(delta), on_stop(), on_cancel(). Removal never completes an action, even a due one: it is the one exception to completion wins (added to the decisions entry);
  4. cancels every INTERRUPTED action of this agent that is still referenced, in start order, with on_cancel();
  5. calls super().remove().

The hooks run while the agent is still in the model. The agent tracks the actions it started in a WeakValueDictionary keyed by start order (added at the first start, dropped when final), so an interrupted action the model dropped isn't kept alive and the order is deterministic. PENDING actions aren't tracked. A hook that removes the agent again during removal is a no-op.

Pickling. The proposal didn't cover this: models with action agents pickle on main, and a WeakValueDictionary can't be pickled. HasActions gets __getstate__/__setstate__ that store the tracked actions as a list and rebuild the weak mapping; a test round-trips a model with an interrupted action.

Usage Examples

class Shift(Action):
    def on_start(self):
        self.station.reserve(self.agent)

    def on_cancel(self):          # also runs when the agent is removed mid-break
        self.station.unreserve()

An override of remove() that calls cancel_action() first still works; it's no longer needed.

Additional Notes

model.remove_all_agents() now fires these hooks for every busy agent, in the model's agent order.

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

remove() used to abandon an active action without any hook, leaving it
ACTIVE forever. It now closes the agent, cancels the active action with
on_progress(), on_stop() and on_cancel(), and cancels every interrupted
action it started that is still referenced, in start order. Removal
never completes an action and queues no wake.

The agent tracks the actions it started weakly, so an interrupted
action the model dropped isn't kept alive; pickling still works.

Refs #4120

Assisted-by: Claude Opus 5.5
@EwoutH EwoutH added the breaking Release notes label label Nov 22, 2026
@EwoutH
EwoutH added this pull request to stack #4178 November 22, 2026 09:40
@github-actions github-actions Bot added ci Release notes label docs Release notes label experimental Release notes label testing Release notes label labels Nov 22, 2026
@EwoutH
EwoutH removed this pull request from stack #4178 November 22, 2026 09:43
@EwoutH

EwoutH commented Nov 22, 2026

Copy link
Copy Markdown
Member Author

Folded into #4172: these four PRs review as one contract, so they're combined there as separate commits.

@EwoutH EwoutH closed this Nov 22, 2026
@EwoutH
EwoutH deleted the lifecycle-actions-remove branch November 22, 2026 09:44
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

breaking Release notes label ci Release notes label docs Release notes label experimental Release notes label testing Release notes label

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant