Skip to main content

Basic Refund Example

This example demonstrates the Actra refund policy workflow in both Python and JavaScript.

It is the ideal first example because it teaches the complete mental model:

  • define schema
  • write a blocking rule
  • compile the policy
  • attach runtime resolvers
  • protect a function
  • validate allow vs block behavior

The business rule is simple:

Refunds greater than 1000 are blocked.


What This Example Teaches

  • schema-driven action validation
  • actor resolver usage
  • snapshot resolver usage
  • runtime admission control
  • policy exceptions
  • testing expected blocks
  • SDK parity across languages

Example

"""
Basic Actra Example

This example shows how Actra can enforce admission
control policies on normal Python functions.
"""

from actra import Actra, ActraPolicyError
from actra.runtime import ActraRuntime


schema_yaml = """
version: 1

actions:
refund:
fields:
amount: number

actor:
fields:
role: string

snapshot:
fields:
fraud_flag: boolean
"""

policy_yaml = """
version: 1

rules:
- id: block_large_refund
scope:
action: refund
when:
subject:
domain: action
field: amount
operator: greater_than
value:
literal: 1000
effect: block
"""

policy = Actra.from_strings(schema_yaml, policy_yaml)
runtime = ActraRuntime(policy)

runtime.set_actor_resolver(lambda ctx: {"role": "support"})
runtime.set_snapshot_resolver(lambda ctx: {"fraud_flag": False})


@runtime.admit()
def refund(amount: int):
print("Refund executed:", amount)


print("\n--- Allowed call ---")
refund(amount=200)

print("\n--- Blocked call ---")
try:
refund(amount=1500)
except ActraPolicyError as e:
print("Refund blocked by policy")
print("Rule:", e.matched_rule)

Expected Output

Allowed

Refund executed: 200

Blocked

Refund blocked by policy
Rule: block_large_refund

Why This Example Matters

This example introduces the most important Actra runtime guarantee:

Policy is evaluated BEFORE business execution

That means blocked calls never reach your application logic.

This is the foundation for:

  • refund controls
  • deploy gates
  • destructive tool safety
  • finance approvals
  • agent tool governance