Enterprise AI Tool Contract Lab
By Thomas Thelliez
https://thomasthelliez.com/blog/resources/enterprise-ai-tool-contract-lab/

Requirements: Python 3.9 or newer. Standard library only.
No package installation, API keys, network connection or GitHub account needed.

Run the checks:
  python3 tool_contract_lab.py --test

Inspect one execution followed by a replay:
  python3 tool_contract_lab.py --demo

Files:
  tool_contract_lab.py  In-memory gateway, demo and 13 executable tests.
  contract.json         Documented argument shape and authority/replay rules.

The principal and approval records are injected by the trusted application
boundary in this example. They are not accepted from model arguments.
The fixed argument shape is validated explicitly in Python. contract.json
documents it using JSON Schema; this is not a general schema implementation.

A replay checks current authority and returns the saved result without
executing again. It does not require a fresh approval because it performs
no new business write. A previously unused request requires a matching,
unexpired approval. An approval is consumed by its first new execution;
it cannot authorize another write under a different request key.

The lock covers the fake business mutation and its replay record within
one process. Restarting the process loses that record. A real downstream
API introduces crash windows and uncertain outcomes that this example
does not resolve. Production needs durable idempotency records, downstream
idempotency or reconciliation, and appropriately protected approval/audit
services. Do not present the sample as an exactly-once production gateway.
