CSE Roadmap

Core tier · about 3 hours · 12 parts

Part 4 of 12 · 2 min read

Invariants and a step budget catch what one reference path misses

A rule like "verify before refund" can be checked on any path at all, not just one specific sequence of steps.

Think of a workplace safety rule that does not care which order you do your tasks in, only that you always put on gloves before handling a certain chemical. The rule is checkable regardless of the rest of your routine. It catches the one dangerous shortcut whatever else happens around it.

In plain words: an invariant is a rule that every acceptable path must obey, such as "verify before refund," and a step budget is a limit on steps, calls, tokens or cost -- together they catch two different failure modes that a single reference path cannot: an unsafe shortcut (which an invariant flags regardless of how the rest of the path looks) and wasteful repetition (which a budget flags regardless of whether every step was individually valid).

A worked example. The skipped-verification run is caught by the invariant "verify before refund" -- it violates that rule outright, independent of anything else about the path. Separately, a run with two redundant extra lookups (6 steps instead of the reference's 4) passes every ordering check cleanly, because every step it takes is individually a step the reference also takes -- it's just repeating one of them. Only the step budget (a cap of 5) flags it, because 6 exceeds that cap.

The key lines. From code/mini-trajectory/trajectory.py:

# from code/mini-trajectory/trajectory.py
def violates(path, invariants=INVARIANTS):
    """Invariant (a, b): if b occurs, a must occur before it."""
    for a, b in invariants:
        if b in path and (a not in path or path.index(a) > path.index(b)):
            return True
    return False

Read it as: the check only cares about the relative position of a and b in the path. It does not care what else the path contains or how long it is. That is why an invariant generalizes across every possible path rather than being tied to one reference sequence. It is a rule about a relationship between two steps. It can be checked on any path those two steps appear in.

Invariant, step budget

An invariant is a rule that every acceptable path obeys, such as "verify before refund". A step budget is a limit on steps, calls, tokens or cost.

Example: the redundant-calls run passing every ordering-based scorer while only the step budget catches it shows that invariants and budgets guard against two genuinely different problems -- one about sequence, one about efficiency -- and a trajectory check needs both, not just one.