guardrail-sim
byJeff Green

Evaluation

How guardrail-sim processes policies

Evaluation

The evaluation process determines whether a discount request is allowed based on policy rules.

Evaluation Flow

Order → PolicyEngine → Rules → Facts → Result
  1. Input: An order with value, quantity, margin, and a proposed discount
  2. Fact Calculation: Engine computes facts like calculated margin
  3. Rule Evaluation: Each rule's conditions are checked against facts
  4. Violation Collection: Failed conditions generate violations
  5. Result: Approved/rejected with details

Basic Evaluation

import { PolicyEngine, defaultPolicy } from '@guardrail-sim/policy-engine';
import type { Order } from '@guardrail-sim/policy-engine';
 
const engine = new PolicyEngine(defaultPolicy);
 
const order: Order = {
  order_value: 5000,
  quantity: 100,
  product_margin: 0.4,
  customer_segment: 'gold'
};
 
const result = await engine.evaluate(order, 0.12);

Understanding Results

interface EvaluationResult {
  approved: boolean;           // Whether discount is allowed
  violations: Violation[];     // Rules that were violated
  applied_rules: string[];     // All rules that triggered
  calculated_margin: number;   // Final margin after discount
}

Approved Result

const result = await engine.evaluate(order, 0.12);
// {
//   approved: true,
//   violations: [],
//   applied_rules: ['margin_floor', 'max_discount', 'volume_tier'],
//   calculated_margin: 0.28
// }

Rejected Result

const result = await engine.evaluate(order, 0.30);
// {
//   approved: false,
//   violations: [
//     { rule: 'max_discount', message: 'Discount cannot exceed 25%' }
//   ],
//   applied_rules: ['max_discount'],
//   calculated_margin: 0.10
// }

How Rules Are Evaluated

Rules run in priority order (highest first). The engine:

  1. Computes all facts from the order and proposed discount
  2. Evaluates each rule's conditions against the facts
  3. Collects violations from failed conditions
  4. Returns approved=true only if no violations occurred

Computed Facts

The engine automatically calculates:

FactFormula
calculated_marginproduct_margin - proposed_discount

Rule Priority

const rules = [
  { name: 'margin_floor', priority: 1 },   // Runs first
  { name: 'max_discount', priority: 2 },   // Runs second
  { name: 'volume_tier', priority: 3 },    // Runs third
];

Multiple Violations

If multiple rules are violated, all violations are returned:

const result = await engine.evaluate(
  { order_value: 1000, quantity: 5, product_margin: 0.2 },
  0.25
);
// {
//   approved: false,
//   violations: [
//     { rule: 'margin_floor', message: 'Margin below 15%' },
//     { rule: 'volume_tier', message: 'Quantity must be >= 100 for 15%+ discount' }
//   ],
//   ...
// }

UCP Error Codes

Violations include UCP-compatible error codes:

import { getUCPErrorCode } from '@guardrail-sim/policy-engine';
 
for (const violation of result.violations) {
  const ucpCode = getUCPErrorCode(violation.rule);
  // 'discount_code_invalid', 'discount_code_user_ineligible', etc.
}

On this page