@agent-custody/receipts 0.14.0 → 0.15.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +33 -20
- package/dist/check.d.ts +10 -0
- package/dist/check.js +179 -3
- package/dist/cli.js +82 -11
- package/dist/config.d.ts +128 -1
- package/dist/config.js +92 -3
- package/dist/console.js +16 -7
- package/dist/deny-report.js +1 -1
- package/dist/door.d.ts +203 -0
- package/dist/door.js +712 -0
- package/dist/export.d.ts +39 -0
- package/dist/export.js +95 -0
- package/dist/gateway.js +53 -21
- package/dist/index.d.ts +6 -0
- package/dist/index.js +5 -0
- package/dist/issue.js +5 -65
- package/dist/lock.d.ts +12 -0
- package/dist/lock.js +124 -0
- package/dist/log.d.ts +11 -5
- package/dist/log.js +55 -42
- package/dist/opa-server.d.ts +34 -0
- package/dist/opa-server.js +93 -0
- package/dist/policy-rego.d.ts +30 -0
- package/dist/policy-rego.js +114 -0
- package/dist/pr-summary.d.ts +25 -0
- package/dist/pr-summary.js +87 -0
- package/dist/receipt.d.ts +3 -1
- package/dist/retention.d.ts +1 -1
- package/dist/retention.js +14 -4
- package/dist/sdk/index.js +10 -3
- package/dist/simulate.d.ts +3 -1
- package/dist/simulate.js +15 -7
- package/dist/verify.js +6 -3
- package/docs/configuration.md +3 -3
- package/docs/door-signals.md +66 -0
- package/docs/door.md +168 -0
- package/docs/github-action.md +164 -0
- package/docs/policies.md +112 -1
- package/docs/sdk.md +25 -9
- package/docs/tutorials.md +3 -0
- package/docs/usage.md +2 -2
- package/docs/verification.md +9 -2
- package/package.json +14 -13
- package/templates/claude-code.cedar +287 -10
- package/vectors/audit.json +27 -27
- package/vectors/canonical.json +5 -5
- package/vectors/merkle.json +123 -0
- package/vectors/receipts.json +1258 -1005
package/docs/policies.md
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
# Writing policies
|
|
2
2
|
|
|
3
|
-
Policies are written in [Cedar](https://www.cedarpolicy.com/), the language AWS uses for AgentCore and Verified Permissions. There is no home-grown policy language here. Every example on this page is executed against the real evaluator in the test suite.
|
|
3
|
+
Policies are written in [Cedar](https://www.cedarpolicy.com/), the language AWS uses for AgentCore and Verified Permissions, or in [Rego](https://www.openpolicyagent.org/docs/policy-language), the language of Open Policy Agent. There is no home-grown policy language here. Cedar is the default and most of this page uses it; [Rego](#rego) has its own section, including the choice between a Rego file the gateway compiles and an OPA server you already run. Every example on this page is executed against the real evaluator in the test suite.
|
|
4
4
|
|
|
5
5
|
## How a tool call becomes a Cedar request
|
|
6
6
|
|
|
@@ -168,6 +168,100 @@ permit(principal, action == Action::"memory.retract", resource)
|
|
|
168
168
|
when { context.facts has target && context.facts.target.provenance == "claimed" };
|
|
169
169
|
```
|
|
170
170
|
|
|
171
|
+
## Rego
|
|
172
|
+
|
|
173
|
+
A gateway decides by Rego in one of two ways. Give `policyFile` a path ending in `.rego`, and the gateway compiles the file itself. Or give `opa` instead of `policyFile`, and the gateway asks an OPA server you run. Both read the same request and the same rules, so a policy moves between them unchanged.
|
|
174
|
+
|
|
175
|
+
### The request and the rules
|
|
176
|
+
|
|
177
|
+
The input is the Cedar request with plain strings for the three identifiers:
|
|
178
|
+
|
|
179
|
+
| Rego | value |
|
|
180
|
+
| --- | --- |
|
|
181
|
+
| `input.principal` | the agent id from the grant |
|
|
182
|
+
| `input.action` | the tool name |
|
|
183
|
+
| `input.resource` | the tool name |
|
|
184
|
+
| `input.context.args` | the call's arguments. **Provenance: claimed.** |
|
|
185
|
+
| `input.context.facts` | the gateway's own upstream lookups. **Provenance: observed.** |
|
|
186
|
+
| `input.context.grant` | `{ principal, scopes }` from the signed delegation. **Provenance: attested.** |
|
|
187
|
+
|
|
188
|
+
The policy declares `package agentcustody` and defines two rules:
|
|
189
|
+
|
|
190
|
+
- **`allow`**, a boolean. **Default deny:** the call is permitted only when `allow` is exactly `true`.
|
|
191
|
+
- **`deny`**, a set of strings. Any entry wins over `allow`, as a Cedar `forbid` wins over a `permit`. Its strings are the reasons in the receipt and in the message the agent gets.
|
|
192
|
+
- **Errors deny.** A policy that fails while it evaluates, or whose `allow` is not a boolean or whose `deny` is not a set of strings, denies the call. The receipt records the error.
|
|
193
|
+
- **The scope check and the fact lookups run first**, exactly as for Cedar, and no policy can override them.
|
|
194
|
+
|
|
195
|
+
The fixture policy from the top of this page, in Rego:
|
|
196
|
+
|
|
197
|
+
```rego
|
|
198
|
+
package agentcustody
|
|
199
|
+
|
|
200
|
+
# Reads of customer records are permitted to any agent holding the scope.
|
|
201
|
+
allow if input.action == "customer.lookup"
|
|
202
|
+
|
|
203
|
+
# Refunds: a whole number of pence from 1 to 100000 (£1,000), and only for customers the gateway itself verified.
|
|
204
|
+
allow if {
|
|
205
|
+
input.action == "stripe.refund"
|
|
206
|
+
amount := input.context.args.amount
|
|
207
|
+
is_number(amount)
|
|
208
|
+
amount == floor(amount)
|
|
209
|
+
amount > 0
|
|
210
|
+
amount <= 100000
|
|
211
|
+
input.context.facts.customer.verified == true
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
deny contains "this customer is flagged for review" if {
|
|
215
|
+
input.action == "stripe.refund"
|
|
216
|
+
input.context.facts.customer.flagged == true
|
|
217
|
+
}
|
|
218
|
+
```
|
|
219
|
+
|
|
220
|
+
A missing attribute is *undefined* in Rego, not an error: the rule does not hold when there is no `amount`, so the call is denied. That is the same outcome as Cedar's error, reached a different way. Rego has floats, which Cedar does not, so `amount <= 100000` alone would hold for `0.5`, and for `0` and `-1` as well. Amounts are integer minor units: `amount == floor(amount)` holds only for a whole number, and `amount > 0` sets the floor.
|
|
221
|
+
|
|
222
|
+
### A Rego file, or an OPA server
|
|
223
|
+
|
|
224
|
+
| | `policyFile: "policy.rego"` | `opa: { "url": ... }` |
|
|
225
|
+
| --- | --- | --- |
|
|
226
|
+
| where the policy lives | a file beside `gateway.json`, as with Cedar | in the bundles of an OPA server your platform team runs |
|
|
227
|
+
| how it is evaluated | compiled to WebAssembly by `opa build` when the file loads or changes, then evaluated inside the gateway | an HTTP query to the server's Data API for every call |
|
|
228
|
+
| what it needs | the `opa` binary on `PATH` when the policy loads, never on a call. The gateway image includes it | a reachable server at an `https` URL, or `http` on a loopback address. A call it cannot answer within `timeoutMs` (default 2000) is denied |
|
|
229
|
+
| what `policyDigest` in a receipt is | the sha256 of the file's text, as for Cedar. The portal can name it, and anyone with the file can check it | the sha256 of the decision URL and the revision of every bundle the server reports. Map it back with `receipts/policy-reloads.jsonl`. A call the server did not answer is denied under the URL's own "unanswered" digest, never under a revision that did not decide it |
|
|
230
|
+
| changing the policy | write the file by rename. It applies from the next call, and a file that does not compile is refused while the policy in force stays | publish a new bundle revision. The first call under it records the new digest |
|
|
231
|
+
| builtins | those the WebAssembly runtime supports. `http.send`, `time.now_ns`, and other builtins it cannot run are refused when the file loads | everything the server allows, including `http.send` |
|
|
232
|
+
| best for | a gateway that should decide on its own, with the policy text pinned in every receipt | an organisation that already governs policy centrally in OPA and wants agents under the same bundles |
|
|
233
|
+
|
|
234
|
+
**What a receipt can prove differs.** With a file, the receipt pins the exact text that decided, so a verifier holding the file can show it was this policy. With a server, the receipt pins only what the server *said* it was running: the bundle revisions in its `provenance` answer. The gateway asks for provenance on every query, records each new combination in `policy-reloads.jsonl` with the bundle names and revisions, and prints it. Serve the policy from a bundle built with `opa build --revision <id>`, so the revision names a version you can find again. A server that reports no revision still works, but the gateway warns at start and on each new answer that its receipts cannot name the policy. `agent-custody check` warns about it as well.
|
|
235
|
+
|
|
236
|
+
**A server outside the policy also decides.** A Rego file is evaluated where the gateway runs, under the operator's control. An OPA server's answer is trusted as it comes back over HTTP. Run it where only your platform team can change it, and give the gateway a token with `tokenEnv` when the server requires one. Each query carries the call's arguments and that token, so the gateway refuses a plain `http` URL unless the host is a loopback address, and it never follows a redirect: a 3xx answer denies the call. The gateway refuses to start when it cannot reach the server, or when the server does not define the package.
|
|
237
|
+
|
|
238
|
+
A gateway config with an OPA server:
|
|
239
|
+
|
|
240
|
+
```json
|
|
241
|
+
"opa": {
|
|
242
|
+
"url": "http://localhost:8181",
|
|
243
|
+
"decision": "agentcustody",
|
|
244
|
+
"tokenEnv": "OPA_TOKEN",
|
|
245
|
+
"timeoutMs": 2000
|
|
246
|
+
}
|
|
247
|
+
```
|
|
248
|
+
|
|
249
|
+
`decision` is the package path under `/v1/data`, such as `authz/agents` for `package authz.agents`, and defaults to `agentcustody`. The document at that path must have the `allow` and `deny` rules above. The gateway posts `{ "input": ... }` to `<url>/v1/data/<decision>?provenance=true`.
|
|
250
|
+
|
|
251
|
+
### Testing and simulating a Rego policy
|
|
252
|
+
|
|
253
|
+
`opa test` runs Rego unit tests as usual. `evaluateRego` and `compileRego` from `@agent-custody/receipts` evaluate a policy exactly as the gateway does, with the same decision and digest:
|
|
254
|
+
|
|
255
|
+
```ts
|
|
256
|
+
import { compileRego, evaluateRego, policyDigest } from "@agent-custody/receipts";
|
|
257
|
+
|
|
258
|
+
const text = readFileSync("policy.rego", "utf8");
|
|
259
|
+
const { policy, errors } = compileRego(text); // errors: why it cannot load
|
|
260
|
+
const d = evaluateRego(policy!, policyDigest(text), { agentId: "support-agent", tool: "stripe.refund", context: { args: { amount: 50000 }, facts: { customer: { verified: true } } } });
|
|
261
|
+
```
|
|
262
|
+
|
|
263
|
+
`agent-custody policy simulate --policy proposed.rego ...` replays recorded receipts against a Rego policy, including receipts a Cedar policy decided, so a move from Cedar to Rego can be checked against history before it is enforced. `agent-custody check` compiles a `.rego` file, or queries the OPA server once. An SDK issuer reads a `.rego` `policyFile` the same way. The console's policy editor, the deny report, and `init claude-code` work with Cedar only for now.
|
|
264
|
+
|
|
171
265
|
## Saying why a rule denies
|
|
172
266
|
|
|
173
267
|
Put a `@reason("...")` annotation before a `forbid` rule. The Claude Code hook shows the text to the agent and to the person who reads the session, and `agent-custody report` prints it before the rule.
|
|
@@ -236,3 +330,20 @@ The report lists every call whose `allow` or `deny` decision changes, grouped by
|
|
|
236
330
|
Every run records a `runId`, `timestamp`, the proposed text's sha256 as `policyDigest`, and the original digests as `previousPolicyDigests`. `--out` saves the full JSON report, and `--json` prints it to stdout. Changes alone exit 0. A report that cannot replay every call says `INCOMPLETE` and exits 1: `skipped` names each receipt and why. This includes record-only receipts with no policy baseline, withheld arguments or fact values, calls whose receipt is missing, and calls stopped before Cedar could evaluate. Grant failures cannot become permits through a policy change, and neither can an approval refusal: the gateway checks the approval before Cedar runs, so the call is skipped as a non-policy gate. The approval record says so even under a private rule. Withheld results alone do not prevent replay. A reconciliation receipt is a provider read, so no policy decision exists to replay. The report counts it in `reconciliations`, not in `evaluated` or `skipped`, and it does not make the report incomplete. A reconciliation whose original receipt is missing is the exception: the call that it settles cannot be replayed, so the report skips that call and says `INCOMPLETE`. The library trusts a reconciliation receipt only after `verifyBundle` accepts it; the CLI and the console verify first.
|
|
237
331
|
|
|
238
332
|
The report is an analysis of the supplied receipt directory; it does not prove that the directory contains the whole history. Keep the report beside the proposed policy and review the changed receipt ids before switching from observe mode to enforcement. [Tutorial 28](../examples/28-policy-simulation.ts) records three refunds, stops the gateway, and replays them against a lower limit.
|
|
333
|
+
|
|
334
|
+
## How an arrival at the door becomes a Cedar request
|
|
335
|
+
|
|
336
|
+
The [door](door.md) uses the same evaluator for arrivals at your own HTTP surface. `principal` is `Agent::"<agent>"` for the agent a verified grant names, and `Agent::"unknown"` for every other arrival: a declared name is a claim and stays in the arrival fact. `action` and `resource` are the route the arrival fell under. `context.args` is the request (`method`, `path`, `query`, `headers`, a JSON `body` when small, `bodyBytes`, `bodyDigest`; `bodyOversized: true` and no digest when the body passed `maxBodyBytes`), claimed. The policy reads it with `null` members dropped and numbers that are not safe integers as strings, since Cedar refuses both. `context.facts.arrival` is the door's own observation of the arrival: its `class` (`verified`, `claimed`, `unknown`), `subject`, what became of its `grant`, and its `remote` address. `context.grant` is present only for a verified arrival. A policy that lets `unknown` through on a route lets everything through on that route.
|
|
337
|
+
|
|
338
|
+
```cedar
|
|
339
|
+
permit(principal, action == Action::"catalogue", resource);
|
|
340
|
+
|
|
341
|
+
permit(principal, action == Action::"refund", resource)
|
|
342
|
+
when {
|
|
343
|
+
context.facts.arrival.class == "verified" &&
|
|
344
|
+
context.args.body.amount > 0 &&
|
|
345
|
+
context.args.body.amount <= 5000
|
|
346
|
+
};
|
|
347
|
+
```
|
|
348
|
+
|
|
349
|
+
A bound on an amount needs both ends: `amount <= 5000` alone lets a negative refund through, and the door relays the body as it came. A permit that names an agent, `principal == Agent::"shopping-agent"`, is satisfied only by a verified grant for that agent: a declared name never reaches the principal slot. `agent-custody check` still warns on a permit that names an agent without requiring `context.facts.arrival.class == "verified"`, because a policy copied from the gateway reads as if it did.
|
package/docs/sdk.md
CHANGED
|
@@ -48,13 +48,15 @@ The command sets up custody for your own Claude Code sessions. It prints each st
|
|
|
48
48
|
|
|
49
49
|
1. It makes an agent key in `~/.config/agent-custody/keys/`.
|
|
50
50
|
2. It writes `sdk.json` in `~/.config/agent-custody/` with `"mode": "observe"` and a `receiptsDir`. No log is configured, so the receipts stay on your machine.
|
|
51
|
-
3. It writes a starter deny set to `policy.cedar`: force push, `rm -rf` on `/`, `~`, and `$HOME`, `terraform destroy`, `terraform apply -destroy`, and `drop database` in both cases. Each rule has a `@reason`, so the denial text says what the rule forbids. The policy permits every other call, and it permits `git push --force-with-lease` (see [The starter deny set](#the-starter-deny-set)).
|
|
51
|
+
3. It writes a starter deny set to `policy.cedar`: force push (`--force`, `-f`, and `+refspec`), `rm -rf` on `/`, `~`, and `$HOME`, `terraform destroy`, `terraform apply -destroy`, and `drop database` in both cases. Each rule has a `@reason`, so the denial text says what the rule forbids. The policy permits every other call, and it permits `git push --force-with-lease` (see [The starter deny set](#the-starter-deny-set)).
|
|
52
52
|
4. It registers the `PreToolUse`, `PostToolUse`, and `PostToolUseFailure` hooks in `.claude/settings.local.json` of the current directory. Claude Code adds that file to your global git ignore only when Claude Code itself creates it, so `init` does not rely on that. When the project is a git repository and git does not ignore the file, `init` prints a warning with the line to add to `.gitignore`: `.claude/settings.local.json`. The file holds absolute paths of your home directory. `init` never edits `.gitignore`. With `--user`, the hooks go to `~/.claude/settings.json` and apply to every project.
|
|
53
53
|
5. It prints the next step: start a Claude Code session, then run the report.
|
|
54
54
|
|
|
55
55
|
The hook command in the settings file uses absolute paths: the Node binary, the CLI script that runs now, and the absolute path of `sdk.json`. The command merges into an existing settings file. It keeps every other key and every other hook, and it does not add a hook that is already there. If the settings file is not valid JSON, the command stops with an error and changes nothing.
|
|
56
56
|
|
|
57
|
-
Each hook is its own process, and Claude Code runs hooks at the same time for parallel tool calls, subagents, and several sessions. The local log `log.jsonl` takes the lock file `log.jsonl.lock` for each append, reads the leaves that other processes appended, and then writes its own leaf. A lock file
|
|
57
|
+
Each hook is its own process, and Claude Code runs hooks at the same time for parallel tool calls, subagents, and several sessions. The local log `log.jsonl` takes the lock file `log.jsonl.lock` for each append, reads the leaves that other processes appended, and then writes its own leaf. The lock file holds an owner token, the process id, and the start time of the process. A holder removes the lock file only when it still holds its own token. A lock is stale when its process on this host is gone. The lock of a live process on this host is never broken. The lock of a process on another host, or a lock file with no readable owner, is stale when nobody touched it for 60 seconds. To break a stale lock, a writer first takes `log.jsonl.lock.break` with an exclusive create, so only one writer breaks at a time. It then reads the lock again, and moves it away only when its inode and token are still the ones it judged stale. A lock file that is gone between the create and the judgment is not broken: the writer tries the create again. The outbox of the gateway uses the same lock code. `agent-custody prune` takes the same lock, so a hook that appends during a prune waits and its leaf is kept.
|
|
58
|
+
|
|
59
|
+
A writer that waits for the lock gives up after 20 seconds. A `PreToolUse` hook that gives up cannot record a denial: in enforce mode the call is still blocked, and the denial text ends with `no receipt: <reason>`. A `PostToolUse` or `PostToolUseFailure` hook that gives up exits with code 1 and prints the error on stderr. That receipt is lost, because the tool call has already run and the hook does not retry. The other receipts are not affected. If a process was killed in the middle of a write, the last line of `log.jsonl` has no newline. The next append moves those bytes to `log.jsonl.torn`, and the log continues after the last whole line. No leaf was returned for those bytes.
|
|
58
60
|
|
|
59
61
|
The hook command pins the path of the Node binary that runs `init`. With nvm, a removed Node version breaks the hook, and every tool call then prints an error. After you remove a Node version, run `init` again. The second run updates the path.
|
|
60
62
|
|
|
@@ -76,15 +78,29 @@ Each rule matches the text of a Bash command with the Cedar `like` operator, and
|
|
|
76
78
|
|
|
77
79
|
| command | the policy |
|
|
78
80
|
| --- | --- |
|
|
79
|
-
| `git push --force`, `git push -f`, `git push origin main -f` | denied |
|
|
80
|
-
| `git push --force
|
|
81
|
-
| `git push
|
|
82
|
-
| `
|
|
83
|
-
| `rm -rf
|
|
81
|
+
| `git push --force`, `git push -f`, `git push origin main -f`, `git push origin -f main`, `git push origin +main`, `git -C repo push --force` | denied |
|
|
82
|
+
| `git push origin main --force && gh pr create`, `git push origin +main && echo done` | denied |
|
|
83
|
+
| `git push --force-with-lease`, `git -C repo push --force-with-lease` | allowed. It is a force push that fails when the remote has new commits, so it is the safer form. The `@reason` of the rule says so |
|
|
84
|
+
| `git push origin main && rm -f stale.lock`, `git push origin fix-f`, `git push origin main && npm install --force`, `git push && git status -f` | allowed |
|
|
85
|
+
| `rm -rf /`, `rm -fr /`, `rm -Rf /`, `rm -r -f /`, `rm -rf /*`, `rm -rf ~`, `rm -rf $HOME`, `rm -rf "$HOME"`, `rm -rf ${HOME}` | denied |
|
|
86
|
+
| `rm -rf /tmp/build`, `rm -rf ~/projects/app/dist`, `rm -rf "$HOME"/projects/app/dist` | allowed |
|
|
84
87
|
| `terraform destroy`, `terraform apply -destroy` | denied |
|
|
85
88
|
| `terraform plan`, `terraform plan -destroy` | allowed |
|
|
86
89
|
|
|
87
|
-
A `--force
|
|
90
|
+
A force push has `--force`, `-f`, or a refspec that starts with `+`. The push can be `git push` or `git -C <dir> push` (any `git` option before `push`). A flag counts in three cases: it is the first argument after `push`, or no `&`, `|`, `;`, or newline follows `push`, or one of them follows the flag. A flag that comes after an operator belongs to another command, so `git push origin main && npm install --force` passes.
|
|
91
|
+
|
|
92
|
+
The `rm` rules know the flags `-rf`, `-fr`, `-Rf`, `-fR`, and the separate forms `-r -f`, `-f -r`, `-R -f`, `-f -R`. The home directory can be `~`, `$HOME`, or `${HOME}`, and `$HOME` can be in double quotes.
|
|
93
|
+
|
|
94
|
+
The rule ids are the place of each rule in `policy.cedar`: the permit rule is `policy0`, the force push rule is `policy1`, and `terraform destroy` is `policy5`. Change a rule in place, and add a new rule at the end of the file, so that an id keeps naming the same rule. Receipts, reports, and pages name rules by id.
|
|
95
|
+
|
|
96
|
+
What Cedar `like` cannot express, and the starter policy therefore does not catch:
|
|
97
|
+
|
|
98
|
+
- `like` has `*` and nothing else. It cannot find where one shell command ends. A chain with two operators after `git push` can be denied by mistake: `git push && npm install --force && echo done` is denied.
|
|
99
|
+
- It cannot read shell words. These forms pass: `rm --recursive --force /`, `rm --no-preserve-root -rf /`, `terraform -chdir=infra destroy`, a command in a variable or in a script file, and a path that reaches the same directory (`rm -rf /usr/..`).
|
|
100
|
+
- It cannot tell a flag from the text of an argument. `git push origin "message -f"` is denied.
|
|
101
|
+
- A force push that the git configuration or an alias sets (a `+` in `remote.<name>.push`) is not in the text of the command.
|
|
102
|
+
|
|
103
|
+
Use a sandbox, or a protected branch on the remote, for these. `test/claude-init.test.ts` runs every command of this table and of this list.
|
|
88
104
|
|
|
89
105
|
### The denial text
|
|
90
106
|
|
|
@@ -110,7 +126,7 @@ The receipt does not change. It keeps the policy ids and the policy digest, and
|
|
|
110
126
|
agent-custody report --config ~/.config/agent-custody/sdk.json
|
|
111
127
|
```
|
|
112
128
|
|
|
113
|
-
`init` prints this command with absolute paths. The report reads the receipts directory of the config and verifies each receipt with the agent's key. Then it prints `would have denied N calls`, and for each call the tool, the command or the file path, the `@reason` of the rule when it has one, the Cedar rule, and the receipt id. A receipt that does not verify is counted apart, listed with the reason, and never counted as a denial. The command then exits with code 1. The report ends with the next step: remove the `"mode": "observe"` line from `sdk.json` to enforce the policy. A denial that the policy enforced (a config without the observe line) is listed apart as `denied`.
|
|
129
|
+
`init` prints this command with absolute paths. The report reads the receipts directory of the config and verifies each receipt with the agent's key. Then it prints `would have denied N calls`, and for each call the tool, the command or the file path, the `@reason` of the rule when it has one, the Cedar rule (cut to 160 characters), and the receipt id. A receipt that does not verify is counted apart, listed with the reason, and never counted as a denial. The command then exits with code 1. The report ends with the next step: remove the `"mode": "observe"` line from `sdk.json` to enforce the policy. A denial that the policy enforced (a config without the observe line) is listed apart as `denied`.
|
|
114
130
|
|
|
115
131
|
`policy simulate` answers another question: what a proposed policy would change in recorded history. Use `report` to read what the policy in the config decided in observe mode.
|
|
116
132
|
|
package/docs/tutorials.md
CHANGED
|
@@ -46,7 +46,10 @@ Suggested reading order is the numbering. Output lands in `examples-out/`, which
|
|
|
46
46
|
| 34 | strict mode | [34-strict-mode.ts](../examples/34-strict-mode.ts) | `check` lists what strict mode would refuse; a local log and a log that is not hash-only are refused at startup; behind a hash-only log with a pinned key, a read is precommitted and its receipt says `issuer.strict`; `requireStrict` accepts it and refuses an ordinary receipt; with the log down the call is not forwarded and the outbox stays empty | `src/config.ts`, `src/gateway.ts`, `src/check.ts`, `src/verify.ts` |
|
|
47
47
|
| 35 | completeness from the provider's side | [35-completeness.ts](../examples/35-completeness.ts) | two refunds through the gateway and a third issued straight through the provider; the auditor lists the provider's refunds with a read-only key, matches two to verified receipts, and reports the third as an action with no receipt |
|
|
48
48
|
| 36 | one command for Claude Code | [36-claude-code-init.ts](../examples/36-claude-code-init.ts) | `init claude-code` in a temporary home: the key, the observe-mode config, the starter policy, and the three hooks; a second run adds nothing; a session of four calls; the report that lists the two calls the policy would have denied | `src/claude-init.ts`, `src/deny-report.ts` |
|
|
49
|
+
| 37 | receipts on a pull request | [37-pr-summary.ts](../examples/37-pr-summary.ts) | two receipts from a gateway verify into one Markdown table (what `verify --markdown` and the GitHub Action post); one changed byte makes the table name the failed check; a directory with no receipt fails | `src/pr-summary.ts`, `action.yml` |
|
|
49
50
|
| 38 | an incident replay | [38-incident-replay.ts](../examples/38-incident-replay.ts) | the same setup as `init claude-code --enforce`, in a temporary home; a scripted Claude Code session that runs the real hook command for each event: `terraform plan` runs and gets an executed receipt, `terraform destroy -auto-approve` and `rm -rf ~` are denied before they run, with the denial text and the receipt id; a reviewer with only the public key verifies all three receipts, and a stranger's key fails | `src/sdk/claude.ts`, `src/claude-init.ts`, `templates/claude-code.cedar` |
|
|
51
|
+
| 39 | exporting receipts to a pull request | [39-export.ts](../examples/39-export.ts) | an agent works under a capture rule; `export` copies the two receipts that withhold their values, byte for byte, and refuses the one that records plain values; one changed byte makes it refuse a third | `src/export.ts`, `src/cli.ts` |
|
|
52
|
+
| 40 | the door: a receipted reverse proxy in front of your own API | [40-door.ts](../examples/40-door.ts) | a stand-in shop behind the door; an unknown arrival reads the catalogue and is refused a refund; an agent with a grant signed by the shop's principal is verified and refunds within the policy, refused above it; a forged grant is claimed; the origin saw only what was allowed; both receipts verified | `src/door.ts`, `src/config.ts` |
|
|
50
53
|
|
|
51
54
|
## How policies are defined, in one paragraph
|
|
52
55
|
|
package/docs/usage.md
CHANGED
|
@@ -148,7 +148,7 @@ The grant is checked on every call, not only when the session opens: a call afte
|
|
|
148
148
|
|
|
149
149
|
The outbox is appended in the order of the signed statements' timestamps, oldest first. A log refusal stops the flush, so one receipt that the log refuses holds the receipts after it. `GET /health` on the HTTP gateway reports `pendingReceipts`. Only a gateway that serves (stdio, `--http`, `--a2a`) runs the flush, at start and every 30 seconds; `check`, `reconcile`, and the other commands never append the outbox. A gateway that stops waits for a flush that runs. A gateway that stops before the log takes the receipt keeps the outbox on disk and appends it on the next start. The gateway writes the outbox entry to disk, and syncs the file and its directory, before the agent gets the result.
|
|
150
150
|
|
|
151
|
-
Several gateways, or a gateway and a command, can share one `receipts/` directory. A flush takes the lock file `receipts/outbox/.lock` with an exclusive create. When another process holds the lock, the flush does nothing and reports the entries as `pending`. The gateway breaks a lock whose owner process no longer runs on this host,
|
|
151
|
+
Several gateways, or a gateway and a command, can share one `receipts/` directory. A flush takes the lock file `receipts/outbox/.lock` with an exclusive create. When another process holds the lock, the flush does nothing and reports the entries as `pending`. The gateway breaks a lock whose owner process no longer runs on this host. It never breaks the lock of a live process on this host. It breaks the lock of a process on another host, or a lock file with no readable owner, when nobody has touched it for 15 minutes (one minute for a file with no owner). It uses the same break procedure as the local log ([sdk.md](sdk.md)).
|
|
152
152
|
|
|
153
153
|
When the log takes a receipt, the gateway records the log's answer in the outbox entry before it writes the bundle. If the bundle write then fails (a full disk, a permission error), the gateway prints the error on stderr, and the next flush writes the bundle from the recorded answer and does not append the leaf again. A process that dies between the append and that record appends the leaf again on the next flush; the log then holds it twice, and either copy proves the receipt. A gateway that dies after the forward and before the outbox write leaves no receipt; `precommit` is the guard for the calls where that matters. Before the gateway appends an outbox entry, it checks the entry. It renames an entry to `<name>.bad`, does not append it, and prints a warning, when the file is damaged, when the entry carries this gateway's key id but the signature does not verify, or when the receipt id in the statement is not the id in the file name. An entry that another key signed (a rotated gateway key, or a restored backup) is not appended, is not called forged, and stays in the outbox; the gateway prints a warning on each flush. Restore the key that signed it, or move the entry away. Until the log takes a pending receipt, `receipts/<id>.json` does not exist, so `verify`, `explain`, `blast`, the review console, and `prune` do not see the call yet.
|
|
154
154
|
|
|
@@ -253,7 +253,7 @@ A reviewer verifies such a receipt three ways:
|
|
|
253
253
|
- With the key: `agent-custody verify ... --capture-key-env AGENT_CUSTODY_CAPTURE_KEY` decrypts every encrypted value, checks each against its commitment, and recomputes the digests and the upstream's signature from the real values. A key that is not the one the values were encrypted under fails the checks, naming the key id, rather than passing them as committed.
|
|
254
254
|
- With a disclosure: `agent-custody verify ... --disclose disclosure.json` does the same from a file holding, at each withheld path, the real value and the salt its commitment was made with: `{ "receiptId", "request": { "args": { "$rest": { "$custody": "disclosed", "salt": "<hex>", "value": { "reason": "…" } } } }, "execution": { "result": { "$rest": { … } }, "error", "upstream": { "rawBody", "signature", "bind" } }, "policy": { "errors" }, "facts": { "<name>": { "value": { "$rest": { … } }, "args": { … }, "resultDigest" } } }`, any part of it. A disclosed `$rest` is folded back into its object before the digest is recomputed. A value without its salt cannot be checked and is refused as such. A disclosure that contradicts a commitment fails the check with the path named, a fact's value included: a reviewer cannot be handed a false policy fact. A disclosure for another receipt is refused.
|
|
255
255
|
|
|
256
|
-
`agent-custody reveal receipts/<id>.json --capture-key-env AGENT_CUSTODY_CAPTURE_KEY --issuer-key keys/gateway.pub --out disclosure.json` writes that disclosure from the key, every encrypted value decrypted and checked against its commitment, with its salt. The bundle must verify against the issuer key given, and each ciphertext is bound to its receipt and path, so a bundle someone assembled around another receipt's ciphertexts reveals nothing. The file is written owner-only and never through a symlink. A value withheld as a digest only carries nothing to reveal, and the command says which paths it could not reveal. With `--include-salts` it also writes, at each such path, the salt from the issuer's salts file beside the receipt, for whoever holds the value to add `value` beside; a file with a salt and no value does not verify, so it cannot be handed over unfinished by mistake. Hand the finished file to the reviewer with the receipt, never the salts file.
|
|
256
|
+
`agent-custody reveal receipts/<id>.json --capture-key-env AGENT_CUSTODY_CAPTURE_KEY --issuer-key keys/gateway.pub --out disclosure.json` writes that disclosure from the key, every encrypted value decrypted and checked against its commitment, with its salt. The bundle must verify against the issuer key given, and each ciphertext is bound to its receipt and path, so a bundle someone assembled around another receipt's ciphertexts reveals nothing. The file is written owner-only and never through a symlink. A value withheld as a digest only carries nothing to reveal, and the command says which paths it could not reveal. With `--include-salts` it also writes, at each such path, the salt from the issuer's salts file beside the receipt, for whoever holds the value to add `value` beside; a file with a salt and no value does not verify, so it cannot be handed over unfinished by mistake. Hand the finished file to the reviewer with the receipt, never the salts file. To put receipts into a pull request, use `agent-custody export`: it copies only the receipts that keep their values private, and never the salts ([receipts on a pull request](github-action.md)).
|
|
257
257
|
|
|
258
258
|
The salts file, `receipts/salts/<id>.json`, is written for the owner alone (mode 0600 in a 0700 directory) and is as sensitive as the values: with it a guess can be confirmed. In mode `digest` it is also the only copy of each salt: lose it, and nobody can disclose those values again, not even the party that holds them, since a value alone cannot be checked against its commitment; the receipt then verifies only as committed. Back the directory up with the receipts, under the same access control as the values themselves. Mode `encrypted` carries each salt inside the ciphertext and nowhere else (the salts file holds only the digest-only ones), so the key alone recovers everything; choose it when a key is easier to keep safe than a directory of files. The key must be 32 random bytes: a reviewer sees its sha256 as `keyid` in every encrypted commitment, which is harmless for a random key and an offline oracle for a key derived from a passphrase. `agent-custody prune` removes the salts and the pre-committed authorization with the receipt.
|
|
259
259
|
|
package/docs/verification.md
CHANGED
|
@@ -69,16 +69,21 @@ execution observed executed
|
|
|
69
69
|
|
|
70
70
|
A refused call's `execution` row carries the reason the receipt records, for example `denied: delegation grant has been revoked`, and so does a `withheld` one. Under a capture rule the reason is a commitment, and the row says `(reason withheld under the capture rule)` rather than printing it.
|
|
71
71
|
|
|
72
|
+
## A directory of receipts, as a Markdown table
|
|
73
|
+
|
|
74
|
+
`verify --markdown <dir> --issuer-key <pub>` verifies every bundle in a directory and prints one table: the receipt id, the tool, the status (`executed`, `denied`, `failed`, `error`, `withheld`), and `verified` or the names of the failed checks. It takes the same key and log options as `verify`. It reads regular `.json` files only, not symbolic links in the directory; it follows a link that is itself the file argument or the directory argument, and the table shows only fixed error text or verified fields. It does not combine with `--json` or `--disclose`. The exit code is 1 when one receipt does not verify and when the directory holds no receipt. The [GitHub Action](github-action.md) posts this table on a pull request. A receipt that goes into a repository publishes its arguments and result unless a capture rule withholds them; that page says how to commit private receipts.
|
|
75
|
+
|
|
72
76
|
## What each check means
|
|
73
77
|
|
|
74
78
|
| check | what it establishes | what a failure usually means |
|
|
75
79
|
| --- | --- | --- |
|
|
76
80
|
| receipt signature (issuer key) | the bundle's envelope was signed by a key you trust as an issuer and has not changed since | edited receipt, or an issuer key you do not trust |
|
|
77
81
|
| receipt payload type | the payload is an in-toto Statement with this project's predicate type | wrong file, or a different envelope replayed as a receipt |
|
|
78
|
-
| issuer kind is known | the receipt says whether a gateway or an SDK produced it; the detail shows which, and the framework | a predicate this verifier does not understand |
|
|
82
|
+
| issuer kind is known | the receipt says whether a gateway, a door, or an SDK produced it; the detail shows which, and the framework | a predicate this verifier does not understand |
|
|
79
83
|
| issuer keyid matches signer | the receipt's own claim of who signed it matches the actual signature | mixed-up or forged predicate |
|
|
80
84
|
| gateway receipt carries a delegation | gateway receipts always embed the signed grant they enforced | an SDK receipt relabelled as gateway |
|
|
81
85
|
| gateway receipt carries a policy decision | gateway receipts always record the Cedar decision | same |
|
|
86
|
+
| door receipt carries a policy decision | a door decides every arrival by policy; it carries a grant only when the arrival presented one, so there is no delegation check for it | a record-only receipt relabelled as door |
|
|
82
87
|
| delegation signature (principal key) | the embedded grant was signed by a key you trust as a principal | a grant the principal never issued |
|
|
83
88
|
| delegation chain to the principal | only when the grant embeds a parent: every link is signed by the key its parent names, holds no scope its parent lacks, sits inside its parent's window, and names the same principal, up to three delegations deep | a sub-agent given more than its delegator had, a link signed by the wrong key, a chain that never reaches a trusted principal |
|
|
84
89
|
| delegation binds principal and agent | the grant names the same principal and agent the receipt names, and the principal keyid matches | a valid grant for someone else, spliced in |
|
|
@@ -204,6 +209,8 @@ The remote log also serves `GET /head`, its current tree head signed with the lo
|
|
|
204
209
|
|
|
205
210
|
`vectors/` in the package holds fixed receipts, keys, logs, proofs, and the verdicts this verifier produces for them, generated by `bun run vectors` and checked by the test suite on every run. A verifier written elsewhere proves it agrees by reproducing every verdict. They are published at [agent-custody.dev/receipt/vectors](https://agent-custody.dev/receipt/vectors).
|
|
206
211
|
|
|
212
|
+
The Lean 4 model in [`spec/lean`](../../../spec/lean/README.md) checks the Merkle vectors as a third implementation. It also proves the inclusion check of `log.ts` complete and sound: for every tree, each audit path verifies, and an accepted proof places the leaf at its index unless the node hash has a collision.
|
|
213
|
+
|
|
207
214
|
## Attested execution
|
|
208
215
|
|
|
209
216
|
On a gateway receipt the execution is `observed`: the gateway saw what the upstream returned. An upstream that holds a key can do better and sign its result for the receipt being issued; the gateway embeds the signature, and a verifier given the upstream's public key checks it and reports the execution as `attested` by that key. The check binds the signature to the receipt id, the tool, and the digest of the result content, so a signature cannot be moved between receipts.
|
|
@@ -228,5 +235,5 @@ Receipts hold values: request arguments, results, facts. The log is append-only
|
|
|
228
235
|
node src/cli.ts prune --log log.jsonl --before 2026-06-01T00:00:00Z --receipts receipts
|
|
229
236
|
```
|
|
230
237
|
|
|
231
|
-
Every leaf whose receipt is older than the cutoff is replaced in the file by its leaf hash, and the receipt's bundle file is deleted. The Merkle tree is built from leaf hashes, so every root, every inclusion proof, and every consistency proof for the remaining leaves is unchanged, and `--log` verification of later receipts still passes. Someone who kept a pruned receipt's bundle can still prove it was in the log; nobody holding only the log can recover what it said. Run it on the same schedule as memory retention.
|
|
238
|
+
Every leaf whose receipt is older than the cutoff is replaced in the file by its leaf hash, and the receipt's bundle file is deleted. The Merkle tree is built from leaf hashes, so every root, every inclusion proof, and every consistency proof for the remaining leaves is unchanged, and `--log` verification of later receipts still passes. Someone who kept a pruned receipt's bundle can still prove it was in the log; nobody holding only the log can recover what it said. `prune` holds the log lock (`<log>.lock`) while it rewrites the file, so a process that appends at the same time waits, and its leaf is kept. `prune` waits up to 20 seconds for the lock. Run it on the same schedule as memory retention.
|
|
232
239
|
|
package/package.json
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@agent-custody/receipts",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.15.0",
|
|
4
4
|
"mcpName": "io.github.svayatta/agent-custody",
|
|
5
|
-
"description": "Chain of custody for AI agents: signed, independently verifiable receipts for tool calls. MCP gateway + Cedar policy + Merkle transparency log",
|
|
5
|
+
"description": "Chain of custody for AI agents: signed, independently verifiable receipts for tool calls. MCP gateway + Cedar or Rego policy + Merkle transparency log",
|
|
6
6
|
"license": "Apache-2.0",
|
|
7
7
|
"repository": {
|
|
8
8
|
"type": "git",
|
|
@@ -72,22 +72,23 @@
|
|
|
72
72
|
},
|
|
73
73
|
"dependencies": {
|
|
74
74
|
"@cedar-policy/cedar-wasm": "4.12.0",
|
|
75
|
-
"@modelcontextprotocol/sdk": "
|
|
76
|
-
"
|
|
75
|
+
"@modelcontextprotocol/sdk": "1.30.0",
|
|
76
|
+
"@open-policy-agent/opa-wasm": "1.10.0",
|
|
77
|
+
"zod": "4.5.4"
|
|
77
78
|
},
|
|
78
79
|
"devDependencies": {
|
|
79
80
|
"@electric-sql/pglite": "0.5.8",
|
|
80
|
-
"@langchain/core": "
|
|
81
|
-
"@node-saml/node-saml": "
|
|
82
|
-
"@openai/agents": "
|
|
83
|
-
"@types/node": "
|
|
84
|
-
"ai": "
|
|
81
|
+
"@langchain/core": "1.2.9",
|
|
82
|
+
"@node-saml/node-saml": "5.1.0",
|
|
83
|
+
"@openai/agents": "0.18.0",
|
|
84
|
+
"@types/node": "26.4.1",
|
|
85
|
+
"ai": "7.0.93",
|
|
85
86
|
"langchain": "1.5.15",
|
|
86
|
-
"tsx": "
|
|
87
|
-
"typescript": "
|
|
87
|
+
"tsx": "4.23.13",
|
|
88
|
+
"typescript": "7.0.2",
|
|
88
89
|
"viem": "2.57.4",
|
|
89
|
-
"vitest": "
|
|
90
|
-
"xml-crypto": "
|
|
90
|
+
"vitest": "5.0.0",
|
|
91
|
+
"xml-crypto": "6.4.0"
|
|
91
92
|
},
|
|
92
93
|
"peerDependencies": {
|
|
93
94
|
"@langchain/core": ">=1.0.0",
|