CSE Roadmap

Core tier · about 4 hours · 13 parts

Part 3 of 13 · 2 min read

Passing the schema is not the same as being safe to execute

A perfectly well-formed request can still be a request to do something that should never be allowed, and no schema can know that on its own.

Think of a withdrawal slip filled out flawlessly in every field, with a correct account number format, a reasonable-looking amount and a valid date. It is for an account that is not actually yours. The slip is syntactically perfect and semantically theft.

semantic validation checks that a value is acceptable given business state. Does this order belong to this customer, does the refund fit within what's left of the order total, does the referenced order even exist -- which requires an actual lookup against current state, something no amount of type-and-range checking on the argument alone can ever determine.

A worked example. Three refund requests all pass the strict schema with zero errors: a refund for ORD-555555, a refund of 400.0 on ORD-123456 (whose total is only 80.0), and a refund for the nonexistent ORD-777777. All three are abusive or impossible, and the schema -- which only checks that order_id matches a pattern and amount is a number in range -- has no way to know any of that. The semantic check catches all three: "order belongs to another customer," "refund exceeds order total," and "unknown order," respectively.

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

# from code/mini-guardrails/guardrails.py
def semantic_check(customer, args, orders=ORDERS):
    o = orders.get(args["order_id"])
    if o is None: return ["unknown order"]
    if o["customer"] != customer: return ["order belongs to another customer"]
    if o["refunded"] + args["amount"] > o["total"]: return ["refund exceeds order total"]
    return []

Read it as: every single check in this function reads from orders, a lookup against actual current state. There is no version of this check that could run against the argument alone. "Does this order belong to this customer" is a fact about the world at the moment of the call. It is not a property of the string "ORD-123456" itself. That is the structural reason semantic checks can never be replaced by a stricter schema.