@agent-custody/receipts 0.16.0 → 0.16.2
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 +4 -2
- package/dist/lock.js +2 -2
- package/dist/log.d.ts +10 -0
- package/dist/log.js +55 -5
- package/dist/operations.d.ts +5 -0
- package/dist/operations.js +9 -1
- package/dist/policy.js +46 -3
- package/docs/github-action.md +3 -3
- package/docs/performance.md +79 -0
- package/package.json +5 -2
package/README.md
CHANGED
|
@@ -36,6 +36,7 @@ Anyone holding the public keys can verify a receipt offline. The agent is not tr
|
|
|
36
36
|
- [Completeness](docs/completeness.md): an auditor's read-only key lists the provider's objects and matches each to a verified receipt, so an action taken outside the gateway shows up
|
|
37
37
|
- [Payment mandates and agent identity](docs/mandates.md): the design for fitting grants and receipts to Google's AP2 mandates, Visa's Trusted Agent Protocol and Mastercard Agent Pay; not yet built
|
|
38
38
|
- [Threat model](docs/threat-model.md): every party who could make a receipt false, the move, what stops it, and whether that is a property of the evidence or of the deployment; and what is not defended
|
|
39
|
+
- [What a receipt costs](docs/performance.md): each step of the issuer's hot path in microseconds, measured with `bun run bench`, what is and is not fsynced, and where the time goes
|
|
39
40
|
- [Aligning with IETF SCITT](docs/scitt.md): the receipt as a SCITT signed statement, the log as a transparency service, our RFC 9162 proofs as COSE Receipts (RFC 9942), what differs and the options for each, an agent-action profile, and the steps toward the working group
|
|
40
41
|
|
|
41
42
|
## Getting started
|
|
@@ -307,11 +308,11 @@ src/cli.ts keygen, grant, gateway, door, egress, egress draft-policy, ac
|
|
|
307
308
|
src/retention.ts pruning the log: leaves become their hashes, bundles are removed, proofs survive
|
|
308
309
|
src/index.ts the package's public surface; adapters are exported on ./sdk/<framework> subpaths
|
|
309
310
|
tsconfig.build.json emits dist/ (JavaScript plus declarations) for consumers; the repo itself runs the .ts directly
|
|
310
|
-
scripts/ fake Stripe upstream (signs its results with --key), a second fake upstream, fixture builders for gateway, SDK, door (with a stand-in origin), and the egress witness (with a stand-in destination), the replay builder (`replay.ts`: example 27 and state example 07 as a folder of receipts and public keys anyone can verify with the published packages), demo
|
|
311
|
+
scripts/ fake Stripe upstream (signs its results with --key), a second fake upstream, fixture builders for gateway, SDK, door (with a stand-in origin), and the egress witness (with a stand-in destination), the replay builder (`replay.ts`: example 27 and state example 07 as a folder of receipts and public keys anyone can verify with the published packages), demo, the site's numbers (`bench.ts`), and the per-step hot-path benchmark (`hot-path-bench.ts`, `bun run bench`)
|
|
311
312
|
examples/ runnable tutorials, plus examples/languages/: Python, Go, Java, and Rust clients of the sidecar, run by the test suite, one per aspect; each is run by the test suite
|
|
312
313
|
test/ unit tests per module, end-to-end gateway test, SDK and hook tests,
|
|
313
314
|
adapter tests against the real packages, and tests that run every policy in docs/policies.md and site/guide/egress-policies.md
|
|
314
|
-
docs/ tutorials, usage (gateway), codex-recovery (live demo), approvals, sdk, policies, verification
|
|
315
|
+
docs/ tutorials, usage (gateway), codex-recovery (live demo), approvals, sdk, policies, verification, performance
|
|
315
316
|
```
|
|
316
317
|
|
|
317
318
|
## Plan
|
|
@@ -320,6 +321,7 @@ The design is several producers feeding one verifier. The SDK is the top of the
|
|
|
320
321
|
|
|
321
322
|
**Done**
|
|
322
323
|
|
|
324
|
+
- A cheaper receipt hot path: a Cedar policy is parsed once per text instead of on every decision (a few slots, evicted oldest first), and the local log keeps one descriptor open for its appends, reopened when a prune replaces the file. The log lock reads its owner's start time once per process instead of running `ps` on every append on macOS. The receipt format, the decisions, and what is fsynced are unchanged. `bun run bench` times each step, Cedar and Rego both; [docs/performance.md](docs/performance.md) has the numbers.
|
|
323
325
|
- The egress witness decides by Rego as well as Cedar: a `policyFile` ending in `.rego` is compiled with `opa build` as the gateway compiles one, the input is the Cedar request with plain strings, and a file that does not compile is not loaded. An OPA server is not supported for egress yet ([the egress witness](docs/egress.md#the-same-policy-in-rego), tutorial 44).
|
|
324
326
|
- Drafting an egress policy: `agent-custody egress draft-policy --receipts <dir> --issuer-key <witness.pub>` verifies each egress receipt, keeps the connections the witness relayed for a verified agent, and prints one `permit` per agent and scheme over the `host:port` destinations it used, sorted, with a count and the first and last time seen for each. The output starts with a warning that it permits whatever the agents did, bad destinations included. [Writing egress policies](../../site/guide/egress-policies.md) is the guide, and its examples are tested; tutorial [42-egress-draft.ts](examples/42-egress-draft.ts).
|
|
325
327
|
- An OWASP Agent Control Standard Guardian, as a spike: `agent-custody acs` answers `steps/toolCallRequest` and `steps/toolCallResult` with the Cedar policy's allow or deny, and every decision is a receipt with the new issuer kind `guardian`, signed with the Guardian's key and logged, which ACS's HMAC baseline cannot attribute (ACS §8.6). It publishes the ACS chain head, and its receipts in a log someone else runs answer two of the three directions in ACS issue #18 (a signed checkpoint of `chain_hash`, and signed heads in an external append-only log; Guardians exchanging heads is not done). Handshake, ACS signatures, the other hooks, modify/ask/defer, and provenance are not implemented; [the list](docs/acs.md#what-it-does-not-implement). Example 43.
|
package/dist/lock.js
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
import { randomUUID } from "node:crypto";
|
|
3
3
|
import { closeSync, openSync, readFileSync, renameSync, rmSync, statSync, writeFileSync } from "node:fs";
|
|
4
4
|
import { hostname } from "node:os";
|
|
5
|
-
import { ownerAlive,
|
|
5
|
+
import { ownerAlive, ownProcessStamp } from "./operations.js";
|
|
6
6
|
/** A lock file that holds no readable owner is a lock that is being written. After this age it is a leftover. */
|
|
7
7
|
const UNREADABLE_STALE_MS = 60_000;
|
|
8
8
|
const pause = (ms) => Atomics.wait(new Int32Array(new SharedArrayBuffer(4)), 0, 0, ms);
|
|
@@ -77,7 +77,7 @@ function breakLock(lockFile, judged) {
|
|
|
77
77
|
*/
|
|
78
78
|
export function takeLock(lockFile, staleMs) {
|
|
79
79
|
const token = randomUUID();
|
|
80
|
-
const body = JSON.stringify({ token, pid: process.pid, start:
|
|
80
|
+
const body = JSON.stringify({ token, pid: process.pid, start: ownProcessStamp(), host: hostname() });
|
|
81
81
|
for (let attempt = 0; attempt < 3; attempt++) {
|
|
82
82
|
try {
|
|
83
83
|
const fd = openSync(lockFile, "wx", 0o600);
|
package/dist/log.d.ts
CHANGED
|
@@ -33,6 +33,7 @@ export declare class MerkleLog {
|
|
|
33
33
|
private readonly file;
|
|
34
34
|
private bytes;
|
|
35
35
|
private inode;
|
|
36
|
+
private readonly out;
|
|
36
37
|
constructor(file: string);
|
|
37
38
|
/**
|
|
38
39
|
* Reads the leaves that another handle or process appended since the last read. A file that was replaced or cut
|
|
@@ -47,6 +48,15 @@ export declare class MerkleLog {
|
|
|
47
48
|
private cutTornTail;
|
|
48
49
|
/** Under the lock: reads what other writers appended, then writes this line, so each leaf gets the next index. */
|
|
49
50
|
private appendLine;
|
|
51
|
+
/**
|
|
52
|
+
* Under the lock: writes the line through one descriptor opened for append, so the file is not opened and closed
|
|
53
|
+
* for every leaf. O_APPEND puts each write at the end, after a torn tail was cut too. The descriptor is reopened when
|
|
54
|
+
* the path names another file than the one it holds (a prune renames a new file into place) or no file. Nothing is
|
|
55
|
+
* fsynced, as before: the line is in the kernel's page cache when the append returns.
|
|
56
|
+
*/
|
|
57
|
+
private write;
|
|
58
|
+
/** Closes the append descriptor. The log stays usable: the next append opens the file again. */
|
|
59
|
+
close(): void;
|
|
50
60
|
get size(): number;
|
|
51
61
|
/** Leaf hashes `from` (inclusive) to `to` (exclusive), as hex: what an export carries and what a copy is rebuilt from. */
|
|
52
62
|
leafHashes(from: number, to?: number): string[];
|
package/dist/log.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
// Append-only Merkle log with RFC 6962 / RFC 9162 hashing and inclusion proofs.
|
|
2
2
|
// Leaves are stored as JSONL so anyone holding the file can recompute the root.
|
|
3
3
|
import { createHash } from "node:crypto";
|
|
4
|
-
import { appendFileSync, closeSync, mkdirSync, openSync, readSync, statSync, truncateSync } from "node:fs";
|
|
4
|
+
import { appendFileSync, closeSync, fstatSync, mkdirSync, openSync, readSync, statSync, truncateSync, writeSync } from "node:fs";
|
|
5
5
|
import { dirname } from "node:path";
|
|
6
6
|
import { withLock } from "./lock.js";
|
|
7
7
|
const h = (...parts) => {
|
|
@@ -182,16 +182,25 @@ const LOG_LOCK_WAIT_MS = 20_000;
|
|
|
182
182
|
export function logLocked(file, fn) {
|
|
183
183
|
return withLock(`${file}.lock`, { staleMs: LOCK_STALE_MS, waitMs: LOG_LOCK_WAIT_MS }, fn);
|
|
184
184
|
}
|
|
185
|
+
const appenders = new FinalizationRegistry((out) => {
|
|
186
|
+
if (out.fd !== null)
|
|
187
|
+
try {
|
|
188
|
+
closeSync(out.fd);
|
|
189
|
+
}
|
|
190
|
+
catch { /* already closed */ }
|
|
191
|
+
});
|
|
185
192
|
export class MerkleLog {
|
|
186
193
|
hashes = [];
|
|
187
194
|
tree = new SubtreeCache(this.hashes);
|
|
188
195
|
file;
|
|
189
196
|
bytes = 0;
|
|
190
197
|
inode = -1;
|
|
198
|
+
out = { fd: null, dev: 0, ino: 0 };
|
|
191
199
|
constructor(file) {
|
|
192
200
|
this.file = file;
|
|
193
201
|
mkdirSync(dirname(file), { recursive: true });
|
|
194
202
|
this.catchUp();
|
|
203
|
+
appenders.register(this, this.out);
|
|
195
204
|
}
|
|
196
205
|
/**
|
|
197
206
|
* Reads the leaves that another handle or process appended since the last read. A file that was replaced or cut
|
|
@@ -212,10 +221,10 @@ export class MerkleLog {
|
|
|
212
221
|
this.bytes = 0;
|
|
213
222
|
}
|
|
214
223
|
if (!st)
|
|
215
|
-
return;
|
|
224
|
+
return null;
|
|
216
225
|
this.inode = st.ino;
|
|
217
226
|
if (st.size === this.bytes)
|
|
218
|
-
return;
|
|
227
|
+
return st;
|
|
219
228
|
const buf = Buffer.alloc(st.size - this.bytes);
|
|
220
229
|
const fd = openSync(this.file, "r");
|
|
221
230
|
let got = 0;
|
|
@@ -240,6 +249,7 @@ export class MerkleLog {
|
|
|
240
249
|
// roots, and every proof are the same either way.
|
|
241
250
|
this.hashes.push(typeof parsed === "string" ? leafHash(parsed) : Buffer.from((parsed.pruned ?? parsed.hash), "hex"));
|
|
242
251
|
}
|
|
252
|
+
return st;
|
|
243
253
|
}
|
|
244
254
|
/**
|
|
245
255
|
* Under the lock: a last line with no newline is the remains of a writer that died in the middle of its write. No
|
|
@@ -269,13 +279,53 @@ export class MerkleLog {
|
|
|
269
279
|
/** Under the lock: reads what other writers appended, then writes this line, so each leaf gets the next index. */
|
|
270
280
|
appendLine(line, hash) {
|
|
271
281
|
return logLocked(this.file, () => {
|
|
272
|
-
this.catchUp();
|
|
282
|
+
const st = this.catchUp();
|
|
273
283
|
this.cutTornTail();
|
|
274
|
-
|
|
284
|
+
this.write(line, st);
|
|
275
285
|
this.bytes += Buffer.byteLength(line);
|
|
276
286
|
return this.pushHash(hash());
|
|
277
287
|
});
|
|
278
288
|
}
|
|
289
|
+
/**
|
|
290
|
+
* Under the lock: writes the line through one descriptor opened for append, so the file is not opened and closed
|
|
291
|
+
* for every leaf. O_APPEND puts each write at the end, after a torn tail was cut too. The descriptor is reopened when
|
|
292
|
+
* the path names another file than the one it holds (a prune renames a new file into place) or no file. Nothing is
|
|
293
|
+
* fsynced, as before: the line is in the kernel's page cache when the append returns.
|
|
294
|
+
*/
|
|
295
|
+
write(line, st) {
|
|
296
|
+
const out = this.out;
|
|
297
|
+
if (out.fd === null || !st || st.ino !== out.ino || st.dev !== out.dev) {
|
|
298
|
+
this.close();
|
|
299
|
+
const fd = openSync(this.file, "a");
|
|
300
|
+
const now = fstatSync(fd);
|
|
301
|
+
if (st && (now.ino !== st.ino || now.dev !== st.dev)) {
|
|
302
|
+
closeSync(fd);
|
|
303
|
+
throw new Error(`${this.file} was replaced while its lock was held; the leaf was not appended`);
|
|
304
|
+
}
|
|
305
|
+
out.fd = fd;
|
|
306
|
+
out.dev = now.dev;
|
|
307
|
+
out.ino = now.ino;
|
|
308
|
+
this.inode = now.ino;
|
|
309
|
+
}
|
|
310
|
+
const buf = Buffer.from(line);
|
|
311
|
+
try {
|
|
312
|
+
for (let off = 0; off < buf.length;)
|
|
313
|
+
off += writeSync(out.fd, buf, off);
|
|
314
|
+
}
|
|
315
|
+
catch (e) {
|
|
316
|
+
// the bytes written so far are a torn tail, which the next append cuts; that append reopens the file
|
|
317
|
+
this.close();
|
|
318
|
+
throw e;
|
|
319
|
+
}
|
|
320
|
+
}
|
|
321
|
+
/** Closes the append descriptor. The log stays usable: the next append opens the file again. */
|
|
322
|
+
close() {
|
|
323
|
+
if (this.out.fd === null)
|
|
324
|
+
return;
|
|
325
|
+
const fd = this.out.fd;
|
|
326
|
+
this.out.fd = null;
|
|
327
|
+
closeSync(fd);
|
|
328
|
+
}
|
|
279
329
|
get size() {
|
|
280
330
|
return this.hashes.length;
|
|
281
331
|
}
|
package/dist/operations.d.ts
CHANGED
|
@@ -11,6 +11,11 @@ export declare function processStamp(pid: number): string | undefined;
|
|
|
11
11
|
* UTC: a creator and an operator that run under different TZ values must still get the same text.
|
|
12
12
|
*/
|
|
13
13
|
export declare function psStamp(pid: number): string | undefined;
|
|
14
|
+
/**
|
|
15
|
+
* This process's stamp. It cannot change while the process runs, so it is read once: without /proc, a read runs `ps`,
|
|
16
|
+
* and the log lock records its owner on every append. A read that failed is tried again on the next call.
|
|
17
|
+
*/
|
|
18
|
+
export declare function ownProcessStamp(): string | undefined;
|
|
14
19
|
/**
|
|
15
20
|
* Whether the process that created an operation may still run. A process that is gone is not alive. A live process
|
|
16
21
|
* with the same PID but another start time is a different process (a container restart gives PID 1 again). When a
|
package/dist/operations.js
CHANGED
|
@@ -36,6 +36,13 @@ export function psStamp(pid) {
|
|
|
36
36
|
return undefined;
|
|
37
37
|
}
|
|
38
38
|
let own;
|
|
39
|
+
/**
|
|
40
|
+
* This process's stamp. It cannot change while the process runs, so it is read once: without /proc, a read runs `ps`,
|
|
41
|
+
* and the log lock records its owner on every append. A read that failed is tried again on the next call.
|
|
42
|
+
*/
|
|
43
|
+
export function ownProcessStamp() {
|
|
44
|
+
return own ??= processStamp(process.pid);
|
|
45
|
+
}
|
|
39
46
|
/**
|
|
40
47
|
* Whether the process that created an operation may still run. A process that is gone is not alive. A live process
|
|
41
48
|
* with the same PID but another start time is a different process (a container restart gives PID 1 again). When a
|
|
@@ -131,10 +138,11 @@ export class OperationStore {
|
|
|
131
138
|
const reserved = this.db.prepare("SELECT amount FROM operations WHERE budget = ? AND period = ? AND status <> 'failed'").all(input.budget, period).reduce((sum, row) => sum + BigInt(row.amount), 0n);
|
|
132
139
|
if (reserved + BigInt(input.amount) > BigInt(input.limit))
|
|
133
140
|
throw new Error(`operation budget ${input.budget} exceeded for ${period}`);
|
|
141
|
+
const ownerStart = ownProcessStamp();
|
|
134
142
|
const operation = {
|
|
135
143
|
id: input.id,
|
|
136
144
|
ownerPid: process.pid,
|
|
137
|
-
...(
|
|
145
|
+
...(ownerStart !== undefined ? { ownerStart } : {}),
|
|
138
146
|
issuerKeyid: input.issuerKeyid,
|
|
139
147
|
...(input.providerDigest !== undefined ? { providerDigest: input.providerDigest } : {}),
|
|
140
148
|
idempotencyKey: digestOf({ issuerKeyid: input.issuerKeyid, providerDigest: input.providerDigest ?? null, tool: input.tool, principal: input.principal, agent: input.agent, id: input.id }),
|
package/dist/policy.js
CHANGED
|
@@ -62,17 +62,60 @@ export function reservedCedarForm(value, path = "args") {
|
|
|
62
62
|
}
|
|
63
63
|
return null;
|
|
64
64
|
}
|
|
65
|
-
|
|
65
|
+
/** Policy sets kept parsed, most recently used last. A reload or a second gateway in the process takes another slot. */
|
|
66
|
+
const PARSED_SLOTS = 8;
|
|
67
|
+
const parsed = new Map();
|
|
68
|
+
/** Slot ids that no live entry holds. At most PARSED_SLOTS + 1 ids are ever made. */
|
|
69
|
+
const freeSlots = [];
|
|
70
|
+
let slotsMade = 0;
|
|
71
|
+
/**
|
|
72
|
+
* The policy set parsed once per text, so a decision no longer parses the whole file. Keyed by the text itself: an
|
|
73
|
+
* edited file is a new key, a new parse, and a new digest. A new text takes a slot that no live entry holds, and the
|
|
74
|
+
* least recently used entry is evicted, and its slot freed, only once the new entry is in place: a preparse that throws
|
|
75
|
+
* or fails never changes the set a live entry decides with. Reusing a freed id replaces the set held in cedar-wasm, so
|
|
76
|
+
* neither side grows past PARSED_SLOTS + 1. A failed preparse leaves the slot's previous set in place, so an entry with
|
|
77
|
+
* parse errors must never be evaluated.
|
|
78
|
+
*/
|
|
79
|
+
function parsedPolicy(policyText) {
|
|
80
|
+
const hit = parsed.get(policyText);
|
|
81
|
+
if (hit) {
|
|
82
|
+
parsed.delete(policyText);
|
|
83
|
+
parsed.set(policyText, hit);
|
|
84
|
+
return hit;
|
|
85
|
+
}
|
|
66
86
|
const digest = policyDigest(policyText);
|
|
87
|
+
const slot = freeSlots.pop() ?? `agent-custody-policy-${slotsMade++}`;
|
|
88
|
+
let answer;
|
|
89
|
+
try {
|
|
90
|
+
answer = cedar.preparsePolicySet(slot, { staticPolicies: policyText });
|
|
91
|
+
}
|
|
92
|
+
catch (e) {
|
|
93
|
+
freeSlots.push(slot);
|
|
94
|
+
throw e;
|
|
95
|
+
}
|
|
96
|
+
const entry = { slot, digest, parseErrors: answer.type === "success" ? [] : answer.errors.map((e) => e.message) };
|
|
97
|
+
parsed.set(policyText, entry);
|
|
98
|
+
if (parsed.size > PARSED_SLOTS) {
|
|
99
|
+
const [oldest, evicted] = parsed.entries().next().value;
|
|
100
|
+
parsed.delete(oldest);
|
|
101
|
+
freeSlots.push(evicted.slot);
|
|
102
|
+
}
|
|
103
|
+
return entry;
|
|
104
|
+
}
|
|
105
|
+
export function evaluate(policyText, req) {
|
|
106
|
+
const policy = parsedPolicy(policyText);
|
|
107
|
+
const digest = policy.digest;
|
|
67
108
|
const reserved = reservedCedarForm(req.context.args);
|
|
68
109
|
if (reserved)
|
|
69
110
|
return { decision: "deny", reasons: [], errors: [`argument ${reserved} uses a reserved Cedar form; arguments are data, never entities or extension values`], policyDigest: digest };
|
|
70
|
-
|
|
111
|
+
if (policy.parseErrors.length > 0)
|
|
112
|
+
return { decision: "deny", reasons: [], errors: [...policy.parseErrors], policyDigest: digest };
|
|
113
|
+
const answer = cedar.statefulIsAuthorized({
|
|
71
114
|
principal: { type: "Agent", id: req.agentId },
|
|
72
115
|
action: { type: "Action", id: req.tool },
|
|
73
116
|
resource: { type: "Tool", id: req.tool },
|
|
74
117
|
context: req.context,
|
|
75
|
-
|
|
118
|
+
preparsedPolicySetId: policy.slot,
|
|
76
119
|
entities: [],
|
|
77
120
|
});
|
|
78
121
|
if (answer.type === "failure") {
|
package/docs/github-action.md
CHANGED
|
@@ -20,7 +20,7 @@ jobs:
|
|
|
20
20
|
runs-on: ubuntu-latest
|
|
21
21
|
steps:
|
|
22
22
|
- uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7
|
|
23
|
-
- uses: svayatta/agent-custody@<commit sha of the release> # 0.16.
|
|
23
|
+
- uses: svayatta/agent-custody@<commit sha of the release> # 0.16.2
|
|
24
24
|
with:
|
|
25
25
|
path: .custody/receipts
|
|
26
26
|
issuer-key: .custody/keys/agent.pub
|
|
@@ -37,8 +37,8 @@ Pin the action to the commit of a release, as you pin every other action. A movi
|
|
|
37
37
|
| `principal-key` | the public key files of the principals whose grants a gateway receipt carries, one path on each line; SDK and Claude Code hook receipts need none | |
|
|
38
38
|
| `log-url` | the URL of the log; the action fetches the log key from it (`verify --log-url`) | |
|
|
39
39
|
| `log-id` | the id that every tree head must name (`verify --log-id`) | |
|
|
40
|
-
| `version` | the `@agent-custody/receipts` version that `npx` runs | `0.16.
|
|
41
|
-
| `github-token` | a token that can write pull request comments | `github.token` |
|
|
40
|
+
| `version` | the `@agent-custody/receipts` version that `npx` runs | `0.16.2` |
|
|
41
|
+
| `github-token` | a token that can write pull request comments; an empty value writes the job summary only | `github.token` |
|
|
42
42
|
| `comment-author` | the login of the account that `github-token` posts as; the action edits only a comment from this account | `github-actions[bot]` |
|
|
43
43
|
|
|
44
44
|
## What the action does
|
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
# What a receipt costs
|
|
2
|
+
|
|
3
|
+
A receipt adds a policy decision, two Ed25519 signatures, and a log append to a tool call. This page says how long each step takes on one machine, how to measure it on yours, and what is written to disk.
|
|
4
|
+
|
|
5
|
+
## Measuring it
|
|
6
|
+
|
|
7
|
+
From `packages/receipts`:
|
|
8
|
+
|
|
9
|
+
```bash
|
|
10
|
+
bun run bench
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
This runs `scripts/hot-path-bench.ts` (`node scripts/hot-path-bench.ts [iterations]`, default 20,000 after 2,000 warm-up). It is not part of `ci`. Each iteration issues one receipt the way the issuer does, and times each step on its own:
|
|
14
|
+
|
|
15
|
+
| step | what it times |
|
|
16
|
+
| --- | --- |
|
|
17
|
+
| cedar evaluate | `evaluate()` on the fixture's Cedar policy. The requests vary by tool, amount, customer, and verification, so a decision cache could not answer them from one entry. |
|
|
18
|
+
| receipt signature | `dsseSign` over a receipt statement |
|
|
19
|
+
| log append (all) | `MerkleLog.append` on a local file: the lock, reading what other writers appended, the torn-tail check, the write, and the Merkle push, inclusion proof, and root |
|
|
20
|
+
| of which Merkle | the push, proof, and root alone, on an in-memory copy of the tree |
|
|
21
|
+
| tree-head signature | `dsseSign` over the new tree head |
|
|
22
|
+
| rego evaluate (alternative) | the same requests decided by the same policy in Rego, compiled once with `opa build`. The script checks that both engines give the same decision for each request. It is skipped when `opa` is not on `PATH`. |
|
|
23
|
+
|
|
24
|
+
The reference rows time a bare `appendFileSync`, a bare write on a descriptor held open, and the lock owner stamp (see below). There is no network and no fsync. The script writes to a temporary directory and removes it. The site's three numbers (a decision, a gateway call, a verification) come from `scripts/bench.ts`.
|
|
25
|
+
|
|
26
|
+
## On one machine
|
|
27
|
+
|
|
28
|
+
An Apple M5 laptop, macOS, Node 22.21.1, 9 November 2026, 20,000 receipts per run, means and medians in microseconds. The two runs were interleaved on a machine that was also running other work (load average about 5).
|
|
29
|
+
|
|
30
|
+
| step | before, mean | before, p50 | after, mean | after, p50 |
|
|
31
|
+
| --- | --- | --- | --- | --- |
|
|
32
|
+
| cedar evaluate | 196.5 | 187.8 | 85.9 | 78.8 |
|
|
33
|
+
| receipt signature | 30.4 | 26.9 | 29.7 | 25.5 |
|
|
34
|
+
| log append (all) | 1392.5 | 1305.6 | 1308.6 | 1218.9 |
|
|
35
|
+
| of which Merkle | 10.9 | 9.5 | 9.7 | 8.4 |
|
|
36
|
+
| tree-head signature | 26.1 | 23.1 | 25.0 | 21.7 |
|
|
37
|
+
| rego evaluate (alternative) | 11.3 | 10.0 | 10.0 | 8.4 |
|
|
38
|
+
| appendFileSync (reference) | 22.8 | 19.5 | 28.1 | 24.1 |
|
|
39
|
+
| write on open fd (reference) | 5.0 | 4.4 | 4.4 | 3.3 |
|
|
40
|
+
| lock owner stamp (reference) | 1170.2 | 1095.8 | 1124.8 | 1054.4 |
|
|
41
|
+
|
|
42
|
+
A second interleaved pair agreed: Cedar went from a mean of 178.1 µs to 91.8 µs, and the log append stayed at about 1.3 ms.
|
|
43
|
+
|
|
44
|
+
What changed:
|
|
45
|
+
|
|
46
|
+
- **Cedar parses each policy once.** `evaluate()` used to hand the policy text to Cedar on every call, so Cedar parsed the whole file for every decision, and the text was hashed for the digest every time. Each text is now parsed once with `preparsePolicySet`, and decided with `statefulIsAuthorized`. The cache is keyed by the text, so an edited file is parsed again and gets its new digest. It holds 8 policy sets and evicts the least recently used, only after the new set is in place, and a new text never takes a slot that a live entry holds. A text that does not parse is a deny with Cedar's errors, as before.
|
|
47
|
+
- **The log keeps its file open.** An append used to open and close the file. The log now keeps one descriptor open for append. It reopens the file when a prune has replaced it, when the file was removed, and after a failed write. On this machine the saving, about 20 µs, is smaller than the run-to-run spread of the lock, so the table cannot show it. The reference rows show the write itself.
|
|
48
|
+
|
|
49
|
+
Rego needed no change: a `.rego` file is compiled once per change, and the gateway passes the digest it computed when the file loaded.
|
|
50
|
+
|
|
51
|
+
### The lock reads its owner stamp once
|
|
52
|
+
|
|
53
|
+
On macOS most of a log append was the lock that keeps the processes writing one log in order. Its owner record carries the process's start time, so that a later process with the same PID is not taken for the owner. On Linux that comes from `/proc/<pid>/stat`. macOS has no `/proc`, so each append ran `ps`, about 1.1 ms. A process's start time cannot change while it runs, so it is now read once per process (`ownProcessStamp`), which the operation store already did. A read that failed is tried again on the next lock. The lock record is the same.
|
|
54
|
+
|
|
55
|
+
The same machine and day, interleaved again, before and after this change alone (load average about 4 to 6):
|
|
56
|
+
|
|
57
|
+
| step | before, mean | before, p50 | after, mean | after, p50 |
|
|
58
|
+
| --- | --- | --- | --- | --- |
|
|
59
|
+
| cedar evaluate | 86.3 | 82.2 | 63.0 | 57.3 |
|
|
60
|
+
| receipt signature | 27.7 | 25.1 | 26.2 | 23.7 |
|
|
61
|
+
| log append (all) | 1275.9 | 1219.6 | 160.4 | 137.8 |
|
|
62
|
+
| of which Merkle | 9.4 | 8.5 | 8.3 | 7.4 |
|
|
63
|
+
| tree-head signature | 23.0 | 21.5 | 19.7 | 18.3 |
|
|
64
|
+
| rego evaluate (alternative) | 10.4 | 9.0 | 6.8 | 5.9 |
|
|
65
|
+
| lock owner stamp (reference) | 1090.9 | 1050.4 | 1140.4 | 1086.3 |
|
|
66
|
+
|
|
67
|
+
A second pair agreed: the log append went from a mean of 1350.9 µs to 159.9 µs. The reference row still times one read of the stamp, which a process now pays once. This change touches only the lock, so the lower Cedar and Rego numbers in the "after" runs are most likely the machine with one `ps` fewer per receipt, not anything in the evaluators. The site's gateway call (`scripts/bench.ts`) went from 2.1 ms to 0.7 ms.
|
|
68
|
+
|
|
69
|
+
## Where the rest of the time goes
|
|
70
|
+
|
|
71
|
+
The rest of a log append, about 140 µs at the median, is the lock file itself (an exclusive create, the owner record written and read back, and a remove), reading what other writers appended, and the torn-tail check. A gateway on Linux never paid the `ps` cost.
|
|
72
|
+
|
|
73
|
+
No decision is cached, only the parsed policy. Receipts are signed and appended before the call returns, never after: `authorize` never defers, strict mode defers nothing, and a financial operation's receipt does not defer (see the security invariants in `CONTRIBUTING.md`).
|
|
74
|
+
|
|
75
|
+
## What is on disk when an append returns
|
|
76
|
+
|
|
77
|
+
The log line has been written to the file with `O_APPEND`, so it is in the operating system's page cache. Nothing fsyncs the log: neither `appendFileSync` before nor the held descriptor now. A process crash loses nothing that was acknowledged. A power failure or a kernel crash can lose the last appends. A line cut short by a writer that died during its write is moved to `<log>.torn` by the next append, and that leaf was never acknowledged.
|
|
78
|
+
|
|
79
|
+
Some other files are fsynced: the issuer writes a receipt it keeps in the outbox with `fsync` on the file and its directory, and an approval's single-use record is synced the same way. A receipt bundle, its authorization file, its salts, and the log are not.
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@agent-custody/receipts",
|
|
3
|
-
"version": "0.16.
|
|
3
|
+
"version": "0.16.2",
|
|
4
4
|
"mcpName": "io.github.svayatta/agent-custody",
|
|
5
5
|
"description": "Proof of what your AI agents did. Give your agents real power, without losing control of them: signed, verifiable receipts for tool calls, an MCP gateway with Cedar or Rego policy, and a Merkle transparency log.",
|
|
6
6
|
"license": "Apache-2.0",
|
|
@@ -64,8 +64,10 @@
|
|
|
64
64
|
"build": "tsc -p tsconfig.build.json",
|
|
65
65
|
"typecheck": "tsc --noEmit",
|
|
66
66
|
"test": "vitest run",
|
|
67
|
+
"coverage": "vitest run --coverage.enabled",
|
|
67
68
|
"demo": "tsx scripts/demo.ts",
|
|
68
|
-
"vectors": "tsx scripts/vectors.ts"
|
|
69
|
+
"vectors": "tsx scripts/vectors.ts",
|
|
70
|
+
"bench": "node scripts/hot-path-bench.ts"
|
|
69
71
|
},
|
|
70
72
|
"engines": {
|
|
71
73
|
"node": ">=22"
|
|
@@ -82,6 +84,7 @@
|
|
|
82
84
|
"@node-saml/node-saml": "5.1.0",
|
|
83
85
|
"@openai/agents": "0.18.0",
|
|
84
86
|
"@types/node": "26.4.1",
|
|
87
|
+
"@vitest/coverage-v8": "5.0.0",
|
|
85
88
|
"ai": "7.0.93",
|
|
86
89
|
"langchain": "1.5.15",
|
|
87
90
|
"tsx": "4.23.13",
|