# Budgets by cost, load-aware proof of work, replay protection and the kill switch

Each of these is off until the operator of the service switches it on (a setting of the service), and a site then opts in. Where a call answers 404 with "not switched on", the operator has not set it.

## Proof of work that follows load and a caller's own cost
With `SHIELD_LOAD_CAPACITY` (requests a second the service is sized for) the proof a caller is asked for rises by up to 6 bits as the last ten seconds' request rate nears that capacity, and falls back on its own within ten seconds of the load going. With budgets on, a caller that has spent its own budget is asked for up to 4 bits more. The challenge answer's `bits` says what was asked. The most ever asked is 24 bits. The report shows the extra bits while there are any.

## Budgets by cost (SHIELD_BUDGETS=1)
A site sets a budget with `PUT /budget?site=ps_...` (the site's secret as a bearer; the admin key uses `PUT /sites/<site>/budget`):

```json
{ "windowSeconds": 3600,
  "limits": { "cents": 500 },
  "agentLimits": { "tokens": 200000, "dbMs": 60000 },
  "challengeAt": 0.8,
  "routes": { "POST /ai/*": { "tokens": 4000 }, "GET /search": { "dbMs": 40 } } }
```

Units: `tokens`, `dbMs` (database milliseconds) and `cents` (money in minor units). A unit with no limit is not limited. `limits` is per client; `agentLimits` is per signed agent (the same as `limits` when left out). A route is `"METHOD /path"` or `"/path"`; a trailing `*` is a prefix; the most specific match wins.

Before the work, call `POST /budget/check` with `{ site, secret, client | agent, method?, path?, cost? }`. `client` is any stable id you use for the caller (an address, a session); Shield keeps only a hash of it. `cost` is the cost you declare for this call; left out, the route's declared cost is used. The answer's `decision` is `allow`, `challenge` (the caller has spent `challengeAt` of a limit: ask it for proof or a touch) or `refuse` (the cost would take it over a limit; nothing is charged; `retryAfterSeconds` says when the window ends). After the work, call `POST /budget/report` with `{ site, secret, client | agent, cost, method?, path?, declared? }` to give the real cost: the declared cost (`declared`, or the route's when you give `path`) is taken back and the real one is counted. Spending is kept on disk (the service's state folder) and survives a restart.

`POST /verify-agent` applies the agent budget itself when the site has one: the answer adds `budget`, and a refusal there makes `decision.allowed` false with `decision.rule` `"budget"`. Send `cost` on the call to override the route's declared cost.

## Signed agents: replay protection and ECDSA
With `SHIELD_AGENT_REPLAY=1` a signature that checked is remembered until it could no longer pass the time checks, and the same signature (same key and nonce, or without a nonce the same key, time and signature) is refused the second time: "this signature was already used". An agent should send a fresh `nonce` parameter. `SHIELD_AGENT_REQUIRE_NONCE=1` refuses a signature without one. With `SHIELD_AGENT_ECDSA=1` Shield also takes `ecdsa-p256-sha256` signatures (raw r and s, RFC 9421 section 3.3.4) by keys of type EC, curve P-256, published in the key directory; the key's name is its RFC 7638 thumbprint.

## Post-quantum agent keys (ML-DSA-87 preferred, ML-DSA-65 accepted) and the both-signatures rule
With `SHIELD_AGENT_PQ=1` Shield also takes `ml-dsa-65` signatures (FIPS 204; the signature is over the RFC 9421 signature base, no context, no pre-hash) by keys of type `AKP` (RFC 9964) published in the same key directory: `{ "kty": "AKP", "alg": "ML-DSA-65", "pub": "<1952-byte public key, base64url>" }`. The key's name (`keyid`) is its RFC 7638 thumbprint over `alg`, `kty` and `pub`. The signature parameter is `alg="ml-dsa-65"`. ML-DSA-87 is the highest-rank standard set and the one new agent keys should use: publish `{ "kty": "AKP", "alg": "ML-DSA-87", "pub": "<2592-byte public key, base64url>" }` and sign with `alg="ml-dsa-87"`. ML-DSA-65 keys already published keep working (accepted for verification, not preferred). A key of another ML-DSA size, or of the wrong length, is ignored. Off by default; Ed25519 (and ECDSA, when on) agents are unchanged.

A site can require both families. Set `requirePqAndClassical: true` in the site's agent rules (`PUT /agents?site=` or `/sites/<site>/agents`; default false, and it needs `SHIELD_AGENT_PQ=1` on the service). `POST /verify-agent` then accepts a request only when it carries two signatures under two labels, `Signature-Input: sig1=(...);alg="ed25519";..., sig2=(...);alg="ml-dsa-65";...` with the matching `Signature: sig1=:...:, sig2=:...:`: one valid classical signature (Ed25519, or ECDSA when on) and one valid ML-DSA-65 signature, by two different keys published by the same agent. With only one, a wrong key, a changed request or a replayed signature the answer is `{ ok: false, class: "undeclared", reason }`, like any signature that fails, and no decision is made. With the rule off the check is as before. With replay protection on, neither signature is used up unless both pass.

## The kill switch (SHIELD_KILL_SWITCH=1)
- `POST /agent-suspensions` with `{ "agent": "agent.example", "reason"?: "..." }` suspends one agent at every site, from the next request. Who may: the admin key; the agent's operator (its manage secret as a bearer); or its accountable owner when that is a verified network identity, by `"ownerProof": { "at": <unix seconds>, "signature": "0x..." }`, an EIP-191 signature over `praecise-shield suspend agent <agent> at <at>`, within five minutes of `at`.
- `POST /owner-suspensions` with `{ "kind", "id", "agent"?, "reason"?, "ownerProof"? }` suspends every agent whose verified owner is that one (a self-declared organisation cannot be suspended this way). The agents it had at that moment stay suspended even if they clear their owner or prove their name again; an agent that later gets this verified owner is suspended too. Who may: the admin key, the operator of one of the owner's agents (`agent` and its manage secret), or the owner's own signature over `praecise-shield suspend owner <address> at <at>`.
- `DELETE /agent-suspensions/<agent>` and `DELETE /owner-suspensions?kind=&id=&agent=` lift them: the admin key lifts any; an operator lifts only what an operator or owner placed.
- `GET /agent-suspensions` (admin key) lists them. A suspended agent's `verify-agent` answer has `decision.allowed` false and `decision.rule` `"suspended"` at every site.

## In the report
`GET /report` (and the admin's `GET /sites/<site>`) adds `shield`: `budget` (the last seven days of allow, challenge and refuse, and the heaviest spenders of the current window), `proofOfWork` (the load level and the bits it adds) and `suspended` (agents seen at the site that are suspended), and writes the same in `lines` and `toDo`.

## How budgets work
- Budgets count the cost the site reports to Shield, in the units you choose: tokens, money or requests.
- A budget is kept by the service process that answers the call.
- Your server asks `verify-agent`, and a suspension or a budget applies to that answer.

## How Shield decides

- One touch, never a puzzle: a person passes with a single touch.
- Shield makes automation slow, costly and visible; agents that sign their requests get their own identity, budget and rules.
- The quiet check reads what a browser reports about itself, and the token is confirmed on your own server.
- The agent owner check is a signature over an Ethereum-style address.
- Rules are applied at the page and at your server; edge rules and certificates are not part of this service.
- Counts are kept by the Shield service.
