Skip to content

Test the action lifecycle invariants under random use - #4176

Open
EwoutH wants to merge 2 commits into
mainfrom
lifecycle-actions-invariants
Open

EwoutH wants to merge 2 commits into
mainfrom
lifecycle-actions-invariants

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.9 (invariants) and section 5, step 4.

Summary

A seeded randomized test that makes random lifecycle calls and checks the invariants of section 3.9 between every call and every event. Part 6 of 6.

Motive

The earlier parts each fix specific sequences. The invariants are what the contract promises for any sequence, including hooks that make lifecycle calls, which is where most lifecycle bugs came from.

Implementation

  • Four agents, 400 random operations per seed, 8 seeds: start or preempt with a new action (random duration including 0, priority including ±inf, interruptible or not, failing start or completion requirements), interrupt (forced or not), cancel, resume or interrupt_for an interrupted action, cancel any open action, remove an agent, and schedule a random call at the exact deadline of the current action. Hooks occasionally interrupt, cancel or resume their own action, and on_idle starts new ones.
  • Events are run one by one (mirroring Model._advance_time), so invariants are checked between events, not just between run calls.
  • Checked: the slot holds exactly the ACTIVE action; an ACTIVE action has exactly one live completion event at its deadline and others have none; reported progress equals progress outside ACTIVE and the deltas sum to it; one on_stop() per ended attempt and one final hook per final state; final states never change; a removed agent has nothing ACTIVE or INTERRUPTED and no wake. A call that raises ValueError must leave everything unchanged.

Usage Examples

Not applicable; test only.

Additional Notes

  • Takes about 1 s. I also ran it with 300 seeds locally; all pass.
  • To check it has teeth, I reintroduced old behaviour one at a time: scheduling the completion after the hook, skipping on_progress, skipping on_stop, skipping interrupted actions on removal, and queuing wakes after removal. Each makes it fail. Completion-wins itself isn't an invariant (an action interrupted at its deadline still satisfies them), so the dedicated tests in part 3 cover it.

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

@EwoutH EwoutH added the testing Release notes label label Nov 22, 2026
@EwoutH
EwoutH added this pull request to stack #4178 November 22, 2026 09:40
@EwoutH
EwoutH removed this pull request from stack #4178 November 22, 2026 09:43
@EwoutH
EwoutH changed the base branch from lifecycle-actions-remove to lifecycle-actions-vocabulary November 22, 2026 09:43
@EwoutH
EwoutH added this pull request to stack #4179 November 22, 2026 09:44
@EwoutH
EwoutH force-pushed the lifecycle-actions-invariants branch from 28d9ad8 to 1bafd12 Compare November 22, 2026 09:54
@atul-upadhyay-7

Copy link
Copy Markdown
Contributor

could Probe.on_progress also check self.progress against the progress reported so far? the checks after the outer call miss the value seen inside the hook: interrupting a duration-10 action at t=2 gives delta=0.2 but self.progress=0.4 there, then progress returns to 0.2 after settlement. a hook-time assertion catches that with the existing seeds.

@EwoutH
EwoutH force-pushed the lifecycle-actions-invariants branch from 1bafd12 to 8403712 Compare November 24, 2026 16:03
@EwoutH
EwoutH force-pushed the lifecycle-actions-invariants branch from 8403712 to 8fbfa57 Compare November 24, 2026 16:11
@EwoutH

EwoutH commented Nov 24, 2026

Copy link
Copy Markdown
Member Author

@atul-upadhyay-7 added in 8fbfa57. On the old code it fails 8 seeds, so it would have caught the double count.

@atul-upadhyay-7 atul-upadhyay-7 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

checked the updated test. it catches the progress double count inside the hook. lgtm

@EwoutH
EwoutH force-pushed the lifecycle-actions-invariants branch 3 times, most recently from 2964d9e to 790a5e8 Compare November 25, 2026 19:28
Base automatically changed from lifecycle-actions-vocabulary to main November 26, 2026 18:38
EwoutH added a commit that referenced this pull request Nov 26, 2026
…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.
A seeded test makes random lifecycle calls, some of them from inside
hooks and some at the exact deadline of an action, and checks the
invariants from #4120 between every call and every event: the slot
holds exactly the ACTIVE action, an ACTIVE action has one completion
event at its deadline, reported progress matches, one on_stop() per
attempt and one final hook per final state, final states never change,
and a removed agent leaves nothing running. A call that raises
ValueError must leave everything unchanged.

Refs #4120

Assisted-by: Claude Opus 5.5
The checks after each outer call missed the value seen inside the hook,
where progress was counted twice before the previous commit's fix.

Assisted-by: Claude Code
@EwoutH
EwoutH force-pushed the lifecycle-actions-invariants branch from 790a5e8 to 0b7eb9a Compare November 26, 2026 18:38
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

testing Release notes label

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants