# 35.5 The role contract in code (/affordable-self-improving-ai-workers/the-worker-platform/the-role-contract-in-code)

---
type: Document
title: "35.5 The role contract in code"
description: "A versioned role contract for each worker, checked in code on every tool call and every Workflow start, so that delegating never adds authority and every sensitive decision is recorded."
status: stable
order: 435.05
ksor:
  owner: team:panaversity
  audience: [ public ]
  approval:
    by: process:panaversity
    at: 2026-11-30T08:18:55Z
chapter: "35"
part: IV
expert_status: manager
concepts: [ "35.5" ]
last_verified: 2026-11-29
sources:
  - id: paper
    title: "Building Affordable, Self-Improving AI Workers (Panaversity, 29 November 2026), section 9.3"
    resource: "Panaversity paper, version 1.0, 29 November 2026 (not yet published)"
generated:
  at: 2026-11-30T08:18:55Z
  by: human:zia
trust_tier: unverified
build_id: sha256:d6ce3f28a2cf8e492a60bae1b426b7cc6466e35e83d70c3b9763db2c55009f4b
dirty: true
ksor_version: 0.0.60
---

**In everyday life.** A new bank clerk gets a written job description and a signing limit. The cash machine does not care what the clerk believes the limit is. It refuses any withdrawal above it.

**What you will learn.** What goes in a role contract, how one check in code enforces it on every tool call and process, and what to record when it says no.

**Why it matters.** The contract is where a client's rules become real. A rule in the contract but not in the code is a promise you cannot keep.

### The contract

Each worker version has a role contract, written as data, not prose. Here is Acme's AP worker, contract `ap-rc-3-acme`:

```yaml
role: accounts-payable
tenant: acme
responsibilities: [read invoices, match to POs, post approved invoices, reconcile monthly]
tools: [lookup_invoice, match_po, request_approval, post_invoice,
        start_reconciliation, reconciliation_status]
processes: [reconcile-month@2]
limits:
  payment_per_invoice_usd: 5000
  payments_per_day_usd: 25000
  model_spend_per_month_usd: 40
approval_required:
  - action: post_invoice
    when: amount_usd > 500
    approver_role: ap-manager
data_class: business
```

Ten lines a finance manager can read and sign. The worker version in the control plane points to this contract by its version, so changing it is a new version, with a record of who changed it.

### One check, everywhere

> Step 4. Build src/contract.ts with one function, authorize(action, context), that loads the worker's contract version and returns allow, deny or needs_approval with a reason. Call it before every tool runs and before every Workflow is started, and again inside each Workflow step that acts. The tenant, worker and version come from the conversation's assignment record, never from the tool's arguments. Record every deny and every needs_approval. Write the tests first.

```ts
export async function authorize(action: Action, ctx: TrustedContext): Promise<Decision> {
  const c = await contractFor(ctx.workerId, ctx.workerVersion);      // from the assignment
  if (action.tenantId && action.tenantId !== ctx.tenantId) return deny("wrong tenant");
  if (action.kind === "tool" && !c.tools.includes(action.name)) return deny("tool not in contract");
  if (action.kind === "process" && !c.processes.includes(action.name)) return deny("process not in contract");
  if (action.name === "post_invoice") {
    if (action.amountUsd > c.limits.payment_per_invoice_usd) return deny("over invoice limit");
    if (await paidToday(ctx) + action.amountUsd > c.limits.payments_per_day_usd) return deny("over daily limit");
    if (action.amountUsd > 500) return needsApproval("ap-manager");
  }
  return allow();
}
```

Notice where `ctx` comes from: the `TrustedContext` is built by your code from the assignment record when the conversation starts. The model's tool arguments supply only `action`: what it wants to do. If the arguments name a tenant, the check compares it with the trusted one and refuses a mismatch.

**How the model sees a refusal.** A deny comes back to the model as a tool *result*, in plain words: "Refused: this invoice is over the $5,000 limit for this worker. A person must post it." As with Chapter 33's rejection, the model can then tell the user what happened, rather than crash or try again.

### Delegating never adds authority

A Workflow started by the worker is a separate program, with its own code. It could, in principle, call the ledger with any amount. So:

- Starting a process is itself an action the contract must allow (`processes: [reconcile-month@2]`).
- The process receives the worker's ID, version and tenant from the control plane's record, not from the worker's message.
- Each step that acts calls `authorize` again, with the same trusted context. A reconciliation that tries to post a payment it found will be refused, because posting is not in the process's steps and payment limits still apply.

The rule from the paper: **no working agent, process or learning loop may widen its own permissions.**

### Record the decisions

Every `deny` and `needs_approval`, and every `allow` for a payment, writes a row. The row records who, which worker and version, which contract, what action, which decision and why, and the correlation ID of the assignment (35.6). This is the record an auditor asks for, and the evidence you use when a client asks, "Why did your worker refuse to pay our supplier?"

**What to check.** Count the tools and processes. Does each one call `authorize` before it acts? Is any limit written in a prompt but missing from the contract? Can any code path build a `TrustedContext` from data the model wrote?

> **The invariant.** Every action is checked against the contract in code, with a context the model cannot write. Delegating never adds authority.

**Check yourself.** A new tool, `refund_supplier`, is added to the worker's code and its prompt, but not to its contract. What happens when the model calls it? *(It is denied with "tool not in contract", and the denial is recorded. To use it, someone must add it to a new contract version, which is a reviewed change.)*
