guardrail-sim
byJeff Green

Policies

The building blocks of guardrail-sim

Policies

A policy is a named collection of rules that define what discounts are allowed in B2B pricing.

Policy Structure

interface Policy {
  id: string;
  name: string;
  rules: PolicyRule[];
}

Required Fields

  • id: Unique identifier for the policy
  • name: Human-readable name
  • rules: Array of rules to enforce

Policy Rules

Each rule uses the json-rules-engine format with conditions and events:

interface PolicyRule {
  name: string;
  conditions: {
    all?: RuleCondition[];
    any?: RuleCondition[];
  };
  event: {
    type: string;
    params?: Record<string, unknown>;
  };
  priority?: number;
}

Example Rule

const marginFloorRule: PolicyRule = {
  name: 'margin_floor',
  conditions: {
    all: [
      {
        fact: 'calculated_margin',
        operator: 'lessThan',
        value: 0.15
      }
    ]
  },
  event: {
    type: 'violation',
    params: {
      message: 'Calculated margin falls below 15% floor'
    }
  },
  priority: 1
};

Default Policy

The default policy includes three rules:

import { defaultPolicy } from '@guardrail-sim/policy-engine';
 
// Includes:
// - margin_floor: 15% minimum margin
// - max_discount: 25% maximum discount
// - volume_tier: 10% base, 15% for qty >= 100

Custom Policies

Create custom policies with your own rules:

import { PolicyEngine } from '@guardrail-sim/policy-engine';
import type { Policy } from '@guardrail-sim/policy-engine';
 
const holidayPolicy: Policy = {
  id: 'holiday-2026',
  name: 'Holiday Sale Policy',
  rules: [
    {
      name: 'holiday_max',
      conditions: {
        all: [
          { fact: 'proposed_discount', operator: 'greaterThan', value: 0.30 }
        ]
      },
      event: {
        type: 'violation',
        params: { message: 'Holiday discount cannot exceed 30%' }
      }
    }
  ]
};
 
const engine = new PolicyEngine(holidayPolicy);

Rule Conditions

Conditions use facts, operators, and values:

OperatorDescription
equalExact match
notEqualNot equal
lessThanLess than value
lessThanInclusiveLess than or equal
greaterThanGreater than value
greaterThanInclusiveGreater than or equal
inValue in array
notInValue not in array
containsArray contains value

Available Facts

The policy engine provides these facts for conditions:

FactDescription
order_valueTotal order value in dollars
quantityTotal units in order
product_marginBase margin (0.40 = 40%)
proposed_discountRequested discount (0.15 = 15%)
calculated_marginMargin after discount
customer_segmentCustomer tier (gold, silver, etc.)

Best Practices

  1. Name rules clearly: Use descriptive names like margin_floor not rule1
  2. Set priorities: Higher priority rules run first
  3. Provide messages: Include helpful violation messages
  4. Test thoroughly: Use multiple test scenarios

On this page