Skip to main content

Deterministic execution guard for AI agents: request-id dedup + finality gating + durable state.

Project description

SafeAgent

Deterministic execution guard for AI agents.

pip install safeagent-exec-guard

Prevents duplicate, replayed, or premature irreversible actions triggered by LLM-based agents by enforcing:

  • request-id (nonce) deduplication
  • confidence / consensus thresholds before finality
  • deterministic state transitions
  • exactly-once settlement/execution semantics
  • durable state persistence (SQLite)

This repository is a reference implementation and pattern demo (not a hosted service).


Install


Exactly-once tool execution

from settlement.settlement_requests import SettlementRequestRegistry

registry = SettlementRequestRegistry()

def send_email():
    print("SENT EMAIL")

receipt = registry.execute(
    request_id="email:C123:invoice",
    action="send_email",
    payload={"to": "c123@example.com", "template": "invoice"},
    execute_fn=send_email,
)

print(receipt)

If the same request_id is replayed, SafeAgent returns the original receipt instead of executing the side effect again.

pip install safeagent-exec-guard

Requires Python 3.10+.


Why SafeAgent?

LLM agents frequently retry tool calls or replay events when something fails.

Without a guard layer, this can cause duplicate execution of irreversible actions (tickets, emails, payouts, trades).

Without SafeAgent

create_support_ticket(customer_id="C123", severity="high")
create_support_ticket(customer_id="C123", severity="high")  # duplicate

With SafeAgent (exactly-once execution)

from settlement.settlement_requests import SettlementRequestRegistry

registry = SettlementRequestRegistry()

receipt = registry.execute(
    request_id="agent_action_123",
    action="create_support_ticket",
    payload={"customer_id": "C123", "severity": "high"}
)

print(receipt)

If the agent retries the same request_id, SafeAgent returns the original receipt instead of executing again.


Quick Example

from settlement.models import Case
from settlement.store import SQLiteStore

store = SQLiteStore("safeagent.db")
case = Case(case_id="example_case")
store.put_case(case)

print("SafeAgent initialized:", case.state)

What problem does this solve?

AI agents running in production often:

  • retry tool calls on partial failure
  • replay webhook events
  • execute the same action twice
  • act on provisional/ambiguous outcomes
  • loop under uncertainty and produce conflicting signals

When agents touch real systems (tickets, emails, DB writes, payouts, trades), duplicate execution becomes expensive.

SafeAgent provides a control-plane pattern that sits between an agent decision and an irreversible action and only allows execution when the outcome is deterministically FINAL.


High-level flow

Agent / Outcome Signals
→ Reconciliation / Consensus
→ Finality Gate
→ Execution (exactly-once)
→ Audit / Receipt


State machine

OPEN
→ RESOLVED_PROVISIONAL
→ IN_RECONCILIATION
→ FINAL
→ SETTLED

  • Ambiguous signals transition to IN_RECONCILIATION
  • Execution is impossible unless state is FINAL
  • Execution is replay-safe (idempotent)
  • Late signals are ignored after finality

Key features

Durable persistence (SQLiteStore)

  • Case state and signals can be persisted to SQLite
  • State survives restarts
  • ACID durability for single-node safety

Request-id (nonce) deduplication

  • Settlement/execution attempts require a unique request_id
  • Replays using the same request_id return the cached result
  • New request_ids after settlement resolve to the same settlement_id
  • Prevents duplicate effects across retries or multiple actors

Confidence / consensus threshold policy

  • Auto-finalize outcomes when agreement exceeds a threshold (e.g. 80%)
  • Falls back to majority decision when threshold is not met (in demo)

Demos

1) SafeAgent demo (duplicate execution prevention)

python examples/safe_agent_demo.py

2) AI outcome simulation (stochastic agent signals)

python examples/simulate_ai.py

3) Persistence demo (prove restart safety)

Run twice:

python examples/persist_demo.py
python examples/persist_demo.py

Core implementation structure

models.py                      case + signal models
state_machine.py               deterministic transitions
reconciliation.py              conflict detection & resolution
gate.py                        settlement/execution gate
store.py                       in-memory + SQLite persistence

policy.py                      confidence/threshold decision helper
settlement_requests.py         request-id (nonce) dedup wrapper

examples/safe_agent_demo.py            SafeAgent demo
examples/simulate_ai.py                AI demo
examples/persist_demo.py               persistence demo
examples/nonce_demo.py                 nonce demo

Need this in production?

If you’re implementing this pattern in a production agent system and want help tailoring reconciliation rules, execution receipts, or persistence/concurrency strategy, see LICENSING.md.


License

Apache-2.0

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

safeagent_exec_guard-0.1.6.tar.gz (23.7 kB view details)

Uploaded Source

Built Distribution

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

safeagent_exec_guard-0.1.6-py3-none-any.whl (21.9 kB view details)

Uploaded Python 3

File details

Details for the file safeagent_exec_guard-0.1.6.tar.gz.

File metadata

  • Download URL: safeagent_exec_guard-0.1.6.tar.gz
  • Upload date:
  • Size: 23.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.10

File hashes

Hashes for safeagent_exec_guard-0.1.6.tar.gz
Algorithm Hash digest
SHA256 70de23ccd0f252ec73a761109732fd7960233b003079f76af639546841f71421
MD5 fe677b0735a471e5a1252a955f6021ba
BLAKE2b-256 36e7f3895b305736ae6066ef4c281bf03cbd0983e2b07f9094ec5a95c5692926

See more details on using hashes here.

File details

Details for the file safeagent_exec_guard-0.1.6-py3-none-any.whl.

File metadata

File hashes

Hashes for safeagent_exec_guard-0.1.6-py3-none-any.whl
Algorithm Hash digest
SHA256 623c692cbc09937c4e2d675eb64efeb1c54edf9a4c82f85592f56cd8ab1dc46b
MD5 28a23f0ad460d53174a1c33c87b57bf7
BLAKE2b-256 e81219539017af873bb16a39e93f9491b1ce5a8a629912df5c22ba32f4b4f016

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