Skip to content
Sefra

Solution: finance operations

Procure to pay, as one plan

An agent onboards a vendor, raises a purchase order and pays a deposit as one plan. A person approves that exact plan. If the payment is refused, the order and the vendor are undone through the ERP's own API.

The vendor and order rules below come from the starter procurement library, which you copy and adapt. It has no payment intent, so the deposit step is an example of one you would write for your own system. The values are illustrative.

The plan

Three steps in a fixed order. The order's vendor and the deposit's order are references to the output of an earlier step, so no model runs between steps.

StepIntentWay back
vendorprocurement.create_vendorDeactivate the vendor
orderprocurement.create_purchase_orderCancel the order
depositprocurement.pay_depositNone. Marked irreversible, so it must run last
The plan as the agent submits it
{ "steps": [    { "id": "vendor", "intent": "procurement.create_vendor",      "input": { "legal_name": "Northwind Supplies GmbH", "country": "DE", "tax_id": "DE811907980" } },    { "id": "order", "intent": "procurement.create_purchase_order",      "input": { "vendor_id": { "$ref": "steps.vendor.output.vendor_id" }, "sku": "LAPTOP-14",                 "quantity": 10, "unit_price_minor": 120000, "currency": "EUR", "department": "it-ops" } },    { "id": "deposit", "intent": "procurement.pay_deposit",      "input": { "purchase_order_id": { "$ref": "steps.order.output.po_id" },                 "amount_minor": 360000, "currency": "EUR" } } ],  "idempotency_key": "onboard-northwind-2c9d4e7a51f0", "rationale": "New laptop supplier, 30% deposit agreed." }

Scroll sideways for long lines.

The rule and the threshold

The numbers are settings at the top of the starter policy, written in minor units. Change them first. The tests that name each setting show what changed.

The starter's thresholds
approval_threshold_minor := {"EUR": 2000000, "USD": 2000000} # 20,000.00 second_approval_threshold_minor := {"EUR": 10000000, "USD": 10000000} # 100,000.00

Scroll sideways for long lines.

RuleEffectFires when
procurement.large_orderApprovalOrder total of at least 2000000 minor units (20,000.00): a finance_approver
procurement.very_large_orderApprovalOrder total of at least 10000000 minor units (100,000.00): a procurement_head as well
procurement.new_vendorApprovalEvery new vendor: a finance_approver
procurement.irreversible_requires_approvalApprovalAny intent marked irreversible: a finance_approver
procurement.budget_exceededDenyQuantity times unit price is above the department's remaining budget
procurement.vendor_inactiveDenyThe vendor does not exist or is not active
procurement.vendor_existsDenyA vendor with this tax id already exists, active or not
procurement.duplicateDenyThe same write by the same principal was accepted in the last 24 hours and did not end rejected, failed or rolled back
The threshold as a rule
# procurement.large_order: a finance approver, not the requester, approves# orders from the first threshold.require_approval contains {  "rule": "procurement.large_order",  "message": sprintf("Order total %d minor units is at least %d.", [order_total, approval_threshold_minor[input.input.currency]]),  "approver": {"role": approver_role},  "distinct_from_requester": true,} if {  is_order  order_total >= approval_threshold_minor[input.input.currency]}

Scroll sideways for long lines.

In the worked example the order total is 1200000, below the threshold. The deposit is what asks for the approver, through procurement.irreversible_requires_approval. With the starter's new-vendor rule, the vendor step also asks for a finance_approver. Requirements across a plan's steps are merged by role, so the plan gets one approval.

What the approver sees

One line per step, built from typed fields, with the full inputs, the rule that asked, and the requester. The agent's rationale is labelled as unverified.

The approval, as the approver reads it

Summary

  1. Create vendor: Northwind Supplies GmbH, DE
  2. Create purchase order: 10 x LAPTOP-14 from (output of step vendor), unit price 120000 EUR (minor units), department it-ops
  3. Pay deposit: 360000 EUR (minor units) for purchase order (output of step order)
Rule
procurement.irreversible_requires_approval
Requester
agent:procurement-bot for user:u_jonas
Required role
finance_approver
Step inputs
shown in full; left out of this drawing
Expires
24 hours after creation
Execution hash
sha256:c41e…

Agent-supplied, unverified

New laptop supplier, 30% deposit agreed.

The decision carries back the hash of what was shown, so a changed plan cannot use it. The approver cannot be the user the agent acts for. Approval does not override policy: each step is evaluated again when it runs, on fresh facts.

What is undone when the payment is refused

The ERP refuses the deposit with a 422. Two steps took effect, so Sefra calls their compensations in reverse order. No model is consulted.

The execution's timeline
  1. Received

    • From agent:procurement-bot on behalf of user:u_jonas, a plan of three steps.execution.received#4120
  2. Policy at submission, held for a person

    • procurement.irreversible_requires_approval asks for a finance_approver. Effect require_approval.policy.evaluated#4123
  3. Approval, allowed

    • user:u_maria, finance_approver, decided approve.approval.decided#4127
  4. Step vendor, allowed

    • POST /vendors, response 201.step.succeeded#4130
  5. Step order, allowed

    • POST /purchase-orders, response 201.step.succeeded#4133
  6. Step deposit, denied or failed

    • POST /payments, response 422, code DOWNSTREAM_REJECTED.step.failed#4136
  7. Compensation

    • Undo of step order succeeded, attempt 2, response 200.compensation.succeeded#4139
    • Undo of step vendor succeeded, attempt 1, response 200.compensation.succeeded#4141
  8. Completed

    • Final status rolled_back with DOWNSTREAM_REJECTED.execution.completed#4142
What the agent receives
{  "execution_id": "exe_…B",  "status": "rolled_back",  "replayed": false,  "steps": [    { "id": "vendor",  "intent": "procurement.create_vendor",         "status": "compensated" },    { "id": "order",   "intent": "procurement.create_purchase_order", "status": "compensated" },    { "id": "deposit", "intent": "procurement.pay_deposit",           "status": "failed" }  ],  "error": {    "code": "DOWNSTREAM_REJECTED", "stage": "execution", "step": "deposit",    "message": "The system of record refused the operation.",    "violations": [ { "rule": "downstream.rejected", "message": "The system of record refused the operation.", "actual": 422 } ],    "retryable": false  }}

Scroll sideways for long lines.

Afterwards the vendor exists and is inactive, the order exists and is cancelled, no payment exists, and the budget is back. The systems are not as if nothing had happened: both records remain, with their own history. If the payment's answer is lost rather than refused, nothing is undone until a person has checked whether it was paid.

What the controller sees afterwards

One execution's events, in order, on the deployment's hash chain, readable without asking the team that runs the agent.

What was asked, and by whom
The agent, the user it acted for, and each step's input. In the audit record, fields marked sensitive, such as the tax id, are a keyed hash. The clear value stays in operational storage until 7 days after the execution ends.
Which rule decided
Each policy decision with its input, its output and the digest of the bundle it was made under.
Who approved which hash
The approver, the role the rule required, when they decided, how they were identified and the execution hash they saw.
Each call and each undo
Every step started, succeeded or failed, and every compensation with its attempts.

The controller can read the execution's events, then check the chain from the first of them.

Start with a pilot

Stopping an agent is the easy half. Sefra also finishes the job.