WRITE · TRACE · UNDERSTAND

Project: validate, authorize and replay a dependency-aware tool plan

Implement the closed local tools read(key) and set(key,JSON scalar value) on state.records[tenant].

Read lesson: Project: validate, authorize and replay a dependency-aware tool plan →

Your experiment

Implement the closed local tools read(key) and set(key,JSON scalar value) on state.records[tenant]. Each plan operation has exactly id,tool,args,deps; IDs and dependencies are unique, all dependencies exist, no cycles. Prevalidate all schemas and current scopes (records:read/write), byte-limit canonical operation payloads to 4096, and require set approval membership. Approval identity is SHA256(canonical JSON {'tenant':tenant,**op}, sorted keys, compact separators, allow_nan=False); approved is a trusted controller-provided allowlist, not a secret or authority created by hashing. Topologically choose the lexicographically smallest ready ID. Verify tenant-scoped receipt fingerprints before reuse. Budget counts new operations only; reject before effects. Execute on a deep copy, return state,order,outputs,executed; exceptions preserve original state. Receipts store fingerprint,result by tenant and operation ID.

Python runs in your browser. Your draft stays on this device.

What to look for

Review trusted approval provenance, current permissions, dependency validation, replay conflicts and transactional publication. Remote tools need their own durable idempotency and uncertain-outcome recovery. Replay follows the main orchestration function; helper computations execute and are checked separately.

Watch your code work

Ready to experiment

Write a solution or load the worked answer, then choose Trace & check.

Step through arrays, variables and training curves produced by your own Python.

About this algorithm

How Project: validate, authorize and replay a dependency-aware tool plan works

The idea

A plan written by a model is untrusted input: a list of tool operations with dependencies between them. Running it safely means deciding everything before any data changes. execute_plan validates the shape of every operation, checks the caller's scopes, and requires that each set was approved by a trusted controller. Approval is matched by a SHA-256 fingerprint of the canonical JSON of the tenant plus the operation, so approval of one exact write cannot be reused for a different one.

Only after that does it order the operations by their deps, check the step budget, and execute on a deep copy of state. Each result is stored as a receipt, so replaying the same plan returns the stored results instead of running the tools again.

How it works

  1. Validate each operation: exactly id, tool, args and deps, a known tool, exact argument keys, unique dependencies that exist.
  2. Serialise {'tenant': tenant, **op} as canonical JSON, enforce the 4096-byte limit, and hash it into a fingerprint.
  3. Require records:read or records:write in scopes, and for every set require its fingerprint in approved.
  4. Build order by repeatedly taking the smallest ID whose dependencies are all done, raising ValueError on a cycle.
  5. On a deep copy, compare stored receipts with the fingerprints, count the new operations and reject the plan if they exceed max_steps.
  6. Run the new operations in order, reuse receipts for the rest, and return state, order, outputs and executed.

What it costs

Scales with
plan size, state copy
Ordering
O(n²) in this answer
New effects
at most max_steps

The worked answer rescans the pending operations each round to find the smallest ready ID, which is quadratic but fine for short plans, and the deep copy costs the size of state.

Where it is used

  • Stripe's API accepts an Idempotency-Key header and returns the stored result when the same request is retried.
  • Terraform separates plan from apply, and applying a saved plan file carries out exactly the changes that were reviewed.
  • GitHub Actions orders jobs by their needs dependencies and can hold a job until a required reviewer approves its environment.

Watch out for

  • Executing operations while later ones are still being validated, so a failure halfway leaves state partly changed.
  • Hashing JSON without sorted keys and fixed separators, so the same operation gets different fingerprints and approvals stop matching.
  • Treating the hash as permission: a fingerprint only identifies an operation, and authority comes from the trusted approved list and the current scopes.