Skip to main content

Deterministic validation layer for AI agents and autonomous systems

Project description

agentguard-trustlayer

AgentGuard-TrustLayer is a runtime safety layer that prevents AI agents from taking invalid or unsafe actions—even when they try.

Prevents AI agents from executing invalid or unsafe actions before they happen.


Why this exists

AI agents can generate actions.

But they don't understand consequences.

Without a validation layer:

  • they can break invariants
  • corrupt system state
  • execute invalid operations

agentguard-trustlayer sits between AI and execution.

It ensures:

  • every action is checked
  • every rule is enforced
  • every failure is contained

Core Idea

agentguard-trustlayer separates:

decision-making (AI) from execution (validated system)


How it works

AI Agent  -->  Proposal  -->  TrustLayer  -->  Execution
                                   ^
                              Constraints

Every update passes through four gates:

  1. Auth — is the token valid and unexpired?
  2. Locks — is the target key frozen?
  3. Constraints — does the new state pass all rules?
  4. Rollback — if anything fails, state is fully restored

Features

  • Constraint-based validation with composable logic (&, |, ~)
  • Delta-aware constraints — rules can compare proposed vs original state
  • Authenticated authority (HMAC-signed tokens with TTL)
  • Safe state updates with automatic rollback
  • set, increment, and update action types
  • Async agent loop with retry, backoff, and error feedback to model
  • Tamper-evident audit chain — every ValidationEvent carries a SHA-256 hash linked to the previous event
  • GuardedAgent high-level API — one object, one call
  • Zero dependencies (standard library only)

Practical Use Cases

  • Prevent AI agents from breaking business rules
  • Enforce invariants in automated systems
  • Add a safety layer to LLM workflows
  • Control multi-agent environments with authority levels

Quick Start

Install:

pip install trustlayer-py

Or clone and run a demo:

git clone https://github.com/AILIFE1/agentguard-trustlayer
cd agentguard-trustlayer
python examples/demo.py

🔥 Try to break the agent

python examples/demo_break_the_agent.py

An agent tries to set balance = 1,000,000. TrustLayer blocks it. The error is fed back into the prompt. The agent self-corrects and increments safely instead.

[MODEL OUTPUT] Attempting INVALID action...

[MODEL INPUT]
Increase balance as much as possible
Last error: balance <= max_limit

[MODEL OUTPUT] Attempting SAFE action...

FINAL STATE
{'balance': 110, 'max_limit': 200}

RESULT
[OK] Increase balance as much as possible

GuardedAgent — one-liner setup

import asyncio, json
from trustlayer import GuardedAgent, LambdaConstraint

async def my_model(prompt: str) -> str:
    return json.dumps({"type": "set", "target": "score", "value": 75})

agent = GuardedAgent(
    model=my_model,
    rules=[LambdaConstraint("score 0-100", lambda v: 0 <= v.get("score", 0) <= 100)],
    initial_state={"score": 50},
)

result = asyncio.run(agent.run("raise the score"))
print(result)
# {'status': 'success', 'state': {'score': 75}, 'audit': '<sha256>'}

Full API example

import asyncio, json
from trustlayer import (
    Agent, AuthorityLevel, AuthToken, Cathedral,
    LambdaConstraint, RetryConfig, State, Validator,
)

SECRET = b"my-secret"

score_ok = LambdaConstraint("score_ok", lambda v: 0 <= v.get("score", 0) <= 100)

state     = State(values={"score": 50})
validator = Validator(state, [score_ok], SECRET)
token     = AuthToken.issue(AuthorityLevel.SYSTEM, "agent", ttl_seconds=60, secret=SECRET)

async def model(prompt: str) -> str:
    return json.dumps({"type": "set", "target": "score", "value": 75})

async def main():
    cathedral = Cathedral(validator, Agent(model), retry=RetryConfig(max_attempts=3))
    event = await cathedral.step("raise the score", token)
    print(event)           # [OK] raise the score
    print(event.audit_hash)  # sha256 chain link
    print(state.values)    # {'score': 75}

asyncio.run(main())

Project Structure

agentguard-trustlayer/
├── trustlayer/
│   ├── __init__.py       # Public API + logging setup
│   ├── auth.py           # AuthToken, AuthorityLevel
│   ├── constraints.py    # Constraint, LambdaConstraint, And/Or/Not
│   ├── types.py          # State, Action, Update
│   ├── validator.py      # Validator, ValidationEvent, audit chain
│   └── engine.py         # Agent, Cathedral, GuardedAgent, RetryConfig
└── examples/
    ├── demo.py                    # Basic walkthrough
    └── demo_break_the_agent.py    # Constraint enforcement + self-correction

Philosophy

agentguard-trustlayer doesn't make decisions — it decides whether decisions are allowed.


License

MIT

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

trustlayer_py-2.1.0.tar.gz (12.3 kB view details)

Uploaded Source

Built Distribution

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

trustlayer_py-2.1.0-py3-none-any.whl (11.9 kB view details)

Uploaded Python 3

File details

Details for the file trustlayer_py-2.1.0.tar.gz.

File metadata

  • Download URL: trustlayer_py-2.1.0.tar.gz
  • Upload date:
  • Size: 12.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.11

File hashes

Hashes for trustlayer_py-2.1.0.tar.gz
Algorithm Hash digest
SHA256 3b003287a8ff13dc99b57348cc45a9e4c5ef99f7c24aacef3611ff0951cde319
MD5 8728755ff49e9854ce384e6e08253182
BLAKE2b-256 9f570c2b287a9586f7e6e372aa868bcd736ca8a8df431a1b4f1b6bbb9b7e2c56

See more details on using hashes here.

File details

Details for the file trustlayer_py-2.1.0-py3-none-any.whl.

File metadata

  • Download URL: trustlayer_py-2.1.0-py3-none-any.whl
  • Upload date:
  • Size: 11.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.11

File hashes

Hashes for trustlayer_py-2.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 98a9157236d4a020a9ba8e93c7ccdc5804d66ecae2e41b61ab1b787c64a7989a
MD5 0b1aa01c254068387c1d86b60c4fce8f
BLAKE2b-256 9e1cd37d54974e5ca1892a5d0e9537d07e0590450cea1b90f5ac542a0607f0c5

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