> For the complete documentation index, see [llms.txt](https://docs.metamynd.ai/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.metamynd.ai/agent-controls-agentsafe/policy-bundle-pinning-and-freshness.md). Agents can also search and read this documentation through its [MCP server](https://docs.metamynd.ai/~gitbook/mcp).

# Policy Bundle Pinning & Freshness

A guard that evaluates policy locally — at the edge, in an MCP server, in a gateway — works from a **policy bundle**: one signed document that binds an agent's DID to the exact Standards, SOPs and mandates governing it. Whoever can change that bundle in flight can change the rules. This page covers how a guard proves a bundle is authentic, current, and the latest one MetaMynd published.

Normative text: [MAGP §6.2](https://metamynd.ai/developers/spec).

### The two endpoints

| Endpoint                   | Auth   | Returns                                                                                                                        |
| -------------------------- | ------ | ------------------------------------------------------------------------------------------------------------------------------ |
| `GET /policy/bundle/{did}` | public | The agent's evaluable policy, signed by the issuer (Ed25519, over canonical sorted-key JSON of everything except its `proof`). |
| `GET /magp/policy/pubkey`  | public | The Ed25519 public key that signs bundles.                                                                                     |

A guard **must** verify the signature before evaluating, and **must** reject a bundle whose signature is absent or invalid when the action is value-bearing.

### Risk-tiered fail-closed

Every bundle carries a `maxStaleness` (an ISO-8601 duration, e.g. `PT10M`), measured from its `issuedAt`.

| Bundle state                                | Value-bearing action (amount > 0)   | Non-value read |
| ------------------------------------------- | ----------------------------------- | -------------- |
| signed + fresh                              | ✅ `POLICY_BUNDLE_OK`                | ✅              |
| stale (older than `maxStaleness`)           | ❌ `POLICY_BUNDLE_STALE`             | tolerated      |
| unsigned                                    | ❌ `POLICY_BUNDLE_UNSIGNED`          | tolerated      |
| tampered / wrong key                        | ❌ `POLICY_BUNDLE_SIGNATURE_INVALID` | ❌ (any tier)   |
| no pinned key, fetched over plain `http://` | ❌ `POLICY_BUNDLE_UNVERIFIED`        | tolerated      |

### Pin the policy key

A guard with no pinned verification key cannot check the signature — it is trusting the transport instead. So:

1. Fetch `GET /magp/policy/pubkey` **once, out of band**.
2. Put the hex key in your guard's configuration.

```js
// @metamynd/agentsafe-mcp-guard (service side)
const guard = createMcpGuard({
  serviceDid,
  issuerApi: 'https://metamynd.ai/api/v1',
  policyPublicKey: '<hex from GET /magp/policy/pubkey>',
});
```

```bash
# @metamynd/agentsafe-http-gateway
AGENTSAFE_POLICY_PUBLIC_KEY=<hex from GET /magp/policy/pubkey>
```

With a pinned key, the guard verifies every bundle's signature and freshness from **any** source — a cache, a CDN, a mirror — and refuses a tampered, unsigned or stale one for a value-bearing call.

**Without a pinned key:**

* `agentsafe-mcp-guard` (since 0.12.0) and `agentsafe-http-gateway` say so at startup. If the bundle arrives over plain `http://`, they refuse value-bearing calls with `POLICY_BUNDLE_UNVERIFIED` — nothing authenticates that bundle, and an attacker on the path could drop the spend cap. Over `https://` they warn and carry on, trusting TLS alone. For local development only, `allowUnverifiedBundle: true` restores the old behaviour. A custom `fetchBundle` is your own source and is not affected.
* `agentsafe-a2a-guard` verifies bundles when given `policyPublicKey`; it does not apply the plain-`http://` refusal, so pin the key.

Gateways scaffolded by `create-metamynd-agent` have the key baked in. Step-by-step: [Pin the Policy Key](https://docs.metamynd.ai/guides/pin-the-policy-key).

### Freshness: serve-time re-issue

`maxStaleness` bounds the age of the **copy a guard holds**, not the age of the rules. Rules that have not changed can be weeks old; a guard must still be able to hold a fresh copy of them. So the issuer **re-issues a compiled bundle on every fetch**:

* `issuedAt` is the **serve time**, and the bundle is signed at that time.
* `compiledAt` is when the rules were compiled.
* `compiledSignature` is the compile's own signature — the one anchored on the agent's Hedera (HCS) topic as the `policy-update` digest.

A re-fetch therefore always yields a copy that is fresh against `maxStaleness`, even when nothing changed. Rule changes themselves are pushed: see [Event-Driven AI Triggers](https://docs.metamynd.ai/agent-protocol/event-driven-ai-triggers) (`GET /policy/events/{did}`, `watchPolicy`).

To recover and check the compiled bundle from a served copy: set `issuedAt` to `compiledAt`, remove `compiledAt` and `compiledSignature`, and verify `compiledSignature` over the result with the pinned key.

### Is this the latest bundle? (`verifyOnChain`)

A valid signature proves MetaMynd signed a bundle; it does not prove it is the **latest** one — an old, correctly signed bundle could be replayed. On every recompile MetaMynd anchors `sha256(signature)` of the compiled bundle on the agent's own HCS topic. A guard can confirm currency from a **public Hedera mirror node, with MetaMynd offline**:

* compare the latest anchored digest with `sha256(compiledSignature)`;
* for a bundle from an issuer that predates serve-time re-issue (no `compiledSignature`), compare with `sha256(proof.signature)`.

`@metamynd/agentsafe-guard` does this with `{ verifyOnChain: true }`:

```js
const guard = await createGuardFromConfig('./agent.metamynd.json', { verifyOnChain: true });
await guard.policyAnchor(); // → { sigDigest, seq } read from Hedera (or null)
```

If it cannot confirm (mismatch, not yet anchored, mirror down), it defers to the authoritative remote gate rather than evaluate a bundle it cannot prove is current. Append-only Hedera consensus makes the latest anchor authoritative and a rollback detectable.

### What local evaluation can and cannot decide

A guard evaluating a verified, current bundle decides a **block**, an **escalate** or a **non-value allow** on its own, with no network. It cannot decide the stateful checks — freshness and nonce, rate limits, circuit breakers, the cumulative-spend cap and the atomic reservation — so a **value-bearing allow is sealed by the remote gate**, and a guard that cannot reach the gate for a value-bearing action blocks (`GATE_UNREACHABLE`). (`agentsafe-guard` lets an operator switch sealing off with `sealValueActions: false`; that knowingly gives up the cumulative cap, replay protection and anchored evidence.) Pinning and freshness make the local rules trustworthy; they do not make value actions work offline.


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation by asking a question.

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
GET https://docs.metamynd.ai/agent-controls-agentsafe/policy-bundle-pinning-and-freshness.md?ask=<question>&goal=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
