Project: authorize and deduplicate approved tool execution
Combine trusted tool policy, tenant boundaries, exact proposal approval and replay protection. A model can propose a call, but the executor must decide whether it may run. A reused request identifier with changed arguments must fail rather than borrowing an earlier approval or result.
This local project executes a supplied callable only after those checks and records successful results. A failed operation is not cached. The review should explain durable idempotency, concurrent duplicate calls, approval expiry and the uncertain window when an external effect succeeds but recording its result fails. A local dictionary cannot erase that distributed failure window.
import hashlib,json
proposal={"tool":"save","args":{"value":2}}
identity=hashlib.sha256(json.dumps(proposal,sort_keys=True,separators=(",",":")).encode()).hexdigest()
print(len(identity))Review the design
The in-memory executor proves policy composition under sequential ownership. Production effects require durable receipts, concurrency control, trusted approval records and a recovery protocol for uncertain external outcomes.
Your task
Implement execute_approved(call,tenant,scopes,policy,approved_digest,receipts,perform). call={id,tenant,tool,args}; policy maps tool to required scope. Validate authority first. Identity is SHA256 of compact sorted ensure_ascii=False allow_nan=False JSON {tool,args}. Require matching approved_digest with compare_digest. receipts maps id to (identity,result). Identical replay returns cached result; changed replay raises ValueError. Cache only successful perform(tool,args) results.
The explanation opens here once every check passes.