Skip to main content

Runtime safety guardrails for Sardis agent payments: circuit breakers, kill switches, rate limiting, and behavioral monitoring

Project description

Sardis Guardrails

Runtime safety guardrails for the Sardis agent payment platform. Provides circuit breakers, kill switches, rate limiting, input validation, and behavioral monitoring to ensure safe and reliable agent payment execution.

Features

  • Circuit Breaker: Automatic failure detection and service protection
  • Kill Switch: Emergency stop mechanisms (global, per-org, per-agent)
  • Rate Limiting: Token bucket algorithm with sliding windows
  • Input Validation: Comprehensive validation and sanitization
  • Behavioral Monitoring: Anomaly detection based on spending patterns

Installation

uv pip install sardis-guardrails

Quick Start

Circuit Breaker

Protect payment operations from cascading failures:

from decimal import Decimal
from sardis_guardrails import CircuitBreaker, CircuitBreakerError

# Create circuit breaker for an agent
breaker = CircuitBreaker(agent_id="agent-123")

# Protect a payment function
@breaker.protected
async def make_payment(amount: Decimal) -> str:
    # Payment logic here
    return transaction_hash

# Use it
try:
    tx_hash = await make_payment(Decimal("100.00"))
except CircuitBreakerError:
    # Circuit is open, reject request
    print("Service temporarily unavailable")

Kill Switch

Emergency stop for critical situations:

from sardis_guardrails import get_kill_switch, ActivationReason, KillSwitchError

# Get global kill switch instance
kill_switch = get_kill_switch()

# Activate for an agent
await kill_switch.activate_agent(
    agent_id="agent-123",
    reason=ActivationReason.FRAUD,
    activated_by="security-team",
    notes="Suspicious activity detected"
)

# Check before payment execution
try:
    await kill_switch.check(agent_id="agent-123", org_id="org-456")
    # Proceed with payment
except KillSwitchError as e:
    # Kill switch active, block payment
    print(f"Payment blocked: {e}")

Rate Limiting

Prevent excessive transaction volume:

from decimal import Decimal
from sardis_guardrails import RateLimiter, RateLimitError

# Create rate limiter for an agent
limiter = RateLimiter(agent_id="agent-123")

# Configure limits
limiter.add_limit(
    name="per_minute",
    max_transactions=10,
    window_seconds=60.0,
    max_amount=Decimal("1000.00")
)

limiter.add_limit(
    name="per_hour",
    max_transactions=100,
    window_seconds=3600.0,
    max_amount=Decimal("10000.00")
)

# Check before transaction
try:
    await limiter.check_all_limits(amount=Decimal("50.00"))
    # Proceed with payment
except RateLimitError as e:
    print(f"Rate limit exceeded: {e}")

Input Validation

Validate and sanitize payment inputs:

from decimal import Decimal
from sardis_guardrails import (
    PaymentInputValidator,
    ValidationError,
    WalletAddressValidator,
    AmountValidator
)

# Comprehensive validation
try:
    validator = PaymentInputValidator(
        recipient_address="0x742d35Cc6634C0532925a3b844Bc9e7595f0bEb",
        amount=Decimal("100.00"),
        token="USDC",
        chain="BASE",
        merchant_name="Example Corp",
        purpose="Payment for services"
    )
    validator.validate_full()
except ValidationError as e:
    print(f"Invalid input: {e}")

# Validate individual components
try:
    address = WalletAddressValidator.validate_ethereum_address(
        "0x742d35Cc6634C0532925a3b844Bc9e7595f0bEb"
    )
    amount = AmountValidator.validate_amount(
        amount=Decimal("100.00"),
        token="USDC",
        min_amount=Decimal("0.01"),
        max_amount=Decimal("1000000.00")
    )
except ValidationError as e:
    print(f"Validation failed: {e}")

Behavioral Monitoring

Detect anomalous spending patterns:

from decimal import Decimal
from sardis_guardrails import (
    BehavioralMonitor,
    TransactionData,
    SensitivityLevel,
    AlertSeverity
)

# Create monitor for an agent
monitor = BehavioralMonitor(
    agent_id="agent-123",
    sensitivity=SensitivityLevel.NORMAL
)

# Record normal transactions to build baseline
for _ in range(20):
    await monitor.record_transaction(
        TransactionData(
            amount=Decimal("100.00"),
            merchant="Example Corp",
            token="USDC",
            chain="BASE"
        )
    )

# Check suspicious transaction
alerts = await monitor.check_transaction(
    TransactionData(
        amount=Decimal("10000.00"),  # Unusually large
        merchant="Example Corp",
        token="USDC",
        chain="BASE"
    )
)

for alert in alerts:
    print(f"[{alert.severity}] {alert.description}")
    if alert.severity == AlertSeverity.CRITICAL:
        # Take action (e.g., activate kill switch)
        pass

Integration Example

Combining all guardrails in a payment flow:

from decimal import Decimal
from sardis_guardrails import (
    CircuitBreaker,
    get_kill_switch,
    RateLimiter,
    PaymentInputValidator,
    BehavioralMonitor,
    TransactionData,
    ActivationReason,
    AlertSeverity
)

async def execute_payment(
    agent_id: str,
    org_id: str,
    recipient: str,
    amount: Decimal,
    token: str,
    chain: str,
    merchant: str,
    purpose: str
):
    # 1. Input validation
    validator = PaymentInputValidator(
        recipient_address=recipient,
        amount=amount,
        token=token,
        chain=chain,
        merchant_name=merchant,
        purpose=purpose
    )
    validator.validate_full()

    # 2. Kill switch check
    kill_switch = get_kill_switch()
    await kill_switch.check(agent_id=agent_id, org_id=org_id)

    # 3. Rate limiting
    limiter = RateLimiter(agent_id=agent_id)
    limiter.add_limit("per_minute", 10, 60.0, Decimal("1000.00"))
    await limiter.check_all_limits(amount)

    # 4. Behavioral check
    monitor = BehavioralMonitor(agent_id=agent_id)
    tx_data = TransactionData(
        amount=amount,
        merchant=merchant,
        token=token,
        chain=chain
    )

    alerts = await monitor.check_transaction(tx_data)
    for alert in alerts:
        if alert.severity == AlertSeverity.CRITICAL:
            # Auto-activate kill switch on critical anomaly
            await kill_switch.activate_agent(
                agent_id=agent_id,
                reason=ActivationReason.ANOMALY,
                notes=alert.description
            )
            raise Exception(f"Payment blocked: {alert.description}")

    # 5. Execute with circuit breaker
    breaker = CircuitBreaker(agent_id=agent_id)

    @breaker.protected
    async def _execute():
        # Actual payment logic here
        tx_hash = await execute_blockchain_transaction(...)

        # Record successful transaction
        await monitor.record_transaction(tx_data)

        return tx_hash

    return await _execute()

Configuration

Circuit Breaker

from sardis_guardrails import CircuitBreakerConfig

config = CircuitBreakerConfig(
    failure_threshold=5,        # Failures before tripping
    reset_timeout=60.0,         # Seconds before retry
    half_open_max_calls=3,      # Test calls in half-open
    success_threshold=2         # Successes to close
)

breaker = CircuitBreaker(agent_id="agent-123", config=config)

Sensitivity Levels

  • RELAXED: 3.0 sigma threshold (fewer alerts)
  • NORMAL: 2.5 sigma threshold (recommended)
  • STRICT: 2.0 sigma threshold (more alerts)
  • PARANOID: 1.5 sigma threshold (maximum alerts)

License

MIT License - see LICENSE file for details.

Links

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

sardis_guardrails-1.1.0.tar.gz (60.1 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

sardis_guardrails-1.1.0-py3-none-any.whl (62.2 kB view details)

Uploaded Python 3

File details

Details for the file sardis_guardrails-1.1.0.tar.gz.

File metadata

  • Download URL: sardis_guardrails-1.1.0.tar.gz
  • Upload date:
  • Size: 60.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.9.22 {"installer":{"name":"uv","version":"0.9.22","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"macOS","version":null,"id":null,"libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

File hashes

Hashes for sardis_guardrails-1.1.0.tar.gz
Algorithm Hash digest
SHA256 9fe5f5f83f5d9692d43c84bc4fa55742b72a6eb98f7f8cef467b4f8328900fc7
MD5 0db3bb723c6cb1269a6db53ca58500db
BLAKE2b-256 3e2c94bb290d464492082082ca86c8427161d6fe48e0594bc19b2bce29b81c33

See more details on using hashes here.

File details

Details for the file sardis_guardrails-1.1.0-py3-none-any.whl.

File metadata

  • Download URL: sardis_guardrails-1.1.0-py3-none-any.whl
  • Upload date:
  • Size: 62.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.9.22 {"installer":{"name":"uv","version":"0.9.22","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"macOS","version":null,"id":null,"libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

File hashes

Hashes for sardis_guardrails-1.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 6808090ab99041e49466928b4870c0ca3d5e42c7d9f6738a162331f4a02ae072
MD5 bb18e135f8f87c72ef8b9f6e5b066505
BLAKE2b-256 9371f4a22c1ca682c6373edbb7ab3b0ee183698bd67b7fe98debd2cb9f4db154

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page