Core

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.

main.ts
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}`);
}
main.py
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:

OperatorViolation
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? } selects tool_call events. tool is a name, a core glob (send_*) or a list; args is a matcher (no judge: an invariant must decide without a model); ok: true keeps calls without an error. HITL treats the action it is guarding as succeeding.
  • { approval: { tool?, status? } } selects approval events; statuses are allowed, approved, denied, expired, cancelled.
  • { event, where? } selects any event type; where matches the whole event.

Correlation, partitions and windows

  • same correlates 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. after or not_after without same loads with the warning invariant.uncorrelated: one earlier event then covers every later guarded event.
  • same: $.call_id between a guarded tool_call and an approval correlates by equal call_id; else by the approval's tool_call_id equal to the tool call's event id; else, only when the call has no call_id and 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.by partitions by a JSONPath; resolving nothing on the guarded event is a violation. limit.sum sums a numeric path; a non-number is a violation.
  • limit.per is a window: an earlier event counts when now - per <= at <= now, where now is the guarded event's time (the end is inclusive, so concurrent same-millisecond actions cannot overshoot a limit).
  • scope (thread, session, actor, tenant; default thread) and effect (deny, ask; default deny) 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.

main.ts
// 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}`);
}
main.py
# 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}")

On this page