Invariants
One invariant/v1 document gives the same verdict when HITL enforces it before each action and when Evals asserts it over a finished trajectory.
An invariant is a rule over a trajectory's events: "an approval before any send", "one charge per booking",
"never a hard delete". HITL enforces invariants before each action; Evals asserts them over a finished trajectory.
Both read the same invariant/v1 document through core, so a rule means the same thing at runtime and in tests.
Core owns the schema (schemas/invariant/v1.json), parseInvariants / parse_invariants and the shared vectors
in spec/conformance/invariants/. It does not evaluate; HITL and Evals do, and both replay the same vectors.
const policy = loadYaml(
`invariants:
- name: approval before any send
each: { tool: "send_*" }
after: { approval: { tool: "send_*", status: [approved, allowed] } }
same: $.call_id
- name: one charge per booking
each: { tool: charge_card }
limit: { max: 1, by: $.args.booking_id, per: 24h }
- name: never hard delete
never: { tool: delete_account, args: { hard: true } }
`,
{ file: "policy.yaml" },
);
const invariants = parseInvariants((policy.value as { invariants: unknown }).invariants, {
at: ["invariants"],
file: "policy.yaml",
locate: (path) => policy.locate(path),
});
for (const inv of invariants) {
const detail = inv.limit
? `max ${inv.limit.max} per ${inv.limit.by} in ${inv.limit.perMs} ms`
: `same ${inv.same.map((s) => s.guarded).join(", ") || "-"}`;
console.log(`${inv.name}: ${inv.kind}, ${inv.effect}, ${detail}`);
}policy = load_yaml(
"""invariants:
- name: approval before any send
each: { tool: "send_*" }
after: { approval: { tool: "send_*", status: [approved, allowed] } }
same: $.call_id
- name: one charge per booking
each: { tool: charge_card }
limit: { max: 1, by: $.args.booking_id, per: 24h }
- name: never hard delete
never: { tool: delete_account, args: { hard: true } }
""",
file="policy.yaml",
)
invariants = parse_invariants(policy.value["invariants"], at=["invariants"], file="policy.yaml", locate=policy.locate)
for inv in invariants:
if inv.limit:
detail = f"max {inv.limit.max} per {inv.limit.by} in {inv.limit.per_ms} ms"
else:
detail = f"same {', '.join(s.guarded for s in inv.same) or '-'}"
print(f"{inv.name}: {inv.kind}, {inv.effect}, {detail}")Operators
Every invariant has a name (unique in its document) and exactly one operator:
| Operator | Violation |
|---|---|
after: <selector> | a guarded (each) event with no earlier matching event |
not_after: <selector> | a guarded event with an earlier matching event |
followed_by: <selector> (+ within) | a guarded event with no later matching event (Evals only; HITL cannot see the future) |
limit: { max, by?, per?, sum? } | the guarded event that takes the count (or the sum of a numeric path) past max in its by partition, and every later one in that partition |
never: <selector> | any matching event (no each) |
Selectors
{ tool, args?, ok? }selectstool_callevents.toolis a name, a core glob (send_*) or a list;argsis a matcher (nojudge: an invariant must decide without a model);ok: truekeeps calls without an error. HITL treats the action it is guarding as succeeding.{ approval: { tool?, status? } }selectsapprovalevents; statuses areallowed,approved,denied,expired,cancelled.{ event, where? }selects any event type;wherematches the whole event.
Correlation, partitions and windows
samecorrelates the guarded event with the other event: one JSONPath for both ($.call_id), a list, or a map{ <guarded path>: <other event path> }for different keys. A path that resolves nothing on either side is a violation, never vacuously equal.afterornot_afterwithoutsameloads with the warninginvariant.uncorrelated: one earlier event then covers every later guarded event.same: $.call_idbetween a guardedtool_calland anapprovalcorrelates by equalcall_id; else by the approval'stool_call_idequal to the tool call's eventid; else, only when the call has nocall_idand the approval carries neither id, by order: the nearest earlier approval for the same tool not already matched to an earlier guarded event, so one approval covers one call (vectors:spec/conformance/invariants/correlation.yaml).limit.bypartitions by a JSONPath; resolving nothing on the guarded event is a violation.limit.sumsums a numeric path; a non-number is a violation.limit.peris a window: an earlier event counts whennow - per <= at <= now, wherenowis the guarded event's time (the end is inclusive, so concurrent same-millisecond actions cannot overshoot a limit).scope(thread,session,actor,tenant; defaultthread) andeffect(deny,ask; defaultdeny) configure HITL. Evals ignores both.
Errors
parseInvariants reports every problem at once as a SpecError with code core.invariant_invalid, with file,
line and column when given a YAML document's locate. Issue codes: operator_missing, operator_ambiguous,
duplicate_name, required, unexpected, unknown_key, type, enum, selector_invalid, ok_not_allowed,
path_invalid, duration_invalid, glob_invalid, plus the matcher's own codes for args and where. Unknown
keys and enum values carry a suggestion when one is close.
// Every problem at once, with the line it was written on.
const broken = loadYaml(
`invariants:
- name: refund guard
each: { tool: refund }
limit: { max: 2, by: args.customer }
- name: refund guard
never: { tool: refund, args: { amount: { gt: 100 } } }
after: { tool: approve }
`,
{ file: "broken.yaml" },
);
try {
parseInvariants((broken.value as { invariants: unknown }).invariants, {
at: ["invariants"],
file: "broken.yaml",
locate: (path) => broken.locate(path),
});
} catch (e) {
for (const i of (e as SpecError).issues) console.log(`${i.file}:${i.line} ${i.path} ${i.code}`);
}# Every problem at once, with the line it was written on.
broken = load_yaml(
"""invariants:
- name: refund guard
each: { tool: refund }
limit: { max: 2, by: args.customer }
- name: refund guard
never: { tool: refund, args: { amount: { gt: 100 } } }
after: { tool: approve }
""",
file="broken.yaml",
)
try:
parse_invariants(broken.value["invariants"], at=["invariants"], file="broken.yaml", locate=broken.locate)
except SpecError as e:
for i in e.issues:
print(f"{i.file}:{i.line} {i.path} {i.code}")