Requirements Checker¶
Abstract
Requirements are named pass/fail checks that run automatically at the end of every trial (and optionally during the simulation loop itself). They give you a structured way to answer the question:
Did this simulation do what it needed to do or meet some specification while it ran?
Results are saved to requirements.json inside each trial directory, stored on TrialStatus, and aggregated per-requirement at the job level for reports and the Dojo monitor.
Overview¶
A requirement is a function you register on the RuntimeManager. It is called with the model, the last simulation state, and (at end of trial) the full telemetry parquet file. Mojo calls each registered function in order, collects the results, and writes them to disk.
The function signature is:
| Python | |
|---|---|
mojo_model: theMojoModelfor the trial, giving access touser_data, distributions, named values, etc.state: the lastMjStateseen before the trial ended (accessstate.data.qpos,state.data.time, etc.)df: the full trial telemetry as aMojoDataFrame(a Polars DataFrame with Mojo's signal-aware namespace).Nonewhen the function is called during a live step check (see Live checks).- Return value:
(passed, message): the verdict and a human-readable description.passedis tri-state: True: the requirement is satisfiedFalse: the requirement is violatedNone: no verdict yet. During live checks this is a no-op (nothing is recorded); a check that is stillNoneat end of trial is recorded as failed
Completion states¶
Requirements affect how a trial's Completion is recorded:
Completion |
Meaning | Counted as |
|---|---|---|
SUCCESS |
Trial ended normally and all requirements passed (or none were registered) | Success |
FAILURE |
Trial ended normally but at least one requirement returned passed=False |
Requirement failure |
TERMINATED |
A live requirement with terminate_on_fail=True stopped the simulation early |
Requirement failure |
ERROR |
An unhandled exception was raised while processing the trial | Error |
INCOMPLETE |
Trial is still running (intermediate state) | Pending |
Failures vs. errors
Job statistics (the runtime report, JobStatus, and the Dojo monitor) keep requirement failures and errors separate: FAILURE/TERMINATED mean the run worked but missed its requirements, while ERROR means the run itself broke.
Errored trials are also excluded from throughput and time-remaining estimates, since a run that crashed says nothing about how long a real trial takes.
Registering requirements¶
There are two equivalent approaches to defining a requirement: using a decorator and direct registration.
Info
It is recommended to define requirements functions before running your main simulation loop.
Both styles accept the same keyword arguments:
| Argument | Meaning |
|---|---|
name |
Label used in results and telemetry |
every |
None: end-of-trial only. N: also evaluate every N steps during the simulation |
terminate_on_fail |
When every is set, a failing live check stops the simulation (trial fails) |
terminate_on_pass |
When every is set, a passing live check stops the simulation (trial succeeds) |
latch_on_fail |
Defaults to True. When every is set, once the check fails it is never evaluated again (see Latching Results) |
latch_on_pass |
Defaults to False. When every is set, once the check passes it is never evaluated again (see Latching Results) |
post_result |
Post live results to the telemetry parquet (see below) |
Decorator style¶
The decorator returns the original function unchanged, so the function remains callable on its own for testing.
Direct registration¶
Given an end-of-trial check:
register it with add_requirement:
Live checks¶
By default (every=None), requirements are evaluated once at end of trial. Set every=N to also run the check inside step() every N steps:
every=N: evaluate every N steps. Useful for expensive checks or when only coarse monitoring is neededdfis alwaysNoneduring live calls; usestatefor real-time values or cache key values in yourmojo_model.user_dataobject- Returning
Nonemeans no verdict yet, for example "reached the goal" before the goal is reachable, so an unfinished objective is not mistaken for a violation. - It never posts telemetry, fails the requirement (see the note below), or triggers
terminate_on_fail/terminate_on_pass. - It is still recorded for that sim time, so
last_passed()can tell "evaluated, no verdict yet" from "not evaluated at this step". - Unless
post_result=False, eachTrue/Falseverdict is recorded in the telemetry parquet asRequirements/{name}:result(1.0passed,0.0failed), so you can plot it in the Dojo beside other signals, where the two values are labeledPASSandFAIL. - The last known verdict is re-posted on steps with no evaluation or no verdict, so the signal is continuous.
Note: Failed Live Requirement
A live requirement that explicitly fails (returns False) at any point during the run is failed for the trial, even if it passes again by end of trial. None verdicts never trigger this.
Since latch_on_fail defaults to True, the function stops being called right after that first failure, and the recorded message reads latched failed at t=.... Pass latch_on_fail=False to keep evaluating after a failure. The message then reports how many live checks failed, when the first failure occurred, and the end-of-trial result.
Reacting to Live Results in the Loop¶
Live results are cached by simulation time, so your control code can read the outcome of the most recent evaluation at no additional cost:
| Python | |
|---|---|
Early termination¶
On Failure¶
Add terminate_on_fail=True to stop the simulation as soon as a live check fails:
When the check fails, RuntimeManager.step() raises RequirementTerminated (a subclass of SimulationStopped, so existing handlers keep working), which unwinds the simulation loop.
- The trial is marked
Completion.TERMINATED. - End-of-trial evaluation still runs, so every requirement appears in
requirements.json. - If several terminating requirements fail on the same step, all are evaluated and their messages are combined into the one exception.
On Success¶
The mirror image: add terminate_on_pass=True to stop the simulation as soon as a live check passes, without spending the rest of the simulation budget. Combine it with None verdicts so the waiting period is not counted as a failure:
When the check passes, RuntimeManager.step() raises RequirementSatisfied (also a subclass of SimulationStopped). Unlike terminate_on_fail=True, this is a normal completion: end-of-trial evaluation runs as usual and the trial is marked SUCCESS (or FAILURE if some other requirement failed), not TERMINATED.
If a failing terminator and a passing one fire on the same step, the failure wins and the trial terminates as a failure.
Note
Only requirements with every set and terminate_on_fail=True/terminate_on_pass=True can stop the simulation early. Non-terminating live checks let it continue.
Latching Results¶
Latching is for a requirement that is expensive to keep checking once it has passed or failed. latch_on_pass and latch_on_fail lock in a verdict the first time the check produces it. The function is never called again, live or at end of trial, and the locked-in result is replayed for free. Each can be set independently.
They are not symmetric, though:
latch_on_fail: Any live failure already dooms the trial for that requirement (see the note above), so locking it in does not change the outcome. It only saves calls tofn. Because of that,latch_on_faildefaults toTrue. Passlatch_on_fail=Falseto keep the function running after a failure, for example for its own logging or side effects.latch_on_pass: Without it, a requirement that passes once but fails on a later evaluation is still failed for the trial. With it, the check is never run again once it passes, so the requirement stays passed. This is the only way to get "once passed, stays passed" without ending the simulation viaterminate_on_pass. Because it can hide a real later violation,latch_on_passdefaults toFalseand must be opted into.
Latching logs one debug line when it locks in ("requirement 'x' latched pass/fail at t=..."), not on every step that replays the verdict.
Accessing results¶
In-process¶
After the with runtime_manager block exits, the results are available directly on the manager:
Per-Trial JSON¶
After each trial, Mojo writes a requirements.json to the trial directory:
| JSON | |
|---|---|
Runtime Report¶
The standard Mojo job runner aggregates results across all trials. The generated MOJO_RUNTIME_REPORT.md includes a per-requirement table and pass/fail counters.
Dojo¶
The Dojo monitor separates successes, requirement failures, and errors in its chips, progress bar, and timeline chart. This provides a graphical way to quickly identify trials with failed requirements.
- Monitor provides a way to view the job requirements at a glance via the top level summary, callouts for the individual trials, and a timeline chart to indicate if any failures are time based
- Mosaic provides a tabular view for a particular trial in the
Trial Executionexpander which includes any of the logs provided for verdicts
Using user_data in checks¶
Because the model argument is the full MojoModel, you can access your handoff data to make requirements parametric per-trial. Define your handoff model:
| Python | |
|---|---|
then read it back with get_user_data inside the check:
This makes the same check automatically adapt to each trial's generated configuration.
Using state for final-state checks¶
End-of-trial requirements receive the last state the simulation reached, which is useful for checking where things ended up without needing the full telemetry:
Success
You now know the ins and outs of using automated requirement evaluation to cleanly trigger simulation shutdowns and more easily gain insight on simulation performance!