Documentation

Receipt checks

A receipt check lets a service refuse any request from an agent that did not pass the gate first.

It is for agents you build yourself with the SDK. An agent connected through a hook cannot attach anything to the requests its tools make, so receipt checks do not apply there.

The ticket

When the engine allows an action, its answer includes a ticket. A ticket is a short statement, signed by the engine, that one specific action was allowed.

FieldMeaning
tenantThe workspace id.
request_idThe id of the ask that was allowed.
mandate_idThe passport id.
agentThe agent's name.
toolThe tool that was allowed.
targetThe target that was allowed.
args_hashThe hash of the action's arguments.
cost, currencyThe cost that was allowed.
seqThe receipt number of the decision in the ledger.
atWhen the decision was made.
expiresFive minutes after at.
sigThe engine's Ed25519 signature over the fields above.

Only an allowed action gets a ticket. A blocked, held or refused action has none.

How it travels

The agent sends the ticket with its request as the HTTP header Countersign-Receipt. The value is the ticket's JSON, base64url-encoded without padding. The SDKs pass the encoded ticket to your action function.

The agent side, Node

const body = { to, amount }
await agent.guard('payments.send', body, (ticket) =>
  fetch(PAYMENTS_URL, { method: 'POST', body: JSON.stringify(body),
    headers: { 'Countersign-Receipt': ticket } }),
  { target: to, cost: amount, currency: 'USD' })

The agent side, Python

The action function receives the ticket when it takes one argument.

def send(ticket):
    return post_payment(body, headers={"Countersign-Receipt": ticket})

agent.guard("payments.send", body, send, target=body["to"], cost=body["amount"], currency="USD")

post_payment stands for your own code that makes the request.

What the service checks

The service needs one thing from you: the workspace's engine key, as hex. This is the pin. See Concepts for where to find it.

Each verifier checks:

  1. the signature, against the pinned engine key
  2. that the ticket has not expired
  3. that the ticket is for this tool, this target and this cost, for each of those you give it
  4. that the ticket has not been used before

A request that fails gets HTTP status 403 from the Node and Go middleware, with a JSON body that gives the reason. For example:

{ "error": "This action needs a valid Countersign receipt: receipt has expired" }

Node

One file with no dependencies: sdk/node/countersign-verify.mjs.

curl -fsSLO https://app.trycountersign.ai/sdk/countersign-verify.mjs
import { requireReceipt } from './countersign-verify.mjs'

app.post('/payments', requireReceipt({
  pin: process.env.COUNTERSIGN_ENGINE_KEY,      // your workspace's engine key, hex
  tool: 'payments.send',
  target: (req) => req.body.to,                 // must match what the agent asked for
  cost: (req) => req.body.amount,               // minor units
}), handler)

requireReceipt is Express-style middleware. tool, target and cost can each be a value or a function of the request. On success the ticket is available to your handler as req.countersign.

ExportUse
requireReceipt({ pin, tool, target, cost, header, onReject })Middleware. Accepts each ticket once. header defaults to countersign-receipt. onReject(req, error) is called when a request is refused.
verifyTicket(header, { pin, tool, target, cost, now })The same check as a plain function, for anything that is not HTTP. Returns the ticket or throws ReceiptError. It does not track used tickets.
decodeTicket(header)Decodes the header value.
ReceiptErrorThe error thrown on any failed check.

Python

One file: sdk/python/countersign_verify.py. It needs the cryptography package.

curl -fsSLO https://app.trycountersign.ai/sdk/countersign_verify.py
pip install cryptography
from countersign_verify import verify_ticket, ReceiptError

try:
    ticket = verify_ticket(request.headers.get("Countersign-Receipt"), pin=ENGINE_KEY,
                           tool="payments.send", target=body["to"], cost=body["amount"])
except ReceiptError as e:
    return {"error": str(e)}, 403

verify_ticket(header, pin, tool=None, target=None, cost=None, now=None, once=True) returns the ticket or raises ReceiptError. With once=True, the default, it accepts each ticket once. There is no ready-made middleware for Python. Call the function from your own request handler.

Go

The package receipt in the repository (receipt/receipt.go).

mux.Handle("POST /payments", receipt.Require(receipt.Options{
	Pin:  engineKey, // your workspace's engine key, as bytes
	Tool: "payments.send",
})(handler))
NameUse
receipt.Require(Options)Middleware. Accepts each ticket once.
receipt.Check(request, Options)Verifies one request's ticket and returns it. It does not track used tickets.
receipt.Decode(header)Decodes the header value.
receipt.HeaderThe header name, Countersign-Receipt.

Options has Pin (the engine key as bytes), Tool (empty accepts any tool), and two optional functions, Target and Cost, that read the expected values from the request.

The package's import path is countersign/receipt and it depends on the core and wire packages of the same repository. It is used from within the repository's module. It is not published as a separate Go module.

Limits

  • "Used once" is per process. Each verifier remembers used tickets in its own memory. If your service runs as several processes, one ticket can be accepted once by each of them within its five minutes. A restart also forgets. To make single use hold across processes, record request_id in a store they share.
  • A ticket is valid for five minutes whatever happens next. If the passport is revoked one minute after an action was allowed, the ticket for that action still verifies until it expires.
  • Check the tool, the target and the cost. A verifier compares only the ones you give it. If you leave target out, a ticket for one payee is accepted for another.
  • The arguments are not compared. The ticket carries args_hash, and the verifiers do not check it against the request body. Anything in the request that is not the tool, the target or the cost is not bound by the check unless you compare the hash yourself.
  • Clocks. Expiry compares the engine's clock with the service's clock. A service clock that runs slow accepts a ticket for longer.
  • The pin is per workspace. A ticket from a different workspace is signed by a different engine key and fails the signature check.
  • The engine key is a software key. See Security and limits.