DEV Community

Mike W
Mike W

Posted on

TrustLayer: A Deterministic Validation Layer for AI Agents

AI agents can generate actions. But they do not understand consequences.

Without a validation layer, an agent can break invariants, corrupt system state, or execute operations it was never supposed to run.

TrustLayer sits between the agent and execution. Every action is checked before it happens.

How it works

AI Agent --> Proposal --> TrustLayer --> Execution
                              ^
                         Constraints
Enter fullscreen mode Exit fullscreen mode

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

Quick example

The agent tries to set C = 100. The system enforces C = B + 5. TrustLayer rejects the action before any state changes. The agent retries with C = 25. Accepted.

--- Agent Attempt 1 ---
Goal: Force C = 100
REJECTED: Would break constraint (C must equal B + 5)
System prevented invalid state.

--- Agent Attempt 2 ---
Adjusting strategy...
ACCEPTED: State remains consistent
Final State: {A: 10, B: 20, C: 25}
Enter fullscreen mode Exit fullscreen mode

Code

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):
    return json.dumps({'type': 'update', '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(state.values)

asyncio.run(main())
Enter fullscreen mode Exit fullscreen mode

Features

  • Constraint-based validation with composable logic
  • HMAC-signed authority tokens with TTL
  • Atomic rollback on any failure
  • Async agent loop with exponential backoff retry
  • Zero dependencies

Run it

git clone https://github.com/AILIFE1/trustlayer
python examples/demo.py
Enter fullscreen mode Exit fullscreen mode

GitHub: https://github.com/AILIFE1/trustlayer

Top comments (0)